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
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
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/>. */
23 #include "coretypes.h"
27 #include "stor-layout.h"
30 #include "constructor.h"
32 #include "trans-const.h"
33 #include "trans-types.h"
34 #include "target-memory.h"
37 /* --------------------------------------------------------------- */
38 /* Calculate the size of an expression. */
42 size_integer (int kind
)
44 return GET_MODE_SIZE (TYPE_MODE (gfc_get_int_type (kind
)));;
51 return GET_MODE_SIZE (TYPE_MODE (gfc_get_real_type (kind
)));;
56 size_complex (int kind
)
58 return 2 * size_float (kind
);
63 size_logical (int kind
)
65 return GET_MODE_SIZE (TYPE_MODE (gfc_get_logical_type (kind
)));;
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. */
81 gfc_element_size (gfc_expr
*e
)
88 return size_integer (e
->ts
.kind
);
90 return size_float (e
->ts
.kind
);
92 return size_complex (e
->ts
.kind
);
94 return size_logical (e
->ts
.kind
);
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
)
104 gfc_extract_int (e
->ts
.u
.cl
->length
, &length
);
105 return size_character (length
, e
->ts
.kind
);
111 return e
->representation
.length
;
117 /* Determine type size without clobbering the typespec for ISO C
122 type
= gfc_typenode_for_spec (&ts
);
123 size
= int_size_in_bytes (type
);
124 gcc_assert (size
>= 0);
128 gfc_internal_error ("Invalid expression in gfc_element_size.");
134 /* Return the size of an expression in its target representation. */
137 gfc_target_expr_size (gfc_expr
*e
)
142 gcc_assert (e
!= NULL
);
146 if (gfc_array_size (e
, &tmp
))
147 asz
= mpz_get_ui (tmp
);
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
162 static unsigned HOST_WIDE_INT
163 encode_array (gfc_expr
*expr
, unsigned char *buffer
, size_t buffer_size
)
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
);
184 encode_integer (int kind
, mpz_t integer
, unsigned char *buffer
,
187 return native_encode_expr (gfc_conv_mpz_to_tree (integer
, kind
),
188 buffer
, buffer_size
);
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
,
201 encode_complex (int kind
, mpc_t cmplx
,
202 unsigned char *buffer
, size_t buffer_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
);
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
),
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
);
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
],
239 static unsigned HOST_WIDE_INT
240 encode_derived (gfc_expr
*source
, unsigned char *buffer
, size_t buffer_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
)
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
);
268 gfc_target_encode_expr (c
->expr
, &buffer
[ptr
],
272 size
= int_size_in_bytes (type
);
273 gcc_assert (size
>= 0);
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
,
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
)
305 return encode_integer (source
->ts
.kind
, source
->value
.integer
, buffer
,
308 return encode_float (source
->ts
.kind
, source
->value
.real
, buffer
,
311 return encode_complex (source
->ts
.kind
, source
->value
.complex,
312 buffer
, buffer_size
);
314 return encode_logical (source
->ts
.kind
, source
->value
.logical
, buffer
,
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
);
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
);
335 if (source
->ts
.u
.derived
->ts
.f90_type
== BT_VOID
)
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
);
348 gfc_internal_error ("Invalid expression in gfc_target_encode_expr.");
355 interpret_array (unsigned char *buffer
, size_t buffer_size
, gfc_expr
*result
)
357 gfc_constructor_base base
= NULL
;
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
,
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
,
384 result
->value
.constructor
= base
;
390 gfc_interpret_integer (int kind
, unsigned char *buffer
, size_t buffer_size
,
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
,
405 gfc_set_model_kind (kind
);
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
,
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));
429 gfc_interpret_logical (int kind
, unsigned char *buffer
, size_t buffer_size
,
432 tree t
= native_interpret_expr (gfc_get_logical_type (kind
), buffer
,
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
,
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
,
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
];
460 unsigned bytes
= size_character (1, result
->ts
.kind
);
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
);
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
)
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
))
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);
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
)
517 gfc_expr
*e
= gfc_get_constant_expr (cmp
->ts
.type
, cmp
->ts
.kind
,
521 /* Copy shape, if needed. */
522 if (cmp
->as
&& cmp
->as
->rank
)
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
)
574 result
->representation
.length
=
575 gfc_interpret_integer (result
->ts
.kind
, buffer
, buffer_size
,
576 result
->value
.integer
);
580 result
->representation
.length
=
581 gfc_interpret_float (result
->ts
.kind
, buffer
, buffer_size
,
586 result
->representation
.length
=
587 gfc_interpret_complex (result
->ts
.kind
, buffer
, buffer_size
,
588 result
->value
.complex);
592 result
->representation
.length
=
593 gfc_interpret_logical (result
->ts
.kind
, buffer
, buffer_size
,
594 &result
->value
.logical
);
598 result
->representation
.length
=
599 gfc_interpret_character (buffer
, buffer_size
, result
);
603 result
->ts
= CLASS_DATA (result
)->ts
;
606 result
->representation
.length
=
607 gfc_interpret_derived (buffer
, buffer_size
, result
);
608 gcc_assert (result
->representation
.length
>= 0);
612 gfc_internal_error ("Invalid expression in gfc_target_interpret_expr.");
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
);
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
644 expr_to_char (gfc_expr
*e
, unsigned char *data
, unsigned char *chk
, size_t len
)
650 unsigned char *buffer
;
655 /* Take a derived type, one component at a time, using the offsets from the backend
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
);
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
);
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 "
690 memcpy (data
, buffer
, 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. */
700 gfc_merge_initializers (gfc_typespec ts
, gfc_expr
*e
, unsigned char *data
,
701 unsigned char *chk
, size_t length
)
706 switch (e
->expr_type
)
710 len
= expr_to_char (e
, &data
[0], &chk
[0], length
);
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
);
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. */
740 gfc_convert_boz (gfc_expr
*expr
, gfc_typespec
*ts
)
742 size_t buffer_size
, boz_bit_size
, ts_bit_size
;
744 unsigned char *buffer
;
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;
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
);
776 for (index
= 0; gfc_integer_kinds
[index
].kind
!= 0; ++index
)
777 if ((unsigned) gfc_integer_kinds
[index
].bit_size
>= ts_bit_size
)
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
);
794 mpc_init2 (expr
->value
.complex, mpfr_get_default_prec());
795 gfc_interpret_complex (ts
->kind
, buffer
, buffer_size
,
796 expr
->value
.complex);
799 expr
->ts
.type
= ts
->type
;
800 expr
->ts
.kind
= ts
->kind
;