2011-08-31 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / fortran / target-memory.c
blob63878959b47e39fd4b1e2117eb59d556ce6ec96f
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
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "machmode.h"
26 #include "tree.h"
27 #include "gfortran.h"
28 #include "arith.h"
29 #include "constructor.h"
30 #include "trans.h"
31 #include "trans-const.h"
32 #include "trans-types.h"
33 #include "target-memory.h"
35 /* --------------------------------------------------------------- */
36 /* Calculate the size of an expression. */
38 static size_t
39 size_array (gfc_expr *e)
41 mpz_t array_size;
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;
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 size_t
86 gfc_target_expr_size (gfc_expr *e)
88 tree type;
90 gcc_assert (e != NULL);
92 if (e->expr_type == EXPR_ARRAY)
93 return size_array (e);
95 switch (e->ts.type)
97 case BT_INTEGER:
98 return size_integer (e->ts.kind);
99 case BT_REAL:
100 return size_float (e->ts.kind);
101 case BT_COMPLEX:
102 return size_complex (e->ts.kind);
103 case BT_LOGICAL:
104 return size_logical (e->ts.kind);
105 case BT_CHARACTER:
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)
112 int length;
114 gfc_extract_int (e->ts.u.cl->length, &length);
115 return size_character (length, e->ts.kind);
117 else
118 return 0;
120 case BT_HOLLERITH:
121 return e->representation.length;
122 case BT_DERIVED:
124 /* Determine type size without clobbering the typespec for ISO C
125 binding types. */
126 gfc_typespec ts;
127 ts = e->ts;
128 type = gfc_typenode_for_spec (&ts);
129 return int_size_in_bytes (type);
131 default:
132 gfc_internal_error ("Invalid expression in gfc_target_expr_size.");
133 return 0;
138 /* The encode_* functions export a value into a buffer, and
139 return the number of bytes of the buffer that have been
140 used. */
142 static int
143 encode_array (gfc_expr *expr, unsigned char *buffer, size_t buffer_size)
145 mpz_t array_size;
146 int i;
147 int ptr = 0;
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);
159 return ptr;
163 static int
164 encode_integer (int kind, mpz_t integer, unsigned char *buffer,
165 size_t buffer_size)
167 return native_encode_expr (gfc_conv_mpz_to_tree (integer, kind),
168 buffer, buffer_size);
172 static int
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,
176 buffer_size);
180 static int
181 encode_complex (int kind, mpc_t cmplx,
182 unsigned char *buffer, size_t buffer_size)
184 int 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);
188 return size;
192 static int
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),
196 logical),
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);
207 int i;
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],
213 elsize);
215 return length;
219 static int
220 encode_derived (gfc_expr *source, unsigned char *buffer, size_t buffer_size)
222 gfc_constructor *c;
223 gfc_component *cmp;
224 int ptr;
225 tree type;
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)
234 gcc_assert (cmp);
235 if (!c->expr)
236 continue;
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)));
243 else
244 gfc_target_encode_expr (c->expr, &buffer[ptr],
245 buffer_size - 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,
255 size_t buffer_size)
257 if (source == NULL)
258 return 0;
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)
278 case BT_INTEGER:
279 return encode_integer (source->ts.kind, source->value.integer, buffer,
280 buffer_size);
281 case BT_REAL:
282 return encode_float (source->ts.kind, source->value.real, buffer,
283 buffer_size);
284 case BT_COMPLEX:
285 return encode_complex (source->ts.kind, source->value.complex,
286 buffer, buffer_size);
287 case BT_LOGICAL:
288 return encode_logical (source->ts.kind, source->value.logical, buffer,
289 buffer_size);
290 case BT_CHARACTER:
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);
296 else
298 int start, end;
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);
308 case BT_DERIVED:
309 return encode_derived (source, buffer, buffer_size);
310 default:
311 gfc_internal_error ("Invalid expression in gfc_target_encode_expr.");
312 return 0;
317 static int
318 interpret_array (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
320 gfc_constructor_base base = NULL;
321 int array_size = 1;
322 int i;
323 int ptr = 0;
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,
335 &result->where);
336 e->ts = result->ts;
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,
344 true);
347 result->value.constructor = base;
348 return ptr;
353 gfc_interpret_integer (int kind, unsigned char *buffer, size_t buffer_size,
354 mpz_t integer)
356 mpz_init (integer);
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,
366 mpfr_t real)
368 gfc_set_model_kind (kind);
369 mpfr_init (real);
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,
380 mpc_t complex)
382 int 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));
387 return size;
392 gfc_interpret_logical (int kind, unsigned char *buffer, size_t buffer_size,
393 int *logical)
395 tree t = native_interpret_expr (gfc_get_logical_type (kind), buffer,
396 buffer_size);
397 *logical = double_int_zero_p (tree_to_double_int (t))
398 ? 0 : 1;
399 return size_logical (kind);
404 gfc_interpret_character (unsigned char *buffer, size_t buffer_size,
405 gfc_expr *result)
407 int i;
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,
414 result->ts.kind));
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];
421 else
423 mpz_t integer;
424 unsigned bytes = size_character (1, result->ts.kind);
425 mpz_init (integer);
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);
437 mpz_clear (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)
449 gfc_component *cmp;
450 int ptr;
451 tree type;
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))
462 gfc_constructor *c;
463 gfc_expr *e;
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);
471 e->ts.is_iso_c = 1;
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)
480 gfc_constructor *c;
481 gfc_expr *e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind,
482 &result->where);
483 e->ts = cmp->ts;
485 /* Copy shape, if needed. */
486 if (cmp->as && cmp->as->rank)
488 int n;
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)
537 case BT_INTEGER:
538 result->representation.length =
539 gfc_interpret_integer (result->ts.kind, buffer, buffer_size,
540 result->value.integer);
541 break;
543 case BT_REAL:
544 result->representation.length =
545 gfc_interpret_float (result->ts.kind, buffer, buffer_size,
546 result->value.real);
547 break;
549 case BT_COMPLEX:
550 result->representation.length =
551 gfc_interpret_complex (result->ts.kind, buffer, buffer_size,
552 result->value.complex);
553 break;
555 case BT_LOGICAL:
556 result->representation.length =
557 gfc_interpret_logical (result->ts.kind, buffer, buffer_size,
558 &result->value.logical);
559 break;
561 case BT_CHARACTER:
562 result->representation.length =
563 gfc_interpret_character (buffer, buffer_size, result);
564 break;
566 case BT_DERIVED:
567 result->representation.length =
568 gfc_interpret_derived (buffer, buffer_size, result);
569 break;
571 default:
572 gfc_internal_error ("Invalid expression in gfc_target_interpret_expr.");
573 break;
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);
580 else
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
601 error. */
603 static size_t
604 expr_to_char (gfc_expr *e, unsigned char *data, unsigned char *chk, size_t len)
606 int i;
607 int ptr;
608 gfc_constructor *c;
609 gfc_component *cmp;
610 unsigned char *buffer;
612 if (e == NULL)
613 return 0;
615 /* Take a derived type, one component at a time, using the offsets from the backend
616 declaration. */
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);
624 if (!c->expr)
625 continue;
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);
630 return 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 "
644 "at %L", &e->where);
645 return 0;
647 chk[i] = 0xFF;
650 memcpy (data, buffer, len);
651 return 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. */
659 size_t
660 gfc_merge_initializers (gfc_typespec ts, gfc_expr *e, unsigned char *data,
661 unsigned char *chk, size_t length)
663 size_t len = 0;
664 gfc_constructor * c;
666 switch (e->expr_type)
668 case EXPR_CONSTANT:
669 case EXPR_STRUCTURE:
670 len = expr_to_char (e, &data[0], &chk[0], length);
672 break;
674 case EXPR_ARRAY:
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);
680 if (c->offset)
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);
686 break;
688 default:
689 return 0;
692 return 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. */
699 bool
700 gfc_convert_boz (gfc_expr *expr, gfc_typespec *ts)
702 size_t buffer_size, boz_bit_size, ts_bit_size;
703 int index;
704 unsigned char *buffer;
706 if (!expr->is_boz)
707 return true;
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;
723 else
724 return true;
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);
733 return false;
736 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
737 if ((unsigned) gfc_integer_kinds[index].bit_size >= ts_bit_size)
738 break;
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);
752 else
754 mpc_init2 (expr->value.complex, mpfr_get_default_prec());
755 gfc_interpret_complex (ts->kind, buffer, buffer_size,
756 expr->value.complex);
758 expr->is_boz = 0;
759 expr->ts.type = ts->type;
760 expr->ts.kind = ts->kind;
762 return true;