PR bootstrap/63496
[official-gcc.git] / gcc / fortran / target-memory.c
blob012880405c5dc548610c85dfef5f0b3686641178
1 /* Simulate storage of variables into target memory.
2 Copyright (C) 2007-2014 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 "machmode.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "gfortran.h"
29 #include "arith.h"
30 #include "constructor.h"
31 #include "trans.h"
32 #include "trans-const.h"
33 #include "trans-types.h"
34 #include "target-memory.h"
35 #include "wide-int.h"
37 /* --------------------------------------------------------------- */
38 /* Calculate the size of an expression. */
41 static size_t
42 size_integer (int kind)
44 return GET_MODE_SIZE (TYPE_MODE (gfc_get_int_type (kind)));;
48 static size_t
49 size_float (int kind)
51 return GET_MODE_SIZE (TYPE_MODE (gfc_get_real_type (kind)));;
55 static size_t
56 size_complex (int kind)
58 return 2 * size_float (kind);
62 static size_t
63 size_logical (int kind)
65 return GET_MODE_SIZE (TYPE_MODE (gfc_get_logical_type (kind)));;
69 static size_t
70 size_character (int length, int kind)
72 int i = gfc_validate_kind (BT_CHARACTER, kind, false);
73 return length * gfc_character_kinds[i].bit_size / 8;
77 /* Return the size of a single element of the given expression.
78 Identical to gfc_target_expr_size for scalars. */
80 size_t
81 gfc_element_size (gfc_expr *e)
83 tree type;
85 switch (e->ts.type)
87 case BT_INTEGER:
88 return size_integer (e->ts.kind);
89 case BT_REAL:
90 return size_float (e->ts.kind);
91 case BT_COMPLEX:
92 return size_complex (e->ts.kind);
93 case BT_LOGICAL:
94 return size_logical (e->ts.kind);
95 case BT_CHARACTER:
96 if (e->expr_type == EXPR_CONSTANT)
97 return size_character (e->value.character.length, e->ts.kind);
98 else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
99 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
100 && e->ts.u.cl->length->ts.type == BT_INTEGER)
102 int length;
104 gfc_extract_int (e->ts.u.cl->length, &length);
105 return size_character (length, e->ts.kind);
107 else
108 return 0;
110 case BT_HOLLERITH:
111 return e->representation.length;
112 case BT_DERIVED:
113 case BT_CLASS:
114 case BT_VOID:
115 case BT_ASSUMED:
117 /* Determine type size without clobbering the typespec for ISO C
118 binding types. */
119 gfc_typespec ts;
120 HOST_WIDE_INT size;
121 ts = e->ts;
122 type = gfc_typenode_for_spec (&ts);
123 size = int_size_in_bytes (type);
124 gcc_assert (size >= 0);
125 return size;
127 default:
128 gfc_internal_error ("Invalid expression in gfc_element_size.");
129 return 0;
134 /* Return the size of an expression in its target representation. */
136 size_t
137 gfc_target_expr_size (gfc_expr *e)
139 mpz_t tmp;
140 size_t asz;
142 gcc_assert (e != NULL);
144 if (e->rank)
146 if (gfc_array_size (e, &tmp))
147 asz = mpz_get_ui (tmp);
148 else
149 asz = 0;
151 else
152 asz = 1;
154 return asz * gfc_element_size (e);
158 /* The encode_* functions export a value into a buffer, and
159 return the number of bytes of the buffer that have been
160 used. */
162 static unsigned HOST_WIDE_INT
163 encode_array (gfc_expr *expr, unsigned char *buffer, size_t buffer_size)
165 mpz_t array_size;
166 int i;
167 int ptr = 0;
169 gfc_constructor_base ctor = expr->value.constructor;
171 gfc_array_size (expr, &array_size);
172 for (i = 0; i < (int)mpz_get_ui (array_size); i++)
174 ptr += gfc_target_encode_expr (gfc_constructor_lookup_expr (ctor, i),
175 &buffer[ptr], buffer_size - ptr);
178 mpz_clear (array_size);
179 return ptr;
183 static int
184 encode_integer (int kind, mpz_t integer, unsigned char *buffer,
185 size_t buffer_size)
187 return native_encode_expr (gfc_conv_mpz_to_tree (integer, kind),
188 buffer, buffer_size);
192 static int
193 encode_float (int kind, mpfr_t real, unsigned char *buffer, size_t buffer_size)
195 return native_encode_expr (gfc_conv_mpfr_to_tree (real, kind, 0), buffer,
196 buffer_size);
200 static int
201 encode_complex (int kind, mpc_t cmplx,
202 unsigned char *buffer, size_t buffer_size)
204 int size;
205 size = encode_float (kind, mpc_realref (cmplx), &buffer[0], buffer_size);
206 size += encode_float (kind, mpc_imagref (cmplx),
207 &buffer[size], buffer_size - size);
208 return size;
212 static int
213 encode_logical (int kind, int logical, unsigned char *buffer, size_t buffer_size)
215 return native_encode_expr (build_int_cst (gfc_get_logical_type (kind),
216 logical),
217 buffer, buffer_size);
222 gfc_encode_character (int kind, int length, const gfc_char_t *string,
223 unsigned char *buffer, size_t buffer_size)
225 size_t elsize = size_character (1, kind);
226 tree type = gfc_get_char_type (kind);
227 int i;
229 gcc_assert (buffer_size >= size_character (length, kind));
231 for (i = 0; i < length; i++)
232 native_encode_expr (build_int_cst (type, string[i]), &buffer[i*elsize],
233 elsize);
235 return length;
239 static unsigned HOST_WIDE_INT
240 encode_derived (gfc_expr *source, unsigned char *buffer, size_t buffer_size)
242 gfc_constructor *c;
243 gfc_component *cmp;
244 int ptr;
245 tree type;
246 HOST_WIDE_INT size;
248 type = gfc_typenode_for_spec (&source->ts);
250 for (c = gfc_constructor_first (source->value.constructor),
251 cmp = source->ts.u.derived->components;
253 c = gfc_constructor_next (c), cmp = cmp->next)
255 gcc_assert (cmp);
256 if (!c->expr)
257 continue;
258 ptr = TREE_INT_CST_LOW(DECL_FIELD_OFFSET(cmp->backend_decl))
259 + TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(cmp->backend_decl))/8;
261 if (c->expr->expr_type == EXPR_NULL)
263 size = int_size_in_bytes (TREE_TYPE (cmp->backend_decl));
264 gcc_assert (size >= 0);
265 memset (&buffer[ptr], 0, size);
267 else
268 gfc_target_encode_expr (c->expr, &buffer[ptr],
269 buffer_size - ptr);
272 size = int_size_in_bytes (type);
273 gcc_assert (size >= 0);
274 return size;
278 /* Write a constant expression in binary form to a buffer. */
279 unsigned HOST_WIDE_INT
280 gfc_target_encode_expr (gfc_expr *source, unsigned char *buffer,
281 size_t buffer_size)
283 if (source == NULL)
284 return 0;
286 if (source->expr_type == EXPR_ARRAY)
287 return encode_array (source, buffer, buffer_size);
289 gcc_assert (source->expr_type == EXPR_CONSTANT
290 || source->expr_type == EXPR_STRUCTURE
291 || source->expr_type == EXPR_SUBSTRING);
293 /* If we already have a target-memory representation, we use that rather
294 than recreating one. */
295 if (source->representation.string)
297 memcpy (buffer, source->representation.string,
298 source->representation.length);
299 return source->representation.length;
302 switch (source->ts.type)
304 case BT_INTEGER:
305 return encode_integer (source->ts.kind, source->value.integer, buffer,
306 buffer_size);
307 case BT_REAL:
308 return encode_float (source->ts.kind, source->value.real, buffer,
309 buffer_size);
310 case BT_COMPLEX:
311 return encode_complex (source->ts.kind, source->value.complex,
312 buffer, buffer_size);
313 case BT_LOGICAL:
314 return encode_logical (source->ts.kind, source->value.logical, buffer,
315 buffer_size);
316 case BT_CHARACTER:
317 if (source->expr_type == EXPR_CONSTANT || source->ref == NULL)
318 return gfc_encode_character (source->ts.kind,
319 source->value.character.length,
320 source->value.character.string,
321 buffer, buffer_size);
322 else
324 int start, end;
326 gcc_assert (source->expr_type == EXPR_SUBSTRING);
327 gfc_extract_int (source->ref->u.ss.start, &start);
328 gfc_extract_int (source->ref->u.ss.end, &end);
329 return gfc_encode_character (source->ts.kind, MAX(end - start + 1, 0),
330 &source->value.character.string[start-1],
331 buffer, buffer_size);
334 case BT_DERIVED:
335 if (source->ts.u.derived->ts.f90_type == BT_VOID)
337 gfc_constructor *c;
338 gcc_assert (source->expr_type == EXPR_STRUCTURE);
339 c = gfc_constructor_first (source->value.constructor);
340 gcc_assert (c->expr->expr_type == EXPR_CONSTANT
341 && c->expr->ts.type == BT_INTEGER);
342 return encode_integer (gfc_index_integer_kind, c->expr->value.integer,
343 buffer, buffer_size);
346 return encode_derived (source, buffer, buffer_size);
347 default:
348 gfc_internal_error ("Invalid expression in gfc_target_encode_expr.");
349 return 0;
354 static int
355 interpret_array (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
357 gfc_constructor_base base = NULL;
358 int array_size = 1;
359 int i;
360 int ptr = 0;
362 /* Calculate array size from its shape and rank. */
363 gcc_assert (result->rank > 0 && result->shape);
365 for (i = 0; i < result->rank; i++)
366 array_size *= (int)mpz_get_ui (result->shape[i]);
368 /* Iterate over array elements, producing constructors. */
369 for (i = 0; i < array_size; i++)
371 gfc_expr *e = gfc_get_constant_expr (result->ts.type, result->ts.kind,
372 &result->where);
373 e->ts = result->ts;
375 if (e->ts.type == BT_CHARACTER)
376 e->value.character.length = result->value.character.length;
378 gfc_constructor_append_expr (&base, e, &result->where);
380 ptr += gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr, e,
381 true);
384 result->value.constructor = base;
385 return ptr;
390 gfc_interpret_integer (int kind, unsigned char *buffer, size_t buffer_size,
391 mpz_t integer)
393 mpz_init (integer);
394 gfc_conv_tree_to_mpz (integer,
395 native_interpret_expr (gfc_get_int_type (kind),
396 buffer, buffer_size));
397 return size_integer (kind);
402 gfc_interpret_float (int kind, unsigned char *buffer, size_t buffer_size,
403 mpfr_t real)
405 gfc_set_model_kind (kind);
406 mpfr_init (real);
407 gfc_conv_tree_to_mpfr (real,
408 native_interpret_expr (gfc_get_real_type (kind),
409 buffer, buffer_size));
411 return size_float (kind);
416 gfc_interpret_complex (int kind, unsigned char *buffer, size_t buffer_size,
417 mpc_t complex)
419 int size;
420 size = gfc_interpret_float (kind, &buffer[0], buffer_size,
421 mpc_realref (complex));
422 size += gfc_interpret_float (kind, &buffer[size], buffer_size - size,
423 mpc_imagref (complex));
424 return size;
429 gfc_interpret_logical (int kind, unsigned char *buffer, size_t buffer_size,
430 int *logical)
432 tree t = native_interpret_expr (gfc_get_logical_type (kind), buffer,
433 buffer_size);
434 *logical = wi::eq_p (t, 0) ? 0 : 1;
435 return size_logical (kind);
440 gfc_interpret_character (unsigned char *buffer, size_t buffer_size,
441 gfc_expr *result)
443 int i;
445 if (result->ts.u.cl && result->ts.u.cl->length)
446 result->value.character.length =
447 (int) mpz_get_ui (result->ts.u.cl->length->value.integer);
449 gcc_assert (buffer_size >= size_character (result->value.character.length,
450 result->ts.kind));
451 result->value.character.string =
452 gfc_get_wide_string (result->value.character.length + 1);
454 if (result->ts.kind == gfc_default_character_kind)
455 for (i = 0; i < result->value.character.length; i++)
456 result->value.character.string[i] = (gfc_char_t) buffer[i];
457 else
459 mpz_t integer;
460 unsigned bytes = size_character (1, result->ts.kind);
461 mpz_init (integer);
462 gcc_assert (bytes <= sizeof (unsigned long));
464 for (i = 0; i < result->value.character.length; i++)
466 gfc_conv_tree_to_mpz (integer,
467 native_interpret_expr (gfc_get_char_type (result->ts.kind),
468 &buffer[bytes*i], buffer_size-bytes*i));
469 result->value.character.string[i]
470 = (gfc_char_t) mpz_get_ui (integer);
473 mpz_clear (integer);
476 result->value.character.string[result->value.character.length] = '\0';
478 return result->value.character.length;
483 gfc_interpret_derived (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
485 gfc_component *cmp;
486 int ptr;
487 tree type;
489 /* The attributes of the derived type need to be bolted to the floor. */
490 result->expr_type = EXPR_STRUCTURE;
492 cmp = result->ts.u.derived->components;
494 if (result->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
495 && (result->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
496 || result->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
498 gfc_constructor *c;
499 gfc_expr *e;
500 /* Needed as gfc_typenode_for_spec as gfc_typenode_for_spec
501 sets this to BT_INTEGER. */
502 result->ts.type = BT_DERIVED;
503 e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind, &result->where);
504 c = gfc_constructor_append_expr (&result->value.constructor, e, NULL);
505 c->n.component = cmp;
506 gfc_target_interpret_expr (buffer, buffer_size, e, true);
507 e->ts.is_iso_c = 1;
508 return int_size_in_bytes (ptr_type_node);
511 type = gfc_typenode_for_spec (&result->ts);
513 /* Run through the derived type components. */
514 for (;cmp; cmp = cmp->next)
516 gfc_constructor *c;
517 gfc_expr *e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind,
518 &result->where);
519 e->ts = cmp->ts;
521 /* Copy shape, if needed. */
522 if (cmp->as && cmp->as->rank)
524 int n;
526 e->expr_type = EXPR_ARRAY;
527 e->rank = cmp->as->rank;
529 e->shape = gfc_get_shape (e->rank);
530 for (n = 0; n < e->rank; n++)
532 mpz_init_set_ui (e->shape[n], 1);
533 mpz_add (e->shape[n], e->shape[n],
534 cmp->as->upper[n]->value.integer);
535 mpz_sub (e->shape[n], e->shape[n],
536 cmp->as->lower[n]->value.integer);
540 c = gfc_constructor_append_expr (&result->value.constructor, e, NULL);
542 /* The constructor points to the component. */
543 c->n.component = cmp;
545 /* Calculate the offset, which consists of the FIELD_OFFSET in
546 bytes, which appears in multiples of DECL_OFFSET_ALIGN-bit-sized,
547 and additional bits of FIELD_BIT_OFFSET. The code assumes that all
548 sizes of the components are multiples of BITS_PER_UNIT,
549 i.e. there are, e.g., no bit fields. */
551 gcc_assert (cmp->backend_decl);
552 ptr = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (cmp->backend_decl));
553 gcc_assert (ptr % 8 == 0);
554 ptr = ptr/8 + TREE_INT_CST_LOW (DECL_FIELD_OFFSET (cmp->backend_decl));
556 gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr, e, true);
559 return int_size_in_bytes (type);
563 /* Read a binary buffer to a constant expression. */
565 gfc_target_interpret_expr (unsigned char *buffer, size_t buffer_size,
566 gfc_expr *result, bool convert_widechar)
568 if (result->expr_type == EXPR_ARRAY)
569 return interpret_array (buffer, buffer_size, result);
571 switch (result->ts.type)
573 case BT_INTEGER:
574 result->representation.length =
575 gfc_interpret_integer (result->ts.kind, buffer, buffer_size,
576 result->value.integer);
577 break;
579 case BT_REAL:
580 result->representation.length =
581 gfc_interpret_float (result->ts.kind, buffer, buffer_size,
582 result->value.real);
583 break;
585 case BT_COMPLEX:
586 result->representation.length =
587 gfc_interpret_complex (result->ts.kind, buffer, buffer_size,
588 result->value.complex);
589 break;
591 case BT_LOGICAL:
592 result->representation.length =
593 gfc_interpret_logical (result->ts.kind, buffer, buffer_size,
594 &result->value.logical);
595 break;
597 case BT_CHARACTER:
598 result->representation.length =
599 gfc_interpret_character (buffer, buffer_size, result);
600 break;
602 case BT_CLASS:
603 result->ts = CLASS_DATA (result)->ts;
604 /* Fall through. */
605 case BT_DERIVED:
606 result->representation.length =
607 gfc_interpret_derived (buffer, buffer_size, result);
608 gcc_assert (result->representation.length >= 0);
609 break;
611 default:
612 gfc_internal_error ("Invalid expression in gfc_target_interpret_expr.");
613 break;
616 if (result->ts.type == BT_CHARACTER && convert_widechar)
617 result->representation.string
618 = gfc_widechar_to_char (result->value.character.string,
619 result->value.character.length);
620 else
622 result->representation.string =
623 XCNEWVEC (char, result->representation.length + 1);
624 memcpy (result->representation.string, buffer,
625 result->representation.length);
626 result->representation.string[result->representation.length] = '\0';
629 return result->representation.length;
633 /* --------------------------------------------------------------- */
634 /* Two functions used by trans-common.c to write overlapping
635 equivalence initializers to a buffer. This is added to the union
636 and the original initializers freed. */
639 /* Writes the values of a constant expression to a char buffer. If another
640 unequal initializer has already been written to the buffer, this is an
641 error. */
643 static size_t
644 expr_to_char (gfc_expr *e, unsigned char *data, unsigned char *chk, size_t len)
646 int i;
647 int ptr;
648 gfc_constructor *c;
649 gfc_component *cmp;
650 unsigned char *buffer;
652 if (e == NULL)
653 return 0;
655 /* Take a derived type, one component at a time, using the offsets from the backend
656 declaration. */
657 if (e->ts.type == BT_DERIVED)
659 for (c = gfc_constructor_first (e->value.constructor),
660 cmp = e->ts.u.derived->components;
661 c; c = gfc_constructor_next (c), cmp = cmp->next)
663 gcc_assert (cmp && cmp->backend_decl);
664 if (!c->expr)
665 continue;
666 ptr = TREE_INT_CST_LOW(DECL_FIELD_OFFSET(cmp->backend_decl))
667 + TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(cmp->backend_decl))/8;
668 expr_to_char (c->expr, &data[ptr], &chk[ptr], len);
670 return len;
673 /* Otherwise, use the target-memory machinery to write a bitwise image, appropriate
674 to the target, in a buffer and check off the initialized part of the buffer. */
675 len = gfc_target_expr_size (e);
676 buffer = (unsigned char*)alloca (len);
677 len = gfc_target_encode_expr (e, buffer, len);
679 for (i = 0; i < (int)len; i++)
681 if (chk[i] && (buffer[i] != data[i]))
683 gfc_error ("Overlapping unequal initializers in EQUIVALENCE "
684 "at %L", &e->where);
685 return 0;
687 chk[i] = 0xFF;
690 memcpy (data, buffer, len);
691 return len;
695 /* Writes the values from the equivalence initializers to a char* array
696 that will be written to the constructor to make the initializer for
697 the union declaration. */
699 size_t
700 gfc_merge_initializers (gfc_typespec ts, gfc_expr *e, unsigned char *data,
701 unsigned char *chk, size_t length)
703 size_t len = 0;
704 gfc_constructor * c;
706 switch (e->expr_type)
708 case EXPR_CONSTANT:
709 case EXPR_STRUCTURE:
710 len = expr_to_char (e, &data[0], &chk[0], length);
712 break;
714 case EXPR_ARRAY:
715 for (c = gfc_constructor_first (e->value.constructor);
716 c; c = gfc_constructor_next (c))
718 size_t elt_size = gfc_target_expr_size (c->expr);
720 if (mpz_cmp_si (c->offset, 0) != 0)
721 len = elt_size * (size_t)mpz_get_si (c->offset);
723 len = len + gfc_merge_initializers (ts, c->expr, &data[len],
724 &chk[len], length - len);
726 break;
728 default:
729 return 0;
732 return len;
736 /* Transfer the bitpattern of a (integer) BOZ to real or complex variables.
737 When successful, no BOZ or nothing to do, true is returned. */
739 bool
740 gfc_convert_boz (gfc_expr *expr, gfc_typespec *ts)
742 size_t buffer_size, boz_bit_size, ts_bit_size;
743 int index;
744 unsigned char *buffer;
746 if (!expr->is_boz)
747 return true;
749 gcc_assert (expr->expr_type == EXPR_CONSTANT
750 && expr->ts.type == BT_INTEGER);
752 /* Don't convert BOZ to logical, character, derived etc. */
753 if (ts->type == BT_REAL)
755 buffer_size = size_float (ts->kind);
756 ts_bit_size = buffer_size * 8;
758 else if (ts->type == BT_COMPLEX)
760 buffer_size = size_complex (ts->kind);
761 ts_bit_size = buffer_size * 8 / 2;
763 else
764 return true;
766 /* Convert BOZ to the smallest possible integer kind. */
767 boz_bit_size = mpz_sizeinbase (expr->value.integer, 2);
769 if (boz_bit_size > ts_bit_size)
771 gfc_error_now ("BOZ constant at %L is too large (%ld vs %ld bits)",
772 &expr->where, (long) boz_bit_size, (long) ts_bit_size);
773 return false;
776 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
777 if ((unsigned) gfc_integer_kinds[index].bit_size >= ts_bit_size)
778 break;
780 expr->ts.kind = gfc_integer_kinds[index].kind;
781 buffer_size = MAX (buffer_size, size_integer (expr->ts.kind));
783 buffer = (unsigned char*)alloca (buffer_size);
784 encode_integer (expr->ts.kind, expr->value.integer, buffer, buffer_size);
785 mpz_clear (expr->value.integer);
787 if (ts->type == BT_REAL)
789 mpfr_init (expr->value.real);
790 gfc_interpret_float (ts->kind, buffer, buffer_size, expr->value.real);
792 else
794 mpc_init2 (expr->value.complex, mpfr_get_default_prec());
795 gfc_interpret_complex (ts->kind, buffer, buffer_size,
796 expr->value.complex);
798 expr->is_boz = 0;
799 expr->ts.type = ts->type;
800 expr->ts.kind = ts->kind;
802 return true;