1 /* Simulate storage of variables into target memory.
2 Copyright (C) 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Paul Thomas and Brooks Moses
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 #include "constructor.h"
31 #include "trans-const.h"
32 #include "trans-types.h"
33 #include "target-memory.h"
35 /* --------------------------------------------------------------- */
36 /* Calculate the size of an expression. */
39 size_array (gfc_expr
*e
)
42 gfc_constructor
*c
= gfc_constructor_first (e
->value
.constructor
);
43 size_t elt_size
= gfc_target_expr_size (c
->expr
);
45 gfc_array_size (e
, &array_size
);
46 return (size_t)mpz_get_ui (array_size
) * elt_size
;
50 size_integer (int kind
)
52 return GET_MODE_SIZE (TYPE_MODE (gfc_get_int_type (kind
)));;
59 return GET_MODE_SIZE (TYPE_MODE (gfc_get_real_type (kind
)));;
64 size_complex (int kind
)
66 return 2 * size_float (kind
);
71 size_logical (int kind
)
73 return GET_MODE_SIZE (TYPE_MODE (gfc_get_logical_type (kind
)));;
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;
86 gfc_target_expr_size (gfc_expr
*e
)
90 gcc_assert (e
!= NULL
);
92 if (e
->expr_type
== EXPR_ARRAY
)
93 return size_array (e
);
98 return size_integer (e
->ts
.kind
);
100 return size_float (e
->ts
.kind
);
102 return size_complex (e
->ts
.kind
);
104 return size_logical (e
->ts
.kind
);
106 if (e
->expr_type
== EXPR_CONSTANT
)
107 return size_character (e
->value
.character
.length
, e
->ts
.kind
);
108 else if (e
->ts
.u
.cl
!= NULL
&& e
->ts
.u
.cl
->length
!= NULL
109 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
110 && e
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
)
114 gfc_extract_int (e
->ts
.u
.cl
->length
, &length
);
115 return size_character (length
, e
->ts
.kind
);
121 return e
->representation
.length
;
124 /* Determine type size without clobbering the typespec for ISO C
128 type
= gfc_typenode_for_spec (&ts
);
129 return int_size_in_bytes (type
);
132 gfc_internal_error ("Invalid expression in gfc_target_expr_size.");
138 /* The encode_* functions export a value into a buffer, and
139 return the number of bytes of the buffer that have been
143 encode_array (gfc_expr
*expr
, unsigned char *buffer
, size_t buffer_size
)
149 gfc_constructor_base ctor
= expr
->value
.constructor
;
151 gfc_array_size (expr
, &array_size
);
152 for (i
= 0; i
< (int)mpz_get_ui (array_size
); i
++)
154 ptr
+= gfc_target_encode_expr (gfc_constructor_lookup_expr (ctor
, i
),
155 &buffer
[ptr
], buffer_size
- ptr
);
158 mpz_clear (array_size
);
164 encode_integer (int kind
, mpz_t integer
, unsigned char *buffer
,
167 return native_encode_expr (gfc_conv_mpz_to_tree (integer
, kind
),
168 buffer
, buffer_size
);
173 encode_float (int kind
, mpfr_t real
, unsigned char *buffer
, size_t buffer_size
)
175 return native_encode_expr (gfc_conv_mpfr_to_tree (real
, kind
, 0), buffer
,
181 encode_complex (int kind
, mpc_t cmplx
,
182 unsigned char *buffer
, size_t buffer_size
)
185 size
= encode_float (kind
, mpc_realref (cmplx
), &buffer
[0], buffer_size
);
186 size
+= encode_float (kind
, mpc_imagref (cmplx
),
187 &buffer
[size
], buffer_size
- size
);
193 encode_logical (int kind
, int logical
, unsigned char *buffer
, size_t buffer_size
)
195 return native_encode_expr (build_int_cst (gfc_get_logical_type (kind
),
197 buffer
, buffer_size
);
202 gfc_encode_character (int kind
, int length
, const gfc_char_t
*string
,
203 unsigned char *buffer
, size_t buffer_size
)
205 size_t elsize
= size_character (1, kind
);
206 tree type
= gfc_get_char_type (kind
);
209 gcc_assert (buffer_size
>= size_character (length
, kind
));
211 for (i
= 0; i
< length
; i
++)
212 native_encode_expr (build_int_cst (type
, string
[i
]), &buffer
[i
*elsize
],
220 encode_derived (gfc_expr
*source
, unsigned char *buffer
, size_t buffer_size
)
227 type
= gfc_typenode_for_spec (&source
->ts
);
229 for (c
= gfc_constructor_first (source
->value
.constructor
),
230 cmp
= source
->ts
.u
.derived
->components
;
232 c
= gfc_constructor_next (c
), cmp
= cmp
->next
)
237 ptr
= TREE_INT_CST_LOW(DECL_FIELD_OFFSET(cmp
->backend_decl
))
238 + TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(cmp
->backend_decl
))/8;
240 if (c
->expr
->expr_type
== EXPR_NULL
)
241 memset (&buffer
[ptr
], 0,
242 int_size_in_bytes (TREE_TYPE (cmp
->backend_decl
)));
244 gfc_target_encode_expr (c
->expr
, &buffer
[ptr
],
248 return int_size_in_bytes (type
);
252 /* Write a constant expression in binary form to a buffer. */
254 gfc_target_encode_expr (gfc_expr
*source
, unsigned char *buffer
,
260 if (source
->expr_type
== EXPR_ARRAY
)
261 return encode_array (source
, buffer
, buffer_size
);
263 gcc_assert (source
->expr_type
== EXPR_CONSTANT
264 || source
->expr_type
== EXPR_STRUCTURE
265 || source
->expr_type
== EXPR_SUBSTRING
);
267 /* If we already have a target-memory representation, we use that rather
268 than recreating one. */
269 if (source
->representation
.string
)
271 memcpy (buffer
, source
->representation
.string
,
272 source
->representation
.length
);
273 return source
->representation
.length
;
276 switch (source
->ts
.type
)
279 return encode_integer (source
->ts
.kind
, source
->value
.integer
, buffer
,
282 return encode_float (source
->ts
.kind
, source
->value
.real
, buffer
,
285 return encode_complex (source
->ts
.kind
, source
->value
.complex,
286 buffer
, buffer_size
);
288 return encode_logical (source
->ts
.kind
, source
->value
.logical
, buffer
,
291 if (source
->expr_type
== EXPR_CONSTANT
|| source
->ref
== NULL
)
292 return gfc_encode_character (source
->ts
.kind
,
293 source
->value
.character
.length
,
294 source
->value
.character
.string
,
295 buffer
, buffer_size
);
300 gcc_assert (source
->expr_type
== EXPR_SUBSTRING
);
301 gfc_extract_int (source
->ref
->u
.ss
.start
, &start
);
302 gfc_extract_int (source
->ref
->u
.ss
.end
, &end
);
303 return gfc_encode_character (source
->ts
.kind
, MAX(end
- start
+ 1, 0),
304 &source
->value
.character
.string
[start
-1],
305 buffer
, buffer_size
);
309 return encode_derived (source
, buffer
, buffer_size
);
311 gfc_internal_error ("Invalid expression in gfc_target_encode_expr.");
318 interpret_array (unsigned char *buffer
, size_t buffer_size
, gfc_expr
*result
)
320 gfc_constructor_base base
= NULL
;
325 /* Calculate array size from its shape and rank. */
326 gcc_assert (result
->rank
> 0 && result
->shape
);
328 for (i
= 0; i
< result
->rank
; i
++)
329 array_size
*= (int)mpz_get_ui (result
->shape
[i
]);
331 /* Iterate over array elements, producing constructors. */
332 for (i
= 0; i
< array_size
; i
++)
334 gfc_expr
*e
= gfc_get_constant_expr (result
->ts
.type
, result
->ts
.kind
,
338 if (e
->ts
.type
== BT_CHARACTER
)
339 e
->value
.character
.length
= result
->value
.character
.length
;
341 gfc_constructor_append_expr (&base
, e
, &result
->where
);
343 ptr
+= gfc_target_interpret_expr (&buffer
[ptr
], buffer_size
- ptr
, e
,
347 result
->value
.constructor
= base
;
353 gfc_interpret_integer (int kind
, unsigned char *buffer
, size_t buffer_size
,
357 gfc_conv_tree_to_mpz (integer
,
358 native_interpret_expr (gfc_get_int_type (kind
),
359 buffer
, buffer_size
));
360 return size_integer (kind
);
365 gfc_interpret_float (int kind
, unsigned char *buffer
, size_t buffer_size
,
368 gfc_set_model_kind (kind
);
370 gfc_conv_tree_to_mpfr (real
,
371 native_interpret_expr (gfc_get_real_type (kind
),
372 buffer
, buffer_size
));
374 return size_float (kind
);
379 gfc_interpret_complex (int kind
, unsigned char *buffer
, size_t buffer_size
,
383 size
= gfc_interpret_float (kind
, &buffer
[0], buffer_size
,
384 mpc_realref (complex));
385 size
+= gfc_interpret_float (kind
, &buffer
[size
], buffer_size
- size
,
386 mpc_imagref (complex));
392 gfc_interpret_logical (int kind
, unsigned char *buffer
, size_t buffer_size
,
395 tree t
= native_interpret_expr (gfc_get_logical_type (kind
), buffer
,
397 *logical
= double_int_zero_p (tree_to_double_int (t
))
399 return size_logical (kind
);
404 gfc_interpret_character (unsigned char *buffer
, size_t buffer_size
,
409 if (result
->ts
.u
.cl
&& result
->ts
.u
.cl
->length
)
410 result
->value
.character
.length
=
411 (int) mpz_get_ui (result
->ts
.u
.cl
->length
->value
.integer
);
413 gcc_assert (buffer_size
>= size_character (result
->value
.character
.length
,
415 result
->value
.character
.string
=
416 gfc_get_wide_string (result
->value
.character
.length
+ 1);
418 if (result
->ts
.kind
== gfc_default_character_kind
)
419 for (i
= 0; i
< result
->value
.character
.length
; i
++)
420 result
->value
.character
.string
[i
] = (gfc_char_t
) buffer
[i
];
424 unsigned bytes
= size_character (1, result
->ts
.kind
);
426 gcc_assert (bytes
<= sizeof (unsigned long));
428 for (i
= 0; i
< result
->value
.character
.length
; i
++)
430 gfc_conv_tree_to_mpz (integer
,
431 native_interpret_expr (gfc_get_char_type (result
->ts
.kind
),
432 &buffer
[bytes
*i
], buffer_size
-bytes
*i
));
433 result
->value
.character
.string
[i
]
434 = (gfc_char_t
) mpz_get_ui (integer
);
440 result
->value
.character
.string
[result
->value
.character
.length
] = '\0';
442 return result
->value
.character
.length
;
447 gfc_interpret_derived (unsigned char *buffer
, size_t buffer_size
, gfc_expr
*result
)
453 /* The attributes of the derived type need to be bolted to the floor. */
454 result
->expr_type
= EXPR_STRUCTURE
;
456 cmp
= result
->ts
.u
.derived
->components
;
458 if (result
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_C_BINDING
459 && (result
->ts
.u
.derived
->intmod_sym_id
== ISOCBINDING_PTR
460 || result
->ts
.u
.derived
->intmod_sym_id
== ISOCBINDING_FUNPTR
))
464 /* Needed as gfc_typenode_for_spec as gfc_typenode_for_spec
465 sets this to BT_INTEGER. */
466 result
->ts
.type
= BT_DERIVED
;
467 e
= gfc_get_constant_expr (cmp
->ts
.type
, cmp
->ts
.kind
, &result
->where
);
468 c
= gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
469 c
->n
.component
= cmp
;
470 gfc_target_interpret_expr (buffer
, buffer_size
, e
, true);
472 return int_size_in_bytes (ptr_type_node
);
475 type
= gfc_typenode_for_spec (&result
->ts
);
477 /* Run through the derived type components. */
478 for (;cmp
; cmp
= cmp
->next
)
481 gfc_expr
*e
= gfc_get_constant_expr (cmp
->ts
.type
, cmp
->ts
.kind
,
485 /* Copy shape, if needed. */
486 if (cmp
->as
&& cmp
->as
->rank
)
490 e
->expr_type
= EXPR_ARRAY
;
491 e
->rank
= cmp
->as
->rank
;
493 e
->shape
= gfc_get_shape (e
->rank
);
494 for (n
= 0; n
< e
->rank
; n
++)
496 mpz_init_set_ui (e
->shape
[n
], 1);
497 mpz_add (e
->shape
[n
], e
->shape
[n
],
498 cmp
->as
->upper
[n
]->value
.integer
);
499 mpz_sub (e
->shape
[n
], e
->shape
[n
],
500 cmp
->as
->lower
[n
]->value
.integer
);
504 c
= gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
506 /* The constructor points to the component. */
507 c
->n
.component
= cmp
;
509 /* Calculate the offset, which consists of the FIELD_OFFSET in
510 bytes, which appears in multiples of DECL_OFFSET_ALIGN-bit-sized,
511 and additional bits of FIELD_BIT_OFFSET. The code assumes that all
512 sizes of the components are multiples of BITS_PER_UNIT,
513 i.e. there are, e.g., no bit fields. */
515 gcc_assert (cmp
->backend_decl
);
516 ptr
= TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (cmp
->backend_decl
));
517 gcc_assert (ptr
% 8 == 0);
518 ptr
= ptr
/8 + TREE_INT_CST_LOW (DECL_FIELD_OFFSET (cmp
->backend_decl
));
520 gfc_target_interpret_expr (&buffer
[ptr
], buffer_size
- ptr
, e
, true);
523 return int_size_in_bytes (type
);
527 /* Read a binary buffer to a constant expression. */
529 gfc_target_interpret_expr (unsigned char *buffer
, size_t buffer_size
,
530 gfc_expr
*result
, bool convert_widechar
)
532 if (result
->expr_type
== EXPR_ARRAY
)
533 return interpret_array (buffer
, buffer_size
, result
);
535 switch (result
->ts
.type
)
538 result
->representation
.length
=
539 gfc_interpret_integer (result
->ts
.kind
, buffer
, buffer_size
,
540 result
->value
.integer
);
544 result
->representation
.length
=
545 gfc_interpret_float (result
->ts
.kind
, buffer
, buffer_size
,
550 result
->representation
.length
=
551 gfc_interpret_complex (result
->ts
.kind
, buffer
, buffer_size
,
552 result
->value
.complex);
556 result
->representation
.length
=
557 gfc_interpret_logical (result
->ts
.kind
, buffer
, buffer_size
,
558 &result
->value
.logical
);
562 result
->representation
.length
=
563 gfc_interpret_character (buffer
, buffer_size
, result
);
567 result
->representation
.length
=
568 gfc_interpret_derived (buffer
, buffer_size
, result
);
572 gfc_internal_error ("Invalid expression in gfc_target_interpret_expr.");
576 if (result
->ts
.type
== BT_CHARACTER
&& convert_widechar
)
577 result
->representation
.string
578 = gfc_widechar_to_char (result
->value
.character
.string
,
579 result
->value
.character
.length
);
582 result
->representation
.string
=
583 XCNEWVEC (char, result
->representation
.length
+ 1);
584 memcpy (result
->representation
.string
, buffer
,
585 result
->representation
.length
);
586 result
->representation
.string
[result
->representation
.length
] = '\0';
589 return result
->representation
.length
;
593 /* --------------------------------------------------------------- */
594 /* Two functions used by trans-common.c to write overlapping
595 equivalence initializers to a buffer. This is added to the union
596 and the original initializers freed. */
599 /* Writes the values of a constant expression to a char buffer. If another
600 unequal initializer has already been written to the buffer, this is an
604 expr_to_char (gfc_expr
*e
, unsigned char *data
, unsigned char *chk
, size_t len
)
610 unsigned char *buffer
;
615 /* Take a derived type, one component at a time, using the offsets from the backend
617 if (e
->ts
.type
== BT_DERIVED
)
619 for (c
= gfc_constructor_first (e
->value
.constructor
),
620 cmp
= e
->ts
.u
.derived
->components
;
621 c
; c
= gfc_constructor_next (c
), cmp
= cmp
->next
)
623 gcc_assert (cmp
&& cmp
->backend_decl
);
626 ptr
= TREE_INT_CST_LOW(DECL_FIELD_OFFSET(cmp
->backend_decl
))
627 + TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(cmp
->backend_decl
))/8;
628 expr_to_char (c
->expr
, &data
[ptr
], &chk
[ptr
], len
);
633 /* Otherwise, use the target-memory machinery to write a bitwise image, appropriate
634 to the target, in a buffer and check off the initialized part of the buffer. */
635 len
= gfc_target_expr_size (e
);
636 buffer
= (unsigned char*)alloca (len
);
637 len
= gfc_target_encode_expr (e
, buffer
, len
);
639 for (i
= 0; i
< (int)len
; i
++)
641 if (chk
[i
] && (buffer
[i
] != data
[i
]))
643 gfc_error ("Overlapping unequal initializers in EQUIVALENCE "
650 memcpy (data
, buffer
, len
);
655 /* Writes the values from the equivalence initializers to a char* array
656 that will be written to the constructor to make the initializer for
657 the union declaration. */
660 gfc_merge_initializers (gfc_typespec ts
, gfc_expr
*e
, unsigned char *data
,
661 unsigned char *chk
, size_t length
)
666 switch (e
->expr_type
)
670 len
= expr_to_char (e
, &data
[0], &chk
[0], length
);
675 for (c
= gfc_constructor_first (e
->value
.constructor
);
676 c
; c
= gfc_constructor_next (c
))
678 size_t elt_size
= gfc_target_expr_size (c
->expr
);
681 len
= elt_size
* (size_t)mpz_get_si (c
->offset
);
683 len
= len
+ gfc_merge_initializers (ts
, c
->expr
, &data
[len
],
684 &chk
[len
], length
- len
);
696 /* Transfer the bitpattern of a (integer) BOZ to real or complex variables.
697 When successful, no BOZ or nothing to do, true is returned. */
700 gfc_convert_boz (gfc_expr
*expr
, gfc_typespec
*ts
)
702 size_t buffer_size
, boz_bit_size
, ts_bit_size
;
704 unsigned char *buffer
;
709 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
710 && expr
->ts
.type
== BT_INTEGER
);
712 /* Don't convert BOZ to logical, character, derived etc. */
713 if (ts
->type
== BT_REAL
)
715 buffer_size
= size_float (ts
->kind
);
716 ts_bit_size
= buffer_size
* 8;
718 else if (ts
->type
== BT_COMPLEX
)
720 buffer_size
= size_complex (ts
->kind
);
721 ts_bit_size
= buffer_size
* 8 / 2;
726 /* Convert BOZ to the smallest possible integer kind. */
727 boz_bit_size
= mpz_sizeinbase (expr
->value
.integer
, 2);
729 if (boz_bit_size
> ts_bit_size
)
731 gfc_error_now ("BOZ constant at %L is too large (%ld vs %ld bits)",
732 &expr
->where
, (long) boz_bit_size
, (long) ts_bit_size
);
736 for (index
= 0; gfc_integer_kinds
[index
].kind
!= 0; ++index
)
737 if ((unsigned) gfc_integer_kinds
[index
].bit_size
>= ts_bit_size
)
740 expr
->ts
.kind
= gfc_integer_kinds
[index
].kind
;
741 buffer_size
= MAX (buffer_size
, size_integer (expr
->ts
.kind
));
743 buffer
= (unsigned char*)alloca (buffer_size
);
744 encode_integer (expr
->ts
.kind
, expr
->value
.integer
, buffer
, buffer_size
);
745 mpz_clear (expr
->value
.integer
);
747 if (ts
->type
== BT_REAL
)
749 mpfr_init (expr
->value
.real
);
750 gfc_interpret_float (ts
->kind
, buffer
, buffer_size
, expr
->value
.real
);
754 mpc_init2 (expr
->value
.complex, mpfr_get_default_prec());
755 gfc_interpret_complex (ts
->kind
, buffer
, buffer_size
,
756 expr
->value
.complex);
759 expr
->ts
.type
= ts
->type
;
760 expr
->ts
.kind
= ts
->kind
;