]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/target-memory.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / fortran / target-memory.c
CommitLineData
7433458d 1/* Simulate storage of variables into target memory.
5624e564 2 Copyright (C) 2007-2015 Free Software Foundation, Inc.
7433458d
PT
3 Contributed by Paul Thomas and Brooks Moses
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
d234d788 9Software Foundation; either version 3, or (at your option) any later
7433458d
PT
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
d234d788
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
7433458d
PT
20
21#include "config.h"
22#include "system.h"
953bee7c 23#include "coretypes.h"
7433458d 24#include "flags.h"
40e23961 25#include "alias.h"
7433458d 26#include "tree.h"
40e23961 27#include "fold-const.h"
d8a2d370 28#include "stor-layout.h"
7433458d
PT
29#include "gfortran.h"
30#include "arith.h"
b7e75771 31#include "constructor.h"
7433458d
PT
32#include "trans.h"
33#include "trans-const.h"
34#include "trans-types.h"
35#include "target-memory.h"
36
25812571 37/* --------------------------------------------------------------- */
7433458d
PT
38/* Calculate the size of an expression. */
39
7433458d
PT
40
41static size_t
42size_integer (int kind)
43{
44 return GET_MODE_SIZE (TYPE_MODE (gfc_get_int_type (kind)));;
45}
46
47
48static size_t
49size_float (int kind)
50{
51 return GET_MODE_SIZE (TYPE_MODE (gfc_get_real_type (kind)));;
52}
53
54
55static size_t
56size_complex (int kind)
57{
58 return 2 * size_float (kind);
59}
60
61
62static size_t
63size_logical (int kind)
64{
65 return GET_MODE_SIZE (TYPE_MODE (gfc_get_logical_type (kind)));;
66}
67
68
69static size_t
00660189 70size_character (int length, int kind)
7433458d 71{
d393bbd7
FXC
72 int i = gfc_validate_kind (BT_CHARACTER, kind, false);
73 return length * gfc_character_kinds[i].bit_size / 8;
7433458d
PT
74}
75
76
e361d18d
JW
77/* Return the size of a single element of the given expression.
78 Identical to gfc_target_expr_size for scalars. */
79
7433458d 80size_t
e361d18d 81gfc_element_size (gfc_expr *e)
7433458d
PT
82{
83 tree type;
84
7433458d
PT
85 switch (e->ts.type)
86 {
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:
86dbed7d
TK
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)
101 {
102 int length;
103
104 gfc_extract_int (e->ts.u.cl->length, &length);
105 return size_character (length, e->ts.kind);
106 }
b7d36ea3 107 else
86dbed7d
TK
108 return 0;
109
3b45d6c4
BM
110 case BT_HOLLERITH:
111 return e->representation.length;
7433458d 112 case BT_DERIVED:
fa1ed658 113 case BT_CLASS:
25812571
PT
114 case BT_VOID:
115 case BT_ASSUMED:
48b155b9
TK
116 {
117 /* Determine type size without clobbering the typespec for ISO C
118 binding types. */
119 gfc_typespec ts;
c6423ef3 120 HOST_WIDE_INT size;
48b155b9
TK
121 ts = e->ts;
122 type = gfc_typenode_for_spec (&ts);
c6423ef3
TB
123 size = int_size_in_bytes (type);
124 gcc_assert (size >= 0);
125 return size;
48b155b9 126 }
7433458d 127 default:
e361d18d 128 gfc_internal_error ("Invalid expression in gfc_element_size.");
7433458d
PT
129 return 0;
130 }
131}
132
133
e361d18d
JW
134/* Return the size of an expression in its target representation. */
135
136size_t
137gfc_target_expr_size (gfc_expr *e)
138{
139 mpz_t tmp;
140 size_t asz;
141
142 gcc_assert (e != NULL);
143
144 if (e->rank)
145 {
146 if (gfc_array_size (e, &tmp))
147 asz = mpz_get_ui (tmp);
148 else
149 asz = 0;
150 }
151 else
152 asz = 1;
153
154 return asz * gfc_element_size (e);
155}
156
157
25812571 158/* The encode_* functions export a value into a buffer, and
7433458d
PT
159 return the number of bytes of the buffer that have been
160 used. */
161
fd2805e1 162static unsigned HOST_WIDE_INT
7433458d
PT
163encode_array (gfc_expr *expr, unsigned char *buffer, size_t buffer_size)
164{
165 mpz_t array_size;
166 int i;
167 int ptr = 0;
168
b7e75771
JD
169 gfc_constructor_base ctor = expr->value.constructor;
170
7433458d
PT
171 gfc_array_size (expr, &array_size);
172 for (i = 0; i < (int)mpz_get_ui (array_size); i++)
173 {
b7e75771 174 ptr += gfc_target_encode_expr (gfc_constructor_lookup_expr (ctor, i),
7433458d
PT
175 &buffer[ptr], buffer_size - ptr);
176 }
177
178 mpz_clear (array_size);
179 return ptr;
180}
181
182
183static int
184encode_integer (int kind, mpz_t integer, unsigned char *buffer,
185 size_t buffer_size)
186{
187 return native_encode_expr (gfc_conv_mpz_to_tree (integer, kind),
188 buffer, buffer_size);
189}
190
191
192static int
193encode_float (int kind, mpfr_t real, unsigned char *buffer, size_t buffer_size)
194{
346a77d1 195 return native_encode_expr (gfc_conv_mpfr_to_tree (real, kind, 0), buffer,
7433458d
PT
196 buffer_size);
197}
198
199
200static int
d0d92baf 201encode_complex (int kind, mpc_t cmplx,
eb6f9a86 202 unsigned char *buffer, size_t buffer_size)
7433458d
PT
203{
204 int size;
d0d92baf
KG
205 size = encode_float (kind, mpc_realref (cmplx), &buffer[0], buffer_size);
206 size += encode_float (kind, mpc_imagref (cmplx),
eb6f9a86 207 &buffer[size], buffer_size - size);
7433458d
PT
208 return size;
209}
210
211
212static int
213encode_logical (int kind, int logical, unsigned char *buffer, size_t buffer_size)
214{
215 return native_encode_expr (build_int_cst (gfc_get_logical_type (kind),
216 logical),
217 buffer, buffer_size);
218}
219
220
d393bbd7
FXC
221int
222gfc_encode_character (int kind, int length, const gfc_char_t *string,
223 unsigned char *buffer, size_t buffer_size)
7433458d 224{
d393bbd7
FXC
225 size_t elsize = size_character (1, kind);
226 tree type = gfc_get_char_type (kind);
227 int i;
00660189
FXC
228
229 gcc_assert (buffer_size >= size_character (length, kind));
00660189 230
d393bbd7
FXC
231 for (i = 0; i < length; i++)
232 native_encode_expr (build_int_cst (type, string[i]), &buffer[i*elsize],
233 elsize);
00660189 234
7433458d
PT
235 return length;
236}
237
238
fd2805e1 239static unsigned HOST_WIDE_INT
7433458d
PT
240encode_derived (gfc_expr *source, unsigned char *buffer, size_t buffer_size)
241{
b7e75771 242 gfc_constructor *c;
7433458d
PT
243 gfc_component *cmp;
244 int ptr;
245 tree type;
fd2805e1 246 HOST_WIDE_INT size;
7433458d
PT
247
248 type = gfc_typenode_for_spec (&source->ts);
249
b7e75771
JD
250 for (c = gfc_constructor_first (source->value.constructor),
251 cmp = source->ts.u.derived->components;
252 c;
253 c = gfc_constructor_next (c), cmp = cmp->next)
7433458d 254 {
9d99ee7b 255 gcc_assert (cmp);
b7e75771 256 if (!c->expr)
9d99ee7b
PT
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;
51df93ba 260
b7e75771 261 if (c->expr->expr_type == EXPR_NULL)
fd2805e1
TB
262 {
263 size = int_size_in_bytes (TREE_TYPE (cmp->backend_decl));
264 gcc_assert (size >= 0);
265 memset (&buffer[ptr], 0, size);
266 }
51df93ba 267 else
b7e75771 268 gfc_target_encode_expr (c->expr, &buffer[ptr],
51df93ba 269 buffer_size - ptr);
7433458d
PT
270 }
271
fd2805e1
TB
272 size = int_size_in_bytes (type);
273 gcc_assert (size >= 0);
274 return size;
7433458d
PT
275}
276
277
278/* Write a constant expression in binary form to a buffer. */
fd2805e1 279unsigned HOST_WIDE_INT
7433458d
PT
280gfc_target_encode_expr (gfc_expr *source, unsigned char *buffer,
281 size_t buffer_size)
282{
283 if (source == NULL)
284 return 0;
285
286 if (source->expr_type == EXPR_ARRAY)
287 return encode_array (source, buffer, buffer_size);
288
289 gcc_assert (source->expr_type == EXPR_CONSTANT
b7d36ea3
FXC
290 || source->expr_type == EXPR_STRUCTURE
291 || source->expr_type == EXPR_SUBSTRING);
7433458d 292
25812571 293 /* If we already have a target-memory representation, we use that rather
20585ad6
BM
294 than recreating one. */
295 if (source->representation.string)
296 {
297 memcpy (buffer, source->representation.string,
298 source->representation.length);
299 return source->representation.length;
300 }
301
7433458d
PT
302 switch (source->ts.type)
303 {
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:
d0d92baf 311 return encode_complex (source->ts.kind, source->value.complex,
eb6f9a86 312 buffer, buffer_size);
7433458d
PT
313 case BT_LOGICAL:
314 return encode_logical (source->ts.kind, source->value.logical, buffer,
315 buffer_size);
316 case BT_CHARACTER:
b7d36ea3 317 if (source->expr_type == EXPR_CONSTANT || source->ref == NULL)
d393bbd7
FXC
318 return gfc_encode_character (source->ts.kind,
319 source->value.character.length,
320 source->value.character.string,
321 buffer, buffer_size);
b7d36ea3
FXC
322 else
323 {
324 int start, end;
325
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);
d393bbd7
FXC
329 return gfc_encode_character (source->ts.kind, MAX(end - start + 1, 0),
330 &source->value.character.string[start-1],
331 buffer, buffer_size);
b7d36ea3
FXC
332 }
333
7433458d 334 case BT_DERIVED:
cadddfdd
TB
335 if (source->ts.u.derived->ts.f90_type == BT_VOID)
336 {
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);
344 }
345
7433458d
PT
346 return encode_derived (source, buffer, buffer_size);
347 default:
348 gfc_internal_error ("Invalid expression in gfc_target_encode_expr.");
349 return 0;
350 }
351}
352
353
354static int
355interpret_array (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
356{
b7e75771 357 gfc_constructor_base base = NULL;
7433458d
PT
358 int array_size = 1;
359 int i;
360 int ptr = 0;
7433458d
PT
361
362 /* Calculate array size from its shape and rank. */
363 gcc_assert (result->rank > 0 && result->shape);
364
365 for (i = 0; i < result->rank; i++)
366 array_size *= (int)mpz_get_ui (result->shape[i]);
367
368 /* Iterate over array elements, producing constructors. */
369 for (i = 0; i < array_size; i++)
370 {
b7e75771
JD
371 gfc_expr *e = gfc_get_constant_expr (result->ts.type, result->ts.kind,
372 &result->where);
373 e->ts = result->ts;
7433458d 374
b7e75771
JD
375 if (e->ts.type == BT_CHARACTER)
376 e->value.character.length = result->value.character.length;
7433458d 377
b7e75771 378 gfc_constructor_append_expr (&base, e, &result->where);
7433458d 379
86dbed7d
TK
380 ptr += gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr, e,
381 true);
7433458d 382 }
7433458d 383
b7e75771 384 result->value.constructor = base;
7433458d
PT
385 return ptr;
386}
387
388
20585ad6
BM
389int
390gfc_interpret_integer (int kind, unsigned char *buffer, size_t buffer_size,
7433458d
PT
391 mpz_t integer)
392{
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);
398}
399
400
20585ad6
BM
401int
402gfc_interpret_float (int kind, unsigned char *buffer, size_t buffer_size,
ace428e3 403 mpfr_t real)
7433458d 404{
ace428e3 405 gfc_set_model_kind (kind);
7433458d
PT
406 mpfr_init (real);
407 gfc_conv_tree_to_mpfr (real,
408 native_interpret_expr (gfc_get_real_type (kind),
409 buffer, buffer_size));
410
411 return size_float (kind);
412}
413
414
20585ad6
BM
415int
416gfc_interpret_complex (int kind, unsigned char *buffer, size_t buffer_size,
d0d92baf 417 mpc_t complex)
7433458d
PT
418{
419 int size;
eb6f9a86 420 size = gfc_interpret_float (kind, &buffer[0], buffer_size,
d0d92baf 421 mpc_realref (complex));
b7d36ea3 422 size += gfc_interpret_float (kind, &buffer[size], buffer_size - size,
d0d92baf 423 mpc_imagref (complex));
7433458d
PT
424 return size;
425}
426
427
20585ad6
BM
428int
429gfc_interpret_logical (int kind, unsigned char *buffer, size_t buffer_size,
7433458d
PT
430 int *logical)
431{
432 tree t = native_interpret_expr (gfc_get_logical_type (kind), buffer,
433 buffer_size);
807e902e 434 *logical = wi::eq_p (t, 0) ? 0 : 1;
7433458d
PT
435 return size_logical (kind);
436}
437
438
20585ad6 439int
00660189
FXC
440gfc_interpret_character (unsigned char *buffer, size_t buffer_size,
441 gfc_expr *result)
7433458d 442{
00660189
FXC
443 int i;
444
bc21d315 445 if (result->ts.u.cl && result->ts.u.cl->length)
7433458d 446 result->value.character.length =
bc21d315 447 (int) mpz_get_ui (result->ts.u.cl->length->value.integer);
7433458d 448
00660189
FXC
449 gcc_assert (buffer_size >= size_character (result->value.character.length,
450 result->ts.kind));
7433458d 451 result->value.character.string =
00660189
FXC
452 gfc_get_wide_string (result->value.character.length + 1);
453
12ba225d
TB
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
458 {
459 mpz_t integer;
460 unsigned bytes = size_character (1, result->ts.kind);
461 mpz_init (integer);
462 gcc_assert (bytes <= sizeof (unsigned long));
463
464 for (i = 0; i < result->value.character.length; i++)
465 {
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);
471 }
472
473 mpz_clear (integer);
474 }
475
00660189 476 result->value.character.string[result->value.character.length] = '\0';
7433458d
PT
477
478 return result->value.character.length;
479}
480
481
20585ad6
BM
482int
483gfc_interpret_derived (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
7433458d
PT
484{
485 gfc_component *cmp;
7433458d
PT
486 int ptr;
487 tree type;
488
489 /* The attributes of the derived type need to be bolted to the floor. */
490 result->expr_type = EXPR_STRUCTURE;
491
bc21d315 492 cmp = result->ts.u.derived->components;
7433458d 493
b5dca6ea
TB
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))
497 {
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;
25812571 503 e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind, &result->where);
b5dca6ea
TB
504 c = gfc_constructor_append_expr (&result->value.constructor, e, NULL);
505 c->n.component = cmp;
86dbed7d 506 gfc_target_interpret_expr (buffer, buffer_size, e, true);
b5dca6ea
TB
507 e->ts.is_iso_c = 1;
508 return int_size_in_bytes (ptr_type_node);
509 }
510
511 type = gfc_typenode_for_spec (&result->ts);
512
7433458d
PT
513 /* Run through the derived type components. */
514 for (;cmp; cmp = cmp->next)
515 {
b7e75771
JD
516 gfc_constructor *c;
517 gfc_expr *e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind,
25812571 518 &result->where);
b7e75771 519 e->ts = cmp->ts;
7433458d
PT
520
521 /* Copy shape, if needed. */
522 if (cmp->as && cmp->as->rank)
523 {
524 int n;
525
b7e75771
JD
526 e->expr_type = EXPR_ARRAY;
527 e->rank = cmp->as->rank;
7433458d 528
b7e75771
JD
529 e->shape = gfc_get_shape (e->rank);
530 for (n = 0; n < e->rank; n++)
7433458d 531 {
b7e75771
JD
532 mpz_init_set_ui (e->shape[n], 1);
533 mpz_add (e->shape[n], e->shape[n],
7433458d 534 cmp->as->upper[n]->value.integer);
b7e75771 535 mpz_sub (e->shape[n], e->shape[n],
7433458d
PT
536 cmp->as->lower[n]->value.integer);
537 }
538 }
539
b7e75771 540 c = gfc_constructor_append_expr (&result->value.constructor, e, NULL);
7433458d 541
b7e75771
JD
542 /* The constructor points to the component. */
543 c->n.component = cmp;
544
dd5a833e 545 /* Calculate the offset, which consists of the FIELD_OFFSET in
6a227990
TB
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. */
550
b5dca6ea 551 gcc_assert (cmp->backend_decl);
6a227990
TB
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));
555
86dbed7d 556 gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr, e, true);
7433458d 557 }
25812571 558
7433458d
PT
559 return int_size_in_bytes (type);
560}
561
562
563/* Read a binary buffer to a constant expression. */
564int
565gfc_target_interpret_expr (unsigned char *buffer, size_t buffer_size,
86dbed7d 566 gfc_expr *result, bool convert_widechar)
7433458d
PT
567{
568 if (result->expr_type == EXPR_ARRAY)
569 return interpret_array (buffer, buffer_size, result);
570
571 switch (result->ts.type)
572 {
573 case BT_INTEGER:
25812571 574 result->representation.length =
20585ad6
BM
575 gfc_interpret_integer (result->ts.kind, buffer, buffer_size,
576 result->value.integer);
577 break;
578
7433458d 579 case BT_REAL:
25812571 580 result->representation.length =
20585ad6
BM
581 gfc_interpret_float (result->ts.kind, buffer, buffer_size,
582 result->value.real);
583 break;
584
7433458d 585 case BT_COMPLEX:
25812571 586 result->representation.length =
20585ad6 587 gfc_interpret_complex (result->ts.kind, buffer, buffer_size,
d0d92baf 588 result->value.complex);
20585ad6
BM
589 break;
590
7433458d 591 case BT_LOGICAL:
25812571 592 result->representation.length =
20585ad6
BM
593 gfc_interpret_logical (result->ts.kind, buffer, buffer_size,
594 &result->value.logical);
595 break;
596
7433458d 597 case BT_CHARACTER:
25812571 598 result->representation.length =
20585ad6
BM
599 gfc_interpret_character (buffer, buffer_size, result);
600 break;
601
fa1ed658
JW
602 case BT_CLASS:
603 result->ts = CLASS_DATA (result)->ts;
604 /* Fall through. */
7433458d 605 case BT_DERIVED:
25812571 606 result->representation.length =
20585ad6 607 gfc_interpret_derived (buffer, buffer_size, result);
fd2805e1 608 gcc_assert (result->representation.length >= 0);
20585ad6
BM
609 break;
610
7433458d
PT
611 default:
612 gfc_internal_error ("Invalid expression in gfc_target_interpret_expr.");
20585ad6
BM
613 break;
614 }
615
86dbed7d 616 if (result->ts.type == BT_CHARACTER && convert_widechar)
00660189
FXC
617 result->representation.string
618 = gfc_widechar_to_char (result->value.character.string,
619 result->value.character.length);
20585ad6
BM
620 else
621 {
622 result->representation.string =
93acb62c 623 XCNEWVEC (char, result->representation.length + 1);
20585ad6
BM
624 memcpy (result->representation.string, buffer,
625 result->representation.length);
626 result->representation.string[result->representation.length] = '\0';
7433458d 627 }
20585ad6
BM
628
629 return result->representation.length;
7433458d 630}
9d99ee7b
PT
631
632
25812571 633/* --------------------------------------------------------------- */
9d99ee7b
PT
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. */
637
638
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. */
642
643static size_t
644expr_to_char (gfc_expr *e, unsigned char *data, unsigned char *chk, size_t len)
645{
646 int i;
647 int ptr;
b7e75771 648 gfc_constructor *c;
9d99ee7b
PT
649 gfc_component *cmp;
650 unsigned char *buffer;
651
652 if (e == NULL)
653 return 0;
654
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)
658 {
b7e75771
JD
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)
9d99ee7b
PT
662 {
663 gcc_assert (cmp && cmp->backend_decl);
b7e75771 664 if (!c->expr)
9d99ee7b 665 continue;
21c0a521
DM
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;
b7e75771 668 expr_to_char (c->expr, &data[ptr], &chk[ptr], len);
9d99ee7b
PT
669 }
670 return len;
671 }
672
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);
678
679 for (i = 0; i < (int)len; i++)
680 {
681 if (chk[i] && (buffer[i] != data[i]))
682 {
683 gfc_error ("Overlapping unequal initializers in EQUIVALENCE "
684 "at %L", &e->where);
685 return 0;
686 }
687 chk[i] = 0xFF;
688 }
689
690 memcpy (data, buffer, len);
691 return len;
692}
693
694
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. */
698
699size_t
700gfc_merge_initializers (gfc_typespec ts, gfc_expr *e, unsigned char *data,
701 unsigned char *chk, size_t length)
702{
703 size_t len = 0;
704 gfc_constructor * c;
705
706 switch (e->expr_type)
707 {
708 case EXPR_CONSTANT:
709 case EXPR_STRUCTURE:
710 len = expr_to_char (e, &data[0], &chk[0], length);
711
712 break;
713
714 case EXPR_ARRAY:
b7e75771
JD
715 for (c = gfc_constructor_first (e->value.constructor);
716 c; c = gfc_constructor_next (c))
9d99ee7b
PT
717 {
718 size_t elt_size = gfc_target_expr_size (c->expr);
719
fd2805e1 720 if (mpz_cmp_si (c->offset, 0) != 0)
b7e75771 721 len = elt_size * (size_t)mpz_get_si (c->offset);
9d99ee7b
PT
722
723 len = len + gfc_merge_initializers (ts, c->expr, &data[len],
724 &chk[len], length - len);
725 }
726 break;
727
728 default:
729 return 0;
730 }
731
732 return len;
733}
00a4618b 734
c7abc45c
TB
735
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. */
738
739bool
00a4618b
TB
740gfc_convert_boz (gfc_expr *expr, gfc_typespec *ts)
741{
c7abc45c
TB
742 size_t buffer_size, boz_bit_size, ts_bit_size;
743 int index;
00a4618b
TB
744 unsigned char *buffer;
745
746 if (!expr->is_boz)
c7abc45c 747 return true;
00a4618b
TB
748
749 gcc_assert (expr->expr_type == EXPR_CONSTANT
750 && expr->ts.type == BT_INTEGER);
751
752 /* Don't convert BOZ to logical, character, derived etc. */
753 if (ts->type == BT_REAL)
c7abc45c
TB
754 {
755 buffer_size = size_float (ts->kind);
756 ts_bit_size = buffer_size * 8;
757 }
00a4618b 758 else if (ts->type == BT_COMPLEX)
c7abc45c
TB
759 {
760 buffer_size = size_complex (ts->kind);
761 ts_bit_size = buffer_size * 8 / 2;
762 }
00a4618b 763 else
c7abc45c
TB
764 return true;
765
766 /* Convert BOZ to the smallest possible integer kind. */
767 boz_bit_size = mpz_sizeinbase (expr->value.integer, 2);
00a4618b 768
c7abc45c
TB
769 if (boz_bit_size > ts_bit_size)
770 {
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;
774 }
775
776 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
ace428e3
DK
777 if ((unsigned) gfc_integer_kinds[index].bit_size >= ts_bit_size)
778 break;
c7abc45c
TB
779
780 expr->ts.kind = gfc_integer_kinds[index].kind;
00a4618b
TB
781 buffer_size = MAX (buffer_size, size_integer (expr->ts.kind));
782
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);
786
787 if (ts->type == BT_REAL)
788 {
789 mpfr_init (expr->value.real);
790 gfc_interpret_float (ts->kind, buffer, buffer_size, expr->value.real);
791 }
792 else
793 {
eb6f9a86 794 mpc_init2 (expr->value.complex, mpfr_get_default_prec());
00a4618b 795 gfc_interpret_complex (ts->kind, buffer, buffer_size,
d0d92baf 796 expr->value.complex);
00a4618b 797 }
25812571 798 expr->is_boz = 0;
00a4618b
TB
799 expr->ts.type = ts->type;
800 expr->ts.kind = ts->kind;
c7abc45c
TB
801
802 return true;
00a4618b 803}