static GTY(()) tree iocall_x_character;
static GTY(()) tree iocall_x_real;
static GTY(()) tree iocall_x_complex;
+static GTY(()) tree iocall_x_array;
static GTY(()) tree iocall_open;
static GTY(()) tree iocall_close;
static GTY(()) tree iocall_inquire;
void_type_node, 2, pvoid_type_node,
gfc_int4_type_node);
+ iocall_x_array =
+ gfc_build_library_function_decl (get_identifier
+ (PREFIX("transfer_array")),
+ void_type_node, 2, pvoid_type_node,
+ gfc_charlen_type_node);
+
/* Library entry points */
iocall_read =
}
+/* Generate a call to pass an array descriptor to the IO library. The
+ array should be of one of the intrinsic types. */
+
+static void
+transfer_array_desc (gfc_se * se, gfc_typespec * ts, tree addr_expr)
+{
+ tree args, tmp, charlen_arg;
+
+ if (ts->type == BT_CHARACTER)
+ charlen_arg = se->string_length;
+ else
+ charlen_arg = build_int_cstu (NULL_TREE, 0);
+
+ args = gfc_chainon_list (NULL_TREE, addr_expr);
+ args = gfc_chainon_list (args, charlen_arg);
+ tmp = gfc_build_function_call (iocall_x_array, args);
+ gfc_add_expr_to_block (&se->pre, tmp);
+ gfc_add_block_to_block (&se->pre, &se->post);
+}
+
+
/* gfc_trans_transfer()-- Translate a TRANSFER code node */
tree
tree tmp;
gfc_start_block (&block);
+ gfc_init_block (&body);
expr = code->expr;
ss = gfc_walk_expr (expr);
gfc_init_se (&se, NULL);
if (ss == gfc_ss_terminator)
- gfc_init_block (&body);
- else
+ {
+ gfc_conv_expr_reference (&se, expr);
+ transfer_expr (&se, &expr->ts, se.expr);
+ }
+ else if (expr->ts.type == BT_DERIVED)
{
/* Initialize the scalarizer. */
gfc_init_loopinfo (&loop);
gfc_copy_loopinfo_to_se (&se, &loop);
se.ss = ss;
- }
-
- gfc_conv_expr_reference (&se, expr);
- transfer_expr (&se, &expr->ts, se.expr);
+ gfc_conv_expr_reference (&se, expr);
+ transfer_expr (&se, &expr->ts, se.expr);
+ }
+ else
+ {
+ /* Pass the array descriptor to the library. */
+ gfc_conv_expr_descriptor (&se, expr, ss);
+ tmp = gfc_build_addr_expr (NULL, se.expr);
+ transfer_array_desc (&se, &expr->ts, tmp);
+ }
gfc_add_block_to_block (&body, &se.pre);
gfc_add_block_to_block (&body, &se.post);
extern void transfer_complex (void *, int);
export_proto(transfer_complex);
+extern void transfer_array (gfc_array_char *, gfc_charlen_type);
+export_proto(transfer_array);
+
gfc_unit *current_unit = NULL;
static int sf_seen_eor = 0;
static int eor_condition = 0;
};
-static void (*transfer) (bt, void *, int);
+static void (*transfer) (bt, void *, int, size_t);
typedef enum
/* Master function for unformatted reads. */
static void
-unformatted_read (bt type, void *dest, int length)
+unformatted_read (bt type, void *dest, int length, size_t nelems)
{
void *source;
int w;
+ length *= nelems;
+
/* Transfer functions get passed the kind of the entity, so we have
to fix this for COMPLEX data which are twice the size of their
kind. */
/* Master function for unformatted writes. */
static void
-unformatted_write (bt type, void *source, int length)
+unformatted_write (bt type, void *source, int length, size_t nelems)
{
void *dest;
+ size_t len;
+
+ len = length * nelems;
/* Correction for kind vs. length as in unformatted_read. */
if (type == BT_COMPLEX)
- length *= 2;
+ len *= 2;
- dest = write_block (length);
+ dest = write_block (len);
if (dest != NULL)
- memcpy (dest, source, length);
+ memcpy (dest, source, len);
}
of the next element, then comes back here to process it. */
static void
-formatted_transfer (bt type, void *p, int len)
+formatted_transfer_scalar (bt type, void *p, int len)
{
int pos, bytes_used;
fnode *f;
unget_format (f);
}
+static void
+formatted_transfer (bt type, void *p, int len, size_t nelems)
+{
+ size_t elem;
+ int size;
+ char *tmp;
+
+ tmp = (char *) p;
+
+ if (type == BT_COMPLEX)
+ size = 2 * len;
+ else
+ size = len;
+
+ /* Big loop over all the elements. */
+ for (elem = 0; elem < nelems; elem++)
+ {
+ g.item_count++;
+ formatted_transfer_scalar (type, tmp + size*elem, len);
+ }
+}
+
+
/* Data transfer entry points. The type of the data entity is
implicit in the subroutine call. This prevents us from having to
void
transfer_integer (void *p, int kind)
{
- g.item_count++;
if (ioparm.library_return != LIBRARY_OK)
return;
- transfer (BT_INTEGER, p, kind);
+ transfer (BT_INTEGER, p, kind, 1);
}
void
transfer_real (void *p, int kind)
{
- g.item_count++;
if (ioparm.library_return != LIBRARY_OK)
return;
- transfer (BT_REAL, p, kind);
+ transfer (BT_REAL, p, kind, 1);
}
void
transfer_logical (void *p, int kind)
{
- g.item_count++;
if (ioparm.library_return != LIBRARY_OK)
return;
- transfer (BT_LOGICAL, p, kind);
+ transfer (BT_LOGICAL, p, kind, 1);
}
void
transfer_character (void *p, int len)
{
- g.item_count++;
if (ioparm.library_return != LIBRARY_OK)
return;
- transfer (BT_CHARACTER, p, len);
+ transfer (BT_CHARACTER, p, len, 1);
}
void
transfer_complex (void *p, int kind)
{
- g.item_count++;
if (ioparm.library_return != LIBRARY_OK)
return;
- transfer (BT_COMPLEX, p, kind);
+ transfer (BT_COMPLEX, p, kind, 1);
+}
+
+
+void
+transfer_array (gfc_array_char *desc, gfc_charlen_type charlen)
+{
+ index_type count[GFC_MAX_DIMENSIONS];
+ index_type extent[GFC_MAX_DIMENSIONS];
+ index_type stride[GFC_MAX_DIMENSIONS];
+ index_type stride0, rank, size, type, n, kind;
+ size_t tsize;
+ char *data;
+ bt iotype;
+
+ if (ioparm.library_return != LIBRARY_OK)
+ return;
+
+ type = GFC_DESCRIPTOR_TYPE (desc);
+ size = GFC_DESCRIPTOR_SIZE (desc);
+ kind = size;
+
+ /* FIXME: What a kludge: Array descriptors and the IO library use
+ different enums for types. */
+ switch (type)
+ {
+ case GFC_DTYPE_UNKNOWN:
+ iotype = BT_NULL; /* Is this correct? */
+ break;
+ case GFC_DTYPE_INTEGER:
+ iotype = BT_INTEGER;
+ break;
+ case GFC_DTYPE_LOGICAL:
+ iotype = BT_LOGICAL;
+ break;
+ case GFC_DTYPE_REAL:
+ iotype = BT_REAL;
+ break;
+ case GFC_DTYPE_COMPLEX:
+ iotype = BT_COMPLEX;
+ kind /= 2;
+ break;
+ case GFC_DTYPE_CHARACTER:
+ iotype = BT_CHARACTER;
+ /* FIXME: Currently dtype contains the charlen, which is
+ clobbered if charlen > 2**24. That's why we use a separate
+ argument for the charlen. However, if we want to support
+ non-8-bit charsets we need to fix dtype to contain
+ sizeof(chartype) and fix the code below. */
+ size = charlen;
+ kind = charlen;
+ break;
+ case GFC_DTYPE_DERIVED:
+ internal_error ("Derived type I/O should have been handled via the frontend.");
+ break;
+ default:
+ internal_error ("transfer_array(): Bad type");
+ }
+
+ if (desc->dim[0].stride == 0)
+ desc->dim[0].stride = 1;
+
+ rank = GFC_DESCRIPTOR_RANK (desc);
+ for (n = 0; n < rank; n++)
+ {
+ count[n] = 0;
+ stride[n] = desc->dim[n].stride;
+ extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
+
+ /* If the extent of even one dimension is zero, then the entire
+ array section contains zero elements, so we return. */
+ if (extent[n] == 0)
+ return;
+ }
+
+ stride0 = stride[0];
+
+ /* If the innermost dimension has stride 1, we can do the transfer
+ in contiguous chunks. */
+ if (stride0 == 1)
+ tsize = extent[0];
+ else
+ tsize = 1;
+
+ data = GFC_DESCRIPTOR_DATA (desc);
+
+ while (data)
+ {
+ transfer (iotype, data, kind, tsize);
+ data += stride0 * size * tsize;
+ count[0] += tsize;
+ n = 0;
+ while (count[n] == extent[n])
+ {
+ count[n] = 0;
+ data -= stride[n] * extent[n] * size;
+ n++;
+ if (n == rank)
+ {
+ data = NULL;
+ break;
+ }
+ else
+ {
+ count[n]++;
+ data += stride[n] * size;
+ }
+ }
+ }
}
/* Start the data transfer if we are doing a formatted transfer. */
if (current_unit->flags.form == FORM_FORMATTED && !ioparm.list_format
&& ioparm.namelist_name == NULL && ionml == NULL)
- formatted_transfer (0, NULL, 0);
+ formatted_transfer (0, NULL, 0, 1);
}
data transfer, it just updates the length counter. */
static void
-iolength_transfer (bt type , void *dest __attribute__ ((unused)),
- int len)
+iolength_transfer (bt type, void *dest __attribute__ ((unused)),
+ int len, size_t nelems)
{
if (ioparm.iolength != NULL)
{
if (type == BT_COMPLEX)
- *ioparm.iolength += 2*len;
+ *ioparm.iolength += 2 * len * nelems;
else
- *ioparm.iolength += len;
+ *ioparm.iolength += len * nelems;
}
}