LCOV - code coverage report
Current view: top level - gcc/fortran - target-memory.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.3 % 428 408
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 27 27
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Simulate storage of variables into target memory.
       2              :    Copyright (C) 2007-2026 Free Software Foundation, Inc.
       3              :    Contributed by Paul Thomas and Brooks Moses
       4              : 
       5              : This file is part of GCC.
       6              : 
       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.
      11              : 
      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.
      16              : 
      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/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "tree.h"
      25              : #include "gfortran.h"
      26              : #include "trans.h"
      27              : #include "fold-const.h"
      28              : #include "stor-layout.h"
      29              : #include "arith.h"
      30              : #include "constructor.h"
      31              : #include "trans-const.h"
      32              : #include "trans-types.h"
      33              : #include "target-memory.h"
      34              : 
      35              : /* --------------------------------------------------------------- */
      36              : /* Calculate the size of an expression.  */
      37              : 
      38              : 
      39              : static size_t
      40         4237 : size_integer (int kind)
      41              : {
      42         4237 :   return GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (gfc_get_int_type (kind)));
      43              : }
      44              : 
      45              : static size_t
      46            6 : size_unsigned (int kind)
      47              : {
      48            6 :   return GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (gfc_get_unsigned_type (kind)));
      49              : }
      50              : 
      51              : static size_t
      52         3926 : size_float (int kind)
      53              : {
      54         3926 :   return GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (gfc_get_real_type (kind)));
      55              : }
      56              : 
      57              : 
      58              : static size_t
      59          726 : size_complex (int kind)
      60              : {
      61            0 :   return 2 * size_float (kind);
      62              : }
      63              : 
      64              : 
      65              : static size_t
      66          953 : size_logical (int kind)
      67              : {
      68          953 :   return GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (gfc_get_logical_type (kind)));
      69              : }
      70              : 
      71              : 
      72              : static size_t
      73       306384 : size_character (gfc_charlen_t length, int kind)
      74              : {
      75       306384 :   int i = gfc_validate_kind (BT_CHARACTER, kind, false);
      76       306384 :   return length * gfc_character_kinds[i].bit_size / 8;
      77              : }
      78              : 
      79              : 
      80              : /* Return the size of a single element of the given expression.
      81              :    Equivalent to gfc_target_expr_size for scalars.  */
      82              : 
      83              : bool
      84         6599 : gfc_element_size (gfc_expr *e, size_t *siz)
      85              : {
      86         6599 :   tree type;
      87              : 
      88         6599 :   switch (e->ts.type)
      89              :     {
      90         2619 :     case BT_INTEGER:
      91         2619 :       *siz = size_integer (e->ts.kind);
      92         2619 :       return true;
      93            6 :     case BT_UNSIGNED:
      94            6 :       *siz = size_unsigned (e->ts.kind);
      95            6 :       return true;
      96         1079 :     case BT_REAL:
      97         1079 :       *siz = size_float (e->ts.kind);
      98         1079 :       return true;
      99          726 :     case BT_COMPLEX:
     100          726 :       *siz = size_complex (e->ts.kind);
     101          726 :       return true;
     102          512 :     case BT_LOGICAL:
     103          512 :       *siz = size_logical (e->ts.kind);
     104          512 :       return true;
     105         1294 :     case BT_CHARACTER:
     106         1294 :       if (e->expr_type == EXPR_CONSTANT)
     107          751 :         *siz = size_character (e->value.character.length, e->ts.kind);
     108          543 :       else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
     109          530 :                && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
     110          529 :                && e->ts.u.cl->length->ts.type == BT_INTEGER)
     111              :         {
     112          529 :           HOST_WIDE_INT length;
     113              : 
     114          529 :           gfc_extract_hwi (e->ts.u.cl->length, &length);
     115          529 :           *siz = size_character (length, e->ts.kind);
     116          529 :         }
     117              :       else
     118              :         {
     119           14 :           *siz = 0;
     120           14 :           return false;
     121              :         }
     122              :       return true;
     123              : 
     124            7 :     case BT_HOLLERITH:
     125            7 :       *siz = e->representation.length;
     126            7 :       return true;
     127          356 :     case BT_DERIVED:
     128          356 :     case BT_CLASS:
     129          356 :     case BT_VOID:
     130          356 :     case BT_ASSUMED:
     131          356 :     case BT_PROCEDURE:
     132          356 :       {
     133              :         /* Determine type size without clobbering the typespec for ISO C
     134              :            binding types.  */
     135          356 :         gfc_typespec ts;
     136          356 :         HOST_WIDE_INT size;
     137          356 :         ts = e->ts;
     138          356 :         type = gfc_typenode_for_spec (&ts);
     139          356 :         size = int_size_in_bytes (type);
     140          356 :         gcc_assert (size >= 0);
     141          356 :         *siz = size;
     142              :       }
     143          356 :       return true;
     144            0 :     default:
     145            0 :       gfc_internal_error ("Invalid expression in gfc_element_size.");
     146              :       *siz = 0;
     147              :       return false;
     148              :     }
     149              : }
     150              : 
     151              : 
     152              : /* Return the size of an expression in its target representation.  */
     153              : 
     154              : bool
     155         4221 : gfc_target_expr_size (gfc_expr *e, size_t *size)
     156              : {
     157         4221 :   mpz_t tmp;
     158         4221 :   size_t asz, el_size;
     159              : 
     160         4221 :   gcc_assert (e != NULL);
     161              : 
     162         4221 :   *size = 0;
     163         4221 :   if (e->rank)
     164              :     {
     165          434 :       if (gfc_array_size (e, &tmp))
     166          434 :         asz = mpz_get_ui (tmp);
     167              :       else
     168              :         return false;
     169              : 
     170          434 :       mpz_clear (tmp);
     171              :     }
     172              :   else
     173              :     asz = 1;
     174              : 
     175         4221 :   if (!gfc_element_size (e, &el_size))
     176              :     return false;
     177         4220 :   *size = asz * el_size;
     178         4220 :   return true;
     179              : }
     180              : 
     181              : 
     182              : /* The encode_* functions export a value into a buffer, and
     183              :    return the number of bytes of the buffer that have been
     184              :    used.  */
     185              : 
     186              : static unsigned HOST_WIDE_INT
     187          451 : encode_array (gfc_expr *expr, unsigned char *buffer, size_t buffer_size)
     188              : {
     189          451 :   mpz_t array_size;
     190          451 :   int i;
     191          451 :   int ptr = 0;
     192              : 
     193          451 :   gfc_constructor_base ctor = expr->value.constructor;
     194              : 
     195          451 :   gfc_array_size (expr, &array_size);
     196         1897 :   for (i = 0; i < (int)mpz_get_ui (array_size); i++)
     197              :     {
     198          983 :       ptr += gfc_target_encode_expr (gfc_constructor_lookup_expr (ctor, i),
     199          983 :                                      &buffer[ptr], buffer_size - ptr);
     200              :     }
     201              : 
     202          451 :   mpz_clear (array_size);
     203          451 :   return ptr;
     204              : }
     205              : 
     206              : 
     207              : static int
     208          708 : encode_integer (int kind, mpz_t integer, unsigned char *buffer,
     209              :                 size_t buffer_size)
     210              : {
     211          708 :   return native_encode_expr (gfc_conv_mpz_to_tree (integer, kind),
     212          708 :                              buffer, buffer_size);
     213              : }
     214              : 
     215              : 
     216              : static int
     217           99 : encode_float (int kind, mpfr_t real, unsigned char *buffer, size_t buffer_size)
     218              : {
     219           99 :   return native_encode_expr (gfc_conv_mpfr_to_tree (real, kind, 0), buffer,
     220           99 :                              buffer_size);
     221              : }
     222              : 
     223              : 
     224              : static int
     225           18 : encode_complex (int kind, mpc_t cmplx,
     226              :                 unsigned char *buffer, size_t buffer_size)
     227              : {
     228           18 :   int size;
     229           18 :   size = encode_float (kind, mpc_realref (cmplx), &buffer[0], buffer_size);
     230           36 :   size += encode_float (kind, mpc_imagref (cmplx),
     231           18 :                         &buffer[size], buffer_size - size);
     232           18 :   return size;
     233              : }
     234              : 
     235              : 
     236              : static int
     237           23 : encode_logical (int kind, int logical, unsigned char *buffer, size_t buffer_size)
     238              : {
     239           23 :   return native_encode_expr (build_int_cst (gfc_get_logical_type (kind),
     240           23 :                                             logical),
     241           23 :                              buffer, buffer_size);
     242              : }
     243              : 
     244              : 
     245              : size_t
     246       151309 : gfc_encode_character (int kind, size_t length, const gfc_char_t *string,
     247              :                       unsigned char *buffer, size_t buffer_size)
     248              : {
     249       151309 :   size_t elsize = size_character (1, kind);
     250       151309 :   tree type = gfc_get_char_type (kind);
     251              : 
     252       151309 :   gcc_assert (buffer_size >= size_character (length, kind));
     253              : 
     254      2140915 :   for (size_t i = 0; i < length; i++)
     255      1989606 :     native_encode_expr (build_int_cst (type, string[i]), &buffer[i*elsize],
     256              :                         elsize);
     257              : 
     258       151309 :   return length;
     259              : }
     260              : 
     261              : 
     262              : static unsigned HOST_WIDE_INT
     263          172 : encode_derived (gfc_expr *source, unsigned char *buffer, size_t buffer_size)
     264              : {
     265          172 :   gfc_constructor *c;
     266          172 :   gfc_component *cmp;
     267          172 :   int ptr;
     268          172 :   tree type;
     269          172 :   HOST_WIDE_INT size;
     270              : 
     271          172 :   type = gfc_typenode_for_spec (&source->ts);
     272              : 
     273          172 :   for (c = gfc_constructor_first (source->value.constructor),
     274          172 :        cmp = source->ts.u.derived->components;
     275          399 :        c;
     276          227 :        c = gfc_constructor_next (c), cmp = cmp->next)
     277              :     {
     278          227 :       gcc_assert (cmp);
     279          227 :       if (!c->expr)
     280            0 :         continue;
     281          227 :       ptr = TREE_INT_CST_LOW(DECL_FIELD_OFFSET(cmp->backend_decl))
     282          227 :             + TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(cmp->backend_decl))/8;
     283              : 
     284          227 :       if (c->expr->expr_type == EXPR_NULL)
     285              :         {
     286            1 :           size = int_size_in_bytes (TREE_TYPE (cmp->backend_decl));
     287            1 :           gcc_assert (size >= 0);
     288            1 :           memset (&buffer[ptr], 0, size);
     289              :         }
     290          226 :       else if (c->expr->rank == 0 && cmp->as && cmp->as->rank)
     291              :         {
     292              :           /* Replicate a scalar constant initializer for an array component as
     293              :              many times as needed.  */
     294          144 :           gfc_constructor_base ctor_head = NULL;
     295          144 :           gfc_array_spec *as = cmp->as;
     296          144 :           gfc_typespec *ts = &c->expr->ts;
     297          144 :           HOST_WIDE_INT ctor_size;
     298          144 :           mpz_t as_size, cons_size;
     299          144 :           gfc_expr *expr;
     300              : 
     301          144 :           spec_size (as, &as_size);
     302          144 :           ctor_size = gfc_mpz_get_hwi (as_size);
     303          612 :           for (HOST_WIDE_INT k = 0; k < ctor_size; k++)
     304          324 :             gfc_constructor_append_expr (&ctor_head, gfc_copy_expr (c->expr),
     305              :                                          &gfc_current_locus);
     306              : 
     307              :           /* Set up expr as an array constructor.  */
     308          144 :           expr = gfc_get_array_expr (ts->type, ts->kind, &c->expr->where);
     309          144 :           expr->value.constructor = ctor_head;
     310          144 :           expr->rank = as->rank;
     311          144 :           expr->shape = gfc_get_shape (as->rank);
     312          288 :           for (int i = 0; i < as->rank; ++i)
     313          144 :             spec_dimen_size (as, i, &expr->shape[i]);
     314              : 
     315              :           /* Validate size of array initializer.  */
     316          144 :           if (!gfc_array_size (expr, &cons_size)
     317          144 :               || mpz_cmp (cons_size, as_size))
     318            0 :             gfc_error ("Bad size of array initializer at %C");
     319          144 :           mpz_clear (cons_size);
     320          144 :           mpz_clear (as_size);
     321              : 
     322          144 :           gfc_target_encode_expr (expr, &buffer[ptr],
     323          144 :                                   buffer_size - ptr);
     324          144 :           gfc_free_expr (expr);
     325          144 :         }
     326              :       else
     327           82 :         gfc_target_encode_expr (c->expr, &buffer[ptr],
     328           82 :                                 buffer_size - ptr);
     329              :     }
     330              : 
     331          172 :   size = int_size_in_bytes (type);
     332          172 :   gcc_assert (size >= 0);
     333          172 :   return size;
     334              : }
     335              : 
     336              : 
     337              : /* Write a constant expression in binary form to a buffer.  */
     338              : unsigned HOST_WIDE_INT
     339         2341 : gfc_target_encode_expr (gfc_expr *source, unsigned char *buffer,
     340              :                         size_t buffer_size)
     341              : {
     342         2341 :   if (source == NULL)
     343              :     return 0;
     344              : 
     345         2341 :   if (source->expr_type == EXPR_ARRAY)
     346          451 :     return encode_array (source, buffer, buffer_size);
     347              : 
     348         1890 :   gcc_assert (source->expr_type == EXPR_CONSTANT
     349              :               || source->expr_type == EXPR_STRUCTURE
     350              :               || source->expr_type == EXPR_SUBSTRING);
     351              : 
     352              :   /* If we already have a target-memory representation, we use that rather
     353              :      than recreating one.  */
     354         1890 :   if (source->representation.string)
     355              :     {
     356          139 :       memcpy (buffer, source->representation.string,
     357          139 :               source->representation.length);
     358          139 :       return source->representation.length;
     359              :     }
     360              : 
     361         1751 :   switch (source->ts.type)
     362              :     {
     363          451 :     case BT_INTEGER:
     364          451 :       return encode_integer (source->ts.kind, source->value.integer, buffer,
     365          451 :                              buffer_size);
     366           63 :     case BT_REAL:
     367           63 :       return encode_float (source->ts.kind, source->value.real, buffer,
     368           63 :                            buffer_size);
     369           18 :     case BT_COMPLEX:
     370           18 :       return encode_complex (source->ts.kind, source->value.complex,
     371           18 :                              buffer, buffer_size);
     372           23 :     case BT_LOGICAL:
     373           23 :       return encode_logical (source->ts.kind, source->value.logical, buffer,
     374           23 :                              buffer_size);
     375         1021 :     case BT_CHARACTER:
     376         1021 :       if (source->expr_type == EXPR_CONSTANT || source->ref == NULL)
     377          991 :         return gfc_encode_character (source->ts.kind,
     378          991 :                                      source->value.character.length,
     379          991 :                                      source->value.character.string,
     380          991 :                                      buffer, buffer_size);
     381              :       else
     382              :         {
     383           30 :           HOST_WIDE_INT start, end;
     384              : 
     385           30 :           gcc_assert (source->expr_type == EXPR_SUBSTRING);
     386           30 :           gfc_extract_hwi (source->ref->u.ss.start, &start);
     387           30 :           gfc_extract_hwi (source->ref->u.ss.end, &end);
     388           30 :           return gfc_encode_character (source->ts.kind, MAX(end - start + 1, 0),
     389           30 :                                        &source->value.character.string[start-1],
     390              :                                        buffer, buffer_size);
     391              :         }
     392              : 
     393          175 :     case BT_DERIVED:
     394          175 :       if (source->ts.u.derived->ts.f90_type == BT_VOID)
     395              :         {
     396            3 :           gfc_constructor *c;
     397            3 :           gcc_assert (source->expr_type == EXPR_STRUCTURE);
     398            3 :           c = gfc_constructor_first (source->value.constructor);
     399            3 :           gcc_assert (c->expr->expr_type == EXPR_CONSTANT
     400              :                       && c->expr->ts.type == BT_INTEGER);
     401            3 :           return encode_integer (gfc_index_integer_kind, c->expr->value.integer,
     402            3 :                                  buffer, buffer_size);
     403              :         }
     404              : 
     405          172 :       return encode_derived (source, buffer, buffer_size);
     406            0 :     default:
     407            0 :       gfc_internal_error ("Invalid expression in gfc_target_encode_expr.");
     408              :       return 0;
     409              :     }
     410              : }
     411              : 
     412              : 
     413              : static size_t
     414          307 : interpret_array (unsigned char *buffer, size_t buffer_size, gfc_expr *result,
     415              :                  bool convert_widechar)
     416              : {
     417          307 :   gfc_constructor_base base = NULL;
     418          307 :   size_t array_size = 1;
     419          307 :   size_t ptr = 0;
     420              : 
     421              :   /* Calculate array size from its shape and rank.  */
     422          307 :   gcc_assert (result->rank > 0 && result->shape);
     423              : 
     424          614 :   for (int i = 0; i < result->rank; i++)
     425          342 :     array_size *= mpz_get_ui (result->shape[i]);
     426              : 
     427              :   /* Iterate over array elements, producing constructors.  */
     428         1399 :   for (size_t i = 0; i < array_size; i++)
     429              :     {
     430         1092 :       gfc_expr *e = gfc_get_constant_expr (result->ts.type, result->ts.kind,
     431              :                                            &result->where);
     432         1092 :       e->ts = result->ts;
     433              : 
     434         1092 :       if (e->ts.type == BT_CHARACTER)
     435          754 :         e->value.character.length = result->value.character.length;
     436              : 
     437         1092 :       gfc_constructor_append_expr (&base, e, &result->where);
     438              : 
     439         1092 :       ptr += gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr, e,
     440              :                                         convert_widechar);
     441              :     }
     442              : 
     443          307 :   result->value.constructor = base;
     444          307 :   return ptr;
     445              : }
     446              : 
     447              : 
     448              : int
     449         1110 : gfc_interpret_integer (int kind, unsigned char *buffer, size_t buffer_size,
     450              :                    mpz_t integer)
     451              : {
     452         1110 :   mpz_init (integer);
     453         1110 :   gfc_conv_tree_to_mpz (integer,
     454              :                         native_interpret_expr (gfc_get_int_type (kind),
     455              :                                                buffer, buffer_size));
     456         1110 :   return size_integer (kind);
     457              : }
     458              : 
     459              : 
     460              : int
     461         1867 : gfc_interpret_float (int kind, unsigned char *buffer, size_t buffer_size,
     462              :                      mpfr_t real)
     463              : {
     464         1867 :   gfc_set_model_kind (kind);
     465              : 
     466         1867 :   tree source = native_interpret_expr (gfc_get_real_type (kind), buffer,
     467              :                                        buffer_size);
     468         1867 :   if (!source)
     469              :     return 0;
     470              : 
     471         1867 :   mpfr_init (real);
     472         1867 :   gfc_conv_tree_to_mpfr (real, source);
     473         1867 :   return size_float (kind);
     474              : }
     475              : 
     476              : 
     477              : int
     478          494 : gfc_interpret_complex (int kind, unsigned char *buffer, size_t buffer_size,
     479              :                        mpc_t complex)
     480              : {
     481          494 :   int size;
     482          988 :   size = gfc_interpret_float (kind, &buffer[0], buffer_size,
     483          494 :                               mpc_realref (complex));
     484          988 :   size += gfc_interpret_float (kind, &buffer[size], buffer_size - size,
     485          494 :                                mpc_imagref (complex));
     486          494 :   return size;
     487              : }
     488              : 
     489              : 
     490              : int
     491          441 : gfc_interpret_logical (int kind, unsigned char *buffer, size_t buffer_size,
     492              :                    int *logical)
     493              : {
     494          441 :   tree t = native_interpret_expr (gfc_get_logical_type (kind), buffer,
     495              :                                   buffer_size);
     496          441 :   *logical = wi::to_wide (t) == 0 ? 0 : 1;
     497          441 :   return size_logical (kind);
     498              : }
     499              : 
     500              : 
     501              : size_t
     502         1015 : gfc_interpret_character (unsigned char *buffer, size_t buffer_size,
     503              :                          gfc_expr *result)
     504              : {
     505         1015 :   if (result->ts.u.cl && result->ts.u.cl->length)
     506         1015 :     result->value.character.length =
     507         1015 :       gfc_mpz_get_hwi (result->ts.u.cl->length->value.integer);
     508              : 
     509         1015 :   gcc_assert (buffer_size >= size_character (result->value.character.length,
     510              :                                              result->ts.kind));
     511         2030 :   result->value.character.string =
     512         1015 :     gfc_get_wide_string (result->value.character.length + 1);
     513              : 
     514         1015 :   if (result->ts.kind == gfc_default_character_kind)
     515         2134 :     for (size_t i = 0; i < (size_t) result->value.character.length; i++)
     516         1575 :       result->value.character.string[i] = (gfc_char_t) buffer[i];
     517              :   else
     518              :     {
     519          456 :       mpz_t integer;
     520          456 :       size_t bytes = size_character (1, result->ts.kind);
     521          456 :       mpz_init (integer);
     522          456 :       gcc_assert (bytes <= sizeof (unsigned long));
     523              : 
     524         1370 :       for (size_t i = 0; i < (size_t) result->value.character.length; i++)
     525              :         {
     526          914 :           gfc_conv_tree_to_mpz (integer,
     527              :             native_interpret_expr (gfc_get_char_type (result->ts.kind),
     528          914 :                                    &buffer[bytes*i], buffer_size-bytes*i));
     529          914 :           result->value.character.string[i]
     530          914 :             = (gfc_char_t) mpz_get_ui (integer);
     531              :         }
     532              : 
     533          456 :       mpz_clear (integer);
     534              :     }
     535              : 
     536         1015 :   result->value.character.string[result->value.character.length] = '\0';
     537              : 
     538         1015 :   return size_character (result->value.character.length, result->ts.kind);
     539              : }
     540              : 
     541              : 
     542              : int
     543           35 : gfc_interpret_derived (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
     544              : {
     545           35 :   gfc_component *cmp;
     546           35 :   int ptr;
     547           35 :   tree type;
     548              : 
     549              :   /* The attributes of the derived type need to be bolted to the floor.  */
     550           35 :   result->expr_type = EXPR_STRUCTURE;
     551              : 
     552           35 :   cmp = result->ts.u.derived->components;
     553              : 
     554           35 :   if (result->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
     555           15 :       && (result->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
     556            1 :           || result->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
     557              :     {
     558           15 :       gfc_constructor *c;
     559           15 :       gfc_expr *e;
     560              :       /* Needed as gfc_typenode_for_spec as gfc_typenode_for_spec
     561              :          sets this to BT_INTEGER.  */
     562           15 :       result->ts.type = BT_DERIVED;
     563           15 :       e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind, &result->where);
     564           15 :       c = gfc_constructor_append_expr (&result->value.constructor, e, NULL);
     565           15 :       c->n.component = cmp;
     566           15 :       gfc_target_interpret_expr (buffer, buffer_size, e, true);
     567           15 :       e->ts.is_iso_c = 1;
     568           15 :       return int_size_in_bytes (ptr_type_node);
     569              :     }
     570              : 
     571           20 :   type = gfc_typenode_for_spec (&result->ts);
     572              : 
     573              :   /* Run through the derived type components.  */
     574           85 :   for (;cmp; cmp = cmp->next)
     575              :     {
     576           51 :       gfc_constructor *c;
     577           51 :       gfc_expr *e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind,
     578              :                                            &result->where);
     579           51 :       e->ts = cmp->ts;
     580              : 
     581              :       /* Copy shape, if needed.  */
     582           51 :       if (cmp->as && cmp->as->rank)
     583              :         {
     584            9 :           int n;
     585              : 
     586            9 :           if (cmp->as->type != AS_EXPLICIT)
     587              :             return 0;
     588              : 
     589            3 :           e->expr_type = EXPR_ARRAY;
     590            3 :           e->rank = cmp->as->rank;
     591              : 
     592            3 :           e->shape = gfc_get_shape (e->rank);
     593            6 :           for (n = 0; n < e->rank; n++)
     594              :              {
     595            3 :                mpz_init_set_ui (e->shape[n], 1);
     596            3 :                mpz_add (e->shape[n], e->shape[n],
     597            3 :                         cmp->as->upper[n]->value.integer);
     598            3 :                mpz_sub (e->shape[n], e->shape[n],
     599            3 :                         cmp->as->lower[n]->value.integer);
     600              :              }
     601              :         }
     602              : 
     603           45 :       c = gfc_constructor_append_expr (&result->value.constructor, e, NULL);
     604              : 
     605              :       /* The constructor points to the component.  */
     606           45 :       c->n.component = cmp;
     607              : 
     608              :       /* Calculate the offset, which consists of the FIELD_OFFSET in
     609              :          bytes, which appears in multiples of DECL_OFFSET_ALIGN-bit-sized,
     610              :          and additional bits of FIELD_BIT_OFFSET. The code assumes that all
     611              :          sizes of the components are multiples of BITS_PER_UNIT,
     612              :          i.e. there are, e.g., no bit fields.  */
     613              : 
     614           45 :       gcc_assert (cmp->backend_decl);
     615           45 :       ptr = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (cmp->backend_decl));
     616           45 :       gcc_assert (ptr % 8 == 0);
     617           45 :       ptr = ptr/8 + TREE_INT_CST_LOW (DECL_FIELD_OFFSET (cmp->backend_decl));
     618              : 
     619           45 :       gcc_assert (e->ts.type != BT_VOID || cmp->attr.caf_token);
     620           45 :       gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr, e, true);
     621              :     }
     622              : 
     623           14 :   return int_size_in_bytes (type);
     624              : }
     625              : 
     626              : 
     627              : /* Read a binary buffer to a constant expression.  */
     628              : size_t
     629         2093 : gfc_target_interpret_expr (unsigned char *buffer, size_t buffer_size,
     630              :                            gfc_expr *result, bool convert_widechar)
     631              : {
     632         2093 :   if (result->expr_type == EXPR_ARRAY)
     633          307 :     return interpret_array (buffer, buffer_size, result, convert_widechar);
     634              : 
     635         1786 :   switch (result->ts.type)
     636              :     {
     637          546 :     case BT_INTEGER:
     638              :       /* 'result->value.integer' is already mpz_initialized by the caller.  */
     639          546 :       mpz_clear (result->value.integer);
     640         1092 :       result->representation.length =
     641          546 :         gfc_interpret_integer (result->ts.kind, buffer, buffer_size,
     642              :                                result->value.integer);
     643          546 :       break;
     644              : 
     645          111 :     case BT_REAL:
     646              :       /* 'result->value.real' is already mpfr_initialized by the caller.  */
     647          111 :       mpfr_clear (result->value.real);
     648          222 :       result->representation.length =
     649          111 :         gfc_interpret_float (result->ts.kind, buffer, buffer_size,
     650              :                              result->value.real);
     651          111 :       break;
     652              : 
     653           19 :     case BT_COMPLEX:
     654              :       /* 'result->value.complex' is already initialized by the caller.  */
     655           19 :       mpfr_clear (mpc_realref (result->value.complex));
     656           19 :       mpfr_clear (mpc_imagref (result->value.complex));
     657           38 :       result->representation.length =
     658           38 :         gfc_interpret_complex (result->ts.kind, buffer, buffer_size,
     659           19 :                                result->value.complex);
     660           19 :       break;
     661              : 
     662           60 :     case BT_LOGICAL:
     663          120 :       result->representation.length =
     664           60 :         gfc_interpret_logical (result->ts.kind, buffer, buffer_size,
     665              :                                &result->value.logical);
     666           60 :       break;
     667              : 
     668         1015 :     case BT_CHARACTER:
     669         2030 :       result->representation.length =
     670         1015 :         gfc_interpret_character (buffer, buffer_size, result);
     671         1015 :       break;
     672              : 
     673            0 :     case BT_CLASS:
     674            0 :       result->ts = CLASS_DATA (result)->ts;
     675              :       /* Fall through.  */
     676           35 :     case BT_DERIVED:
     677           70 :       result->representation.length =
     678           35 :         gfc_interpret_derived (buffer, buffer_size, result);
     679           35 :       gcc_assert (result->representation.length >= 0);
     680              :       break;
     681              : 
     682            0 :     case BT_VOID:
     683              :       /* This deals with caf_tokens.  */
     684            0 :       result->representation.length =
     685            0 :         gfc_interpret_integer (result->ts.kind, buffer, buffer_size,
     686            0 :                                result->value.integer);
     687            0 :       break;
     688              : 
     689            0 :     default:
     690            0 :       gfc_internal_error ("Invalid expression in gfc_target_interpret_expr.");
     691         1786 :       break;
     692              :     }
     693              : 
     694         1786 :   if (result->ts.type == BT_CHARACTER && convert_widechar)
     695            0 :     result->representation.string
     696            0 :       = gfc_widechar_to_char (result->value.character.string,
     697            0 :                               result->value.character.length);
     698              :   else
     699              :     {
     700         3572 :       result->representation.string =
     701         1786 :         XCNEWVEC (char, result->representation.length + 1);
     702         1786 :       memcpy (result->representation.string, buffer,
     703         1786 :               result->representation.length);
     704         1786 :       result->representation.string[result->representation.length] = '\0';
     705              :     }
     706              : 
     707         1786 :   return result->representation.length;
     708              : }
     709              : 
     710              : 
     711              : /* --------------------------------------------------------------- */
     712              : /* Two functions used by trans-common.cc to write overlapping
     713              :    equivalence initializers to a buffer.  This is added to the union
     714              :    and the original initializers freed.  */
     715              : 
     716              : 
     717              : /* Writes the values of a constant expression to a char buffer. If another
     718              :    unequal initializer has already been written to the buffer, this is an
     719              :    error.  */
     720              : 
     721              : static size_t
     722          206 : expr_to_char (gfc_expr *e, locus *loc,
     723              :               unsigned char *data, unsigned char *chk, size_t len)
     724              : {
     725          206 :   int i;
     726          206 :   int ptr;
     727          206 :   gfc_constructor *c;
     728          206 :   gfc_component *cmp;
     729          206 :   unsigned char *buffer;
     730              : 
     731          206 :   if (e == NULL)
     732              :     return 0;
     733              : 
     734              :   /* Take a derived type, one component at a time, using the offsets from the backend
     735              :      declaration.  */
     736          206 :   if (e->ts.type == BT_DERIVED)
     737              :     {
     738           15 :       for (c = gfc_constructor_first (e->value.constructor),
     739           15 :            cmp = e->ts.u.derived->components;
     740           54 :            c; c = gfc_constructor_next (c), cmp = cmp->next)
     741              :         {
     742           39 :           gcc_assert (cmp && cmp->backend_decl);
     743           39 :           if (!c->expr)
     744           12 :             continue;
     745           27 :           ptr = TREE_INT_CST_LOW(DECL_FIELD_OFFSET(cmp->backend_decl))
     746           27 :             + TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(cmp->backend_decl))/8;
     747           27 :           expr_to_char (c->expr, loc, &data[ptr], &chk[ptr], len);
     748              :         }
     749           15 :       return len;
     750              :     }
     751              : 
     752              :   /* Otherwise, use the target-memory machinery to write a bitwise image, appropriate
     753              :      to the target, in a buffer and check off the initialized part of the buffer.  */
     754          191 :   gfc_target_expr_size (e, &len);
     755          191 :   buffer = (unsigned char*)alloca (len);
     756          191 :   len = gfc_target_encode_expr (e, buffer, len);
     757              : 
     758         1009 :   for (i = 0; i < (int)len; i++)
     759              :     {
     760          821 :       if (chk[i] && (buffer[i] != data[i]))
     761              :         {
     762            3 :           if (loc)
     763            3 :             gfc_error ("Overlapping unequal initializers in EQUIVALENCE "
     764              :                         "at %L", loc);
     765              :           else
     766            0 :             gfc_error ("Overlapping unequal initializers in EQUIVALENCE "
     767              :                         "at %C");
     768              :           return 0;
     769              :         }
     770          818 :       chk[i] = 0xFF;
     771              :     }
     772              : 
     773          188 :   memcpy (data, buffer, len);
     774          188 :   return len;
     775              : }
     776              : 
     777              : 
     778              : /* Writes the values from the equivalence initializers to a char* array
     779              :    that will be written to the constructor to make the initializer for
     780              :    the union declaration.  */
     781              : 
     782              : size_t
     783          239 : gfc_merge_initializers (const gfc_typespec &ts, gfc_expr *e, locus *loc,
     784              :                         unsigned char *data,
     785              :                         unsigned char *chk, size_t length)
     786              : {
     787          239 :   size_t len = 0;
     788          239 :   gfc_constructor * c;
     789              : 
     790          239 :   switch (e->expr_type)
     791              :     {
     792          179 :     case EXPR_CONSTANT:
     793          179 :     case EXPR_STRUCTURE:
     794          179 :       len = expr_to_char (e, loc, &data[0], &chk[0], length);
     795          179 :       break;
     796              : 
     797           60 :     case EXPR_ARRAY:
     798           60 :       for (c = gfc_constructor_first (e->value.constructor);
     799          203 :            c; c = gfc_constructor_next (c))
     800              :         {
     801          143 :           size_t elt_size;
     802              : 
     803          143 :           gfc_target_expr_size (c->expr, &elt_size);
     804              : 
     805          143 :           if (mpz_cmp_si (c->offset, 0) != 0)
     806           83 :             len = elt_size * (size_t)mpz_get_si (c->offset);
     807              : 
     808          143 :           len = len + gfc_merge_initializers (ts, c->expr, loc, &data[len],
     809              :                                               &chk[len], length - len);
     810              :         }
     811              :       break;
     812              : 
     813              :     default:
     814              :       return 0;
     815              :     }
     816              : 
     817              :   return len;
     818              : }
     819              : 
     820              : 
     821              : /* Transfer the bitpattern of a (integer) BOZ to real or complex variables.
     822              :    When successful, no BOZ or nothing to do, true is returned.  */
     823              : 
     824              : bool
     825          254 : gfc_convert_boz (gfc_expr *expr, gfc_typespec *ts)
     826              : {
     827          254 :   size_t buffer_size, boz_bit_size, ts_bit_size;
     828          254 :   int index;
     829          254 :   unsigned char *buffer;
     830              : 
     831          254 :   if (expr->ts.type != BT_INTEGER)
     832              :     return true;
     833              : 
     834              :   /* Don't convert BOZ to logical, character, derived etc.  */
     835          254 :   gcc_assert (ts->type == BT_REAL);
     836              : 
     837          254 :   buffer_size = size_float (ts->kind);
     838          254 :   ts_bit_size = buffer_size * 8;
     839              : 
     840              :   /* Convert BOZ to the smallest possible integer kind.  */
     841          254 :   boz_bit_size = mpz_sizeinbase (expr->value.integer, 2);
     842              : 
     843          254 :   gcc_assert (boz_bit_size <= ts_bit_size);
     844              : 
     845          880 :   for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
     846          880 :     if ((unsigned) gfc_integer_kinds[index].bit_size >= ts_bit_size)
     847              :       break;
     848              : 
     849          254 :   expr->ts.kind = gfc_integer_kinds[index].kind;
     850          254 :   buffer_size = MAX (buffer_size, size_integer (expr->ts.kind));
     851              : 
     852          254 :   buffer = (unsigned char*)alloca (buffer_size);
     853          254 :   encode_integer (expr->ts.kind, expr->value.integer, buffer, buffer_size);
     854          254 :   mpz_clear (expr->value.integer);
     855              : 
     856          254 :   mpfr_init (expr->value.real);
     857          254 :   gfc_interpret_float (ts->kind, buffer, buffer_size, expr->value.real);
     858              : 
     859          254 :   expr->ts.type = ts->type;
     860          254 :   expr->ts.kind = ts->kind;
     861              : 
     862          254 :   return true;
     863              : }
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.