diff options
Diffstat (limited to 'libgfortran/io/transfer.c')
-rw-r--r-- | libgfortran/io/transfer.c | 178 |
1 files changed, 156 insertions, 22 deletions
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c index a279f92..ca9246b 100644 --- a/libgfortran/io/transfer.c +++ b/libgfortran/io/transfer.c @@ -78,6 +78,9 @@ export_proto(transfer_character); 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; @@ -101,7 +104,7 @@ static st_option advance_opt[] = { }; -static void (*transfer) (bt, void *, int); +static void (*transfer) (bt, void *, int, size_t); typedef enum @@ -312,11 +315,13 @@ write_block (int length) /* 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. */ @@ -337,17 +342,20 @@ unformatted_read (bt type, void *dest, int length) /* 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); } @@ -442,7 +450,7 @@ require_type (bt expected, bt actual, fnode * f) 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; @@ -837,6 +845,29 @@ formatted_transfer (bt type, void *p, int len) 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 @@ -845,50 +876,153 @@ formatted_transfer (bt type, void *p, int len) 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; + } + } + } } @@ -1245,7 +1379,7 @@ data_transfer_init (int read_flag) /* 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); } @@ -1568,15 +1702,15 @@ finalize_transfer (void) 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; } } |