f622221ab4
In order to handle large character lengths on (L)LP64 targets, switch the GFortran character length from an int to a size_t. This is an ABI change, as procedures with character arguments take hidden arguments with the character length. I also changed the _size member in vtables from int to size_t, as there were some cases where character lengths and sizes were apparently mixed up and caused regressions otherwise. Although I haven't tested, this might enable very large derived types as well. Also, as there are some places in the frontend were negative character lengths are used as special flag values, in the frontend the character length is handled as a signed variable of the same size as a size_t, although in the runtime library it really is size_t. I haven't changed the character length variables for the co-array intrinsics, as this is something that may need to be synchronized with OpenCoarrays. This is v5 of the patch. v4 was applied but caused breakage on big endian targets. These have been fixed and tested, thanks to access to the GCC compile farm. Overview of v4 of the patch: v3 was applied but had to reverted due to breaking bootstrap. The fix is in resolve.c:resolve_charlen, where it's necessary to check that an expression is constant before using mpz_sgn. Overview of v3 of the patch: All the issues pointed out by FX's review of v2 have been fixed. In particular, there are now new functions gfc_mpz_get_hwi and gfc_mpz_set_hwi, similar to the GMP functions mpz_get_si and mpz_set_si, except that they get/set a HOST_WIDE_INT instead of a long value. Similarly, gfc_get_int_expr now takes a HOST_WIDE_INT instead of a long, gfc_extract_long is replaced by gfc_extract_hwi. Also, the preliminary work to handle gfc_charlen_type_node being unsigned has been removed. Regtested on x86_64-pc-linux-gnu, i686-pc-linux-gnu and powerpc64-unknown-linux-gnu. Also regtested all three targets by modifying gfortran-dg.exp to also test with "-g -flto", no new failures observed. frontend: 2018-01-05 Janne Blomqvist <jb@gcc.gnu.org> PR fortran/78534 PR fortran/66310 * array.c (got_charlen): Use gfc_charlen_int_kind. * class.c (gfc_find_derived_vtab): Use gfc_size_kind instead of hardcoded kind. (find_intrinsic_vtab): Likewise. * decl.c (match_char_length): Use gfc_charlen_int_kind. (add_init_expr_to_sym): Use gfc_charlen_t and gfc_charlen_int_kind. (gfc_match_implicit): Use gfc_charlen_int_kind. * dump-parse-tree.c (show_char_const): Use gfc_charlen_t and size_t. (show_expr): Use HOST_WIDE_INT_PRINT_DEC. * expr.c (gfc_get_character_expr): Length parameter of type gfc_charlen_t. (gfc_get_int_expr): Value argument of type HOST_WIDE_INT. (gfc_extract_hwi): New function. (simplify_const_ref): Make string_len of type gfc_charlen_t. (gfc_simplify_expr): Use HOST_WIDE_INT for substring refs. * frontend-passes.c (optimize_trim): Use gfc_charlen_int_kind. * gfortran.h (gfc_mpz_get_hwi): New prototype. (gfc_mpz_set_hwi): Likewise. (gfc_charlen_t): New typedef. (gfc_expr): Use gfc_charlen_t for character lengths. (gfc_size_kind): New extern variable. (gfc_extract_hwi): New prototype. (gfc_get_character_expr): Use gfc_charlen_t for character length. (gfc_get_int_expr): Use HOST_WIDE_INT type for value argument. * gfortran.texi: Update description of hidden string length argument. * iresolve.c (check_charlen_present): Use gfc_charlen_int_kind. (gfc_resolve_char_achar): Likewise. (gfc_resolve_repeat): Pass string length directly without temporary, use gfc_charlen_int_kind. (gfc_resolve_transfer): Use gfc_charlen_int_kind. * match.c (select_intrinsic_set_tmp): Use HOST_WIDE_INT for charlen. * misc.c (gfc_mpz_get_hwi): New function. (gfc_mpz_set_hwi): New function. * module.c (atom_int): Change type from int to HOST_WIDE_INT. (parse_integer): Don't complain about large integers. (write_atom): Use HOST_WIDE_INT for integers. (mio_integer): Handle integer type mismatch. (mio_hwi): New function. (mio_intrinsic_op): Use HOST_WIDE_INT. (mio_array_ref): Likewise. (mio_expr): Likewise. * primary.c (match_substring): Use gfc_charlen_int_kind. * resolve.c (resolve_substring_charlen): Use gfc_charlen_int_kind. (resolve_character_operator): Likewise. (resolve_assoc_var): Likewise. (resolve_select_type): Use HOST_WIDE_INT for charlen, use snprintf. (resolve_charlen): Use mpz_sgn to determine sign. * simplify.c (gfc_simplify_repeat): Use HOST_WIDE_INT/gfc_charlen_t instead of long. * symbol.c (generate_isocbinding_symbol): Use gfc_charlen_int_kind. * target-memory.c (size_character): Length argument of type gfc_charlen_t. (gfc_encode_character): Likewise. (gfc_interpret_character): Use gfc_charlen_t. * target-memory.h (gfc_encode_character): Modify prototype. * trans-array.c (gfc_trans_array_ctor_element): Use existing type. (get_array_ctor_var_strlen): Use gfc_conv_mpz_to_tree_type. (trans_array_constructor): Use existing type. (get_array_charlen): Likewise. * trans-const.c (gfc_conv_mpz_to_tree_type): New function. * trans-const.h (gfc_conv_mpz_to_tree_type): New prototype. * trans-decl.c (gfc_trans_deferred_vars): Use existing type. (add_argument_checking): Likewise. * trans-expr.c (gfc_class_len_or_zero_get): Build const of type gfc_charlen_type_node. (gfc_conv_intrinsic_to_class): Use gfc_charlen_int_kind instead of 4, fold_convert to correct type. (gfc_conv_class_to_class): Build const of type size_type_node for size. (gfc_copy_class_to_class): Likewise. (gfc_conv_string_length): Use same type in expression. (gfc_conv_substring): Likewise, use HOST_WIDE_INT for charlen. (gfc_conv_string_tmp): Make sure len is of the right type. (gfc_conv_concat_op): Use same type in expression. (gfc_conv_procedure_call): Likewise. (fill_with_spaces): Comment out memset() block due to spurious -Wstringop-overflow warnings. (gfc_trans_string_copy): Use gfc_charlen_type_node. (alloc_scalar_allocatable_for_subcomponent_assignment): fold_convert to right type. (gfc_trans_subcomponent_assign): Likewise. (trans_class_vptr_len_assignment): Build const of correct type. (gfc_trans_pointer_assignment): Likewise. (alloc_scalar_allocatable_for_assignment): fold_convert to right type in expr. (trans_class_assignment): Build const of correct type. * trans-intrinsic.c (gfc_conv_associated): Likewise. (gfc_conv_intrinsic_repeat): Do calculation in sizetype. * trans-io.c (gfc_build_io_library_fndecls): Use gfc_charlen_type_node for character lengths. (set_string): Convert to right type in assignment. * trans-stmt.c (gfc_trans_label_assign): Build const of gfc_charlen_type_node. (trans_associate_var): Likewise. (gfc_trans_character_select): Likewise. (gfc_trans_allocate): Likewise, don't typecast strlen result. (gfc_trans_deallocate): Don't typecast strlen result. * trans-types.c (gfc_size_kind): New variable. (gfc_init_types): Determine gfc_charlen_int_kind and gfc_size_kind from size_type_node. * trans-types.h: Fix comment. testsuite: 2018-01-05 Janne Blomqvist <jb@gcc.gnu.org> PR fortran/78534 PR fortran/66310 * gfortran.dg/char_cast_1.f90: Update scan pattern. * gfortran.dg/dependency_49.f90: Likewise. * gfortran.dg/repeat_4.f90: Use integers of kind C_SIZE_T. * gfortran.dg/repeat_7.f90: New test for PR 66310. * gfortran.dg/scan_2.f90: Handle potential cast in assignment. * gfortran.dg/string_1.f90: Limit to ilp32 targets. * gfortran.dg/string_1_lp64.f90: New test. * gfortran.dg/string_3.f90: Limit to ilp32 targets. * gfortran.dg/string_3_lp64.f90: New test. libgfortran: 2019-01-05 Janne Blomqvist <jb@gcc.gnu.org> PR fortran/78534 * intrinsics/args.c (getarg_i4): Use gfc_charlen_type. (get_command_argument_i4): Likewise. (get_command_i4): Likewise. * intrinsics/chmod.c (chmod_internal): Likewise. * intrinsics/env.c (get_environment_variable_i4): Likewise. * intrinsics/extends_type_of.c (struct vtype): Use size_t for size member. * intrinsics/gerror.c (gerror): Use gfc_charlen_type. * intrinsics/getlog.c (getlog): Likewise. * intrinsics/hostnm.c (hostnm_0): Likewise. * intrinsics/string_intrinsics_inc.c (string_len_trim): Rework to work if gfc_charlen_type is unsigned. (string_scan): Likewise. * io/transfer.c (transfer_character): Modify prototype. (transfer_character_write): Likewise. (transfer_character_wide): Likewise. (transfer_character_wide_write): Likewise. (transfer_array): Typecast to avoid signed-unsigned comparison. * io/unit.c (is_trim_ok): Use gfc_charlen_type. * io/write.c (namelist_write): Likewise. * libgfortran.h (gfc_charlen_type): Change typedef to size_t. From-SVN: r256284
454 lines
10 KiB
C
454 lines
10 KiB
C
/* String intrinsics helper functions.
|
|
Copyright (C) 2002-2018 Free Software Foundation, Inc.
|
|
|
|
This file is part of the GNU Fortran runtime library (libgfortran).
|
|
|
|
Libgfortran is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 3 of the License, or (at your option) any later version.
|
|
|
|
Libgfortran is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
3.1, as published by the Free Software Foundation.
|
|
|
|
You should have received a copy of the GNU General Public License and
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
/* Rename the functions. */
|
|
#define concat_string SUFFIX(concat_string)
|
|
#define string_len_trim SUFFIX(string_len_trim)
|
|
#define adjustl SUFFIX(adjustl)
|
|
#define adjustr SUFFIX(adjustr)
|
|
#define string_index SUFFIX(string_index)
|
|
#define string_scan SUFFIX(string_scan)
|
|
#define string_verify SUFFIX(string_verify)
|
|
#define string_trim SUFFIX(string_trim)
|
|
#define string_minmax SUFFIX(string_minmax)
|
|
#define zero_length_string SUFFIX(zero_length_string)
|
|
#define compare_string SUFFIX(compare_string)
|
|
|
|
|
|
/* The prototypes. */
|
|
|
|
extern void concat_string (gfc_charlen_type, CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *);
|
|
export_proto(concat_string);
|
|
|
|
extern void adjustl (CHARTYPE *, gfc_charlen_type, const CHARTYPE *);
|
|
export_proto(adjustl);
|
|
|
|
extern void adjustr (CHARTYPE *, gfc_charlen_type, const CHARTYPE *);
|
|
export_proto(adjustr);
|
|
|
|
extern gfc_charlen_type string_index (gfc_charlen_type, const CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *,
|
|
GFC_LOGICAL_4);
|
|
export_proto(string_index);
|
|
|
|
extern gfc_charlen_type string_scan (gfc_charlen_type, const CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *,
|
|
GFC_LOGICAL_4);
|
|
export_proto(string_scan);
|
|
|
|
extern gfc_charlen_type string_verify (gfc_charlen_type, const CHARTYPE *,
|
|
gfc_charlen_type, const CHARTYPE *,
|
|
GFC_LOGICAL_4);
|
|
export_proto(string_verify);
|
|
|
|
extern void string_trim (gfc_charlen_type *, CHARTYPE **, gfc_charlen_type,
|
|
const CHARTYPE *);
|
|
export_proto(string_trim);
|
|
|
|
extern void string_minmax (gfc_charlen_type *, CHARTYPE **, int, int, ...);
|
|
export_proto(string_minmax);
|
|
|
|
|
|
/* Use for functions which can return a zero-length string. */
|
|
static CHARTYPE zero_length_string = 0;
|
|
|
|
|
|
/* Strings of unequal length are extended with pad characters. */
|
|
|
|
int
|
|
compare_string (gfc_charlen_type len1, const CHARTYPE *s1,
|
|
gfc_charlen_type len2, const CHARTYPE *s2)
|
|
{
|
|
const UCHARTYPE *s;
|
|
gfc_charlen_type len;
|
|
int res;
|
|
|
|
res = MEMCMP (s1, s2, ((len1 < len2) ? len1 : len2));
|
|
if (res != 0)
|
|
return res;
|
|
|
|
if (len1 == len2)
|
|
return 0;
|
|
|
|
if (len1 < len2)
|
|
{
|
|
len = len2 - len1;
|
|
s = (UCHARTYPE *) &s2[len1];
|
|
res = -1;
|
|
}
|
|
else
|
|
{
|
|
len = len1 - len2;
|
|
s = (UCHARTYPE *) &s1[len2];
|
|
res = 1;
|
|
}
|
|
|
|
while (len--)
|
|
{
|
|
if (*s != ' ')
|
|
{
|
|
if (*s > ' ')
|
|
return res;
|
|
else
|
|
return -res;
|
|
}
|
|
s++;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
iexport(compare_string);
|
|
|
|
|
|
/* The destination and source should not overlap. */
|
|
|
|
void
|
|
concat_string (gfc_charlen_type destlen, CHARTYPE * dest,
|
|
gfc_charlen_type len1, const CHARTYPE * s1,
|
|
gfc_charlen_type len2, const CHARTYPE * s2)
|
|
{
|
|
if (len1 >= destlen)
|
|
{
|
|
memcpy (dest, s1, destlen * sizeof (CHARTYPE));
|
|
return;
|
|
}
|
|
memcpy (dest, s1, len1 * sizeof (CHARTYPE));
|
|
dest += len1;
|
|
destlen -= len1;
|
|
|
|
if (len2 >= destlen)
|
|
{
|
|
memcpy (dest, s2, destlen * sizeof (CHARTYPE));
|
|
return;
|
|
}
|
|
|
|
memcpy (dest, s2, len2 * sizeof (CHARTYPE));
|
|
MEMSET (&dest[len2], ' ', destlen - len2);
|
|
}
|
|
|
|
|
|
/* Return string with all trailing blanks removed. */
|
|
|
|
void
|
|
string_trim (gfc_charlen_type *len, CHARTYPE **dest, gfc_charlen_type slen,
|
|
const CHARTYPE *src)
|
|
{
|
|
*len = string_len_trim (slen, src);
|
|
|
|
if (*len == 0)
|
|
*dest = &zero_length_string;
|
|
else
|
|
{
|
|
/* Allocate space for result string. */
|
|
*dest = xmallocarray (*len, sizeof (CHARTYPE));
|
|
|
|
/* Copy string if necessary. */
|
|
memcpy (*dest, src, *len * sizeof (CHARTYPE));
|
|
}
|
|
}
|
|
|
|
|
|
/* The length of a string not including trailing blanks. */
|
|
|
|
gfc_charlen_type
|
|
string_len_trim (gfc_charlen_type len, const CHARTYPE *s)
|
|
{
|
|
if (len <= 0)
|
|
return 0;
|
|
|
|
const size_t long_len = sizeof (unsigned long);
|
|
|
|
size_t i = len - 1;
|
|
|
|
/* If we've got the standard (KIND=1) character type, we scan the string in
|
|
long word chunks to speed it up (until a long word is hit that does not
|
|
consist of ' 's). */
|
|
if (sizeof (CHARTYPE) == 1 && i >= long_len)
|
|
{
|
|
size_t starting;
|
|
unsigned long blank_longword;
|
|
|
|
/* Handle the first characters until we're aligned on a long word
|
|
boundary. Actually, s + i + 1 must be properly aligned, because
|
|
s + i will be the last byte of a long word read. */
|
|
starting = (
|
|
#ifdef __INTPTR_TYPE__
|
|
(__INTPTR_TYPE__)
|
|
#endif
|
|
(s + i + 1)) % long_len;
|
|
i -= starting;
|
|
for (; starting > 0; --starting)
|
|
if (s[i + starting] != ' ')
|
|
return i + starting + 1;
|
|
|
|
/* Handle the others in a batch until first non-blank long word is
|
|
found. Here again, s + i is the last byte of the current chunk,
|
|
to it starts at s + i - sizeof (long) + 1. */
|
|
|
|
#if __SIZEOF_LONG__ == 4
|
|
blank_longword = 0x20202020L;
|
|
#elif __SIZEOF_LONG__ == 8
|
|
blank_longword = 0x2020202020202020L;
|
|
#else
|
|
#error Invalid size of long!
|
|
#endif
|
|
|
|
while (i >= long_len)
|
|
{
|
|
i -= long_len;
|
|
if (*((unsigned long*) (s + i + 1)) != blank_longword)
|
|
{
|
|
i += long_len;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Simply look for the first non-blank character. */
|
|
while (s[i] == ' ')
|
|
{
|
|
if (i == 0)
|
|
return 0;
|
|
--i;
|
|
}
|
|
return i + 1;
|
|
}
|
|
|
|
|
|
/* Find a substring within a string. */
|
|
|
|
gfc_charlen_type
|
|
string_index (gfc_charlen_type slen, const CHARTYPE *str,
|
|
gfc_charlen_type sslen, const CHARTYPE *sstr,
|
|
GFC_LOGICAL_4 back)
|
|
{
|
|
gfc_charlen_type start, last, delta, i;
|
|
|
|
if (sslen == 0)
|
|
return back ? (slen + 1) : 1;
|
|
|
|
if (sslen > slen)
|
|
return 0;
|
|
|
|
if (!back)
|
|
{
|
|
last = slen + 1 - sslen;
|
|
start = 0;
|
|
delta = 1;
|
|
}
|
|
else
|
|
{
|
|
last = -1;
|
|
start = slen - sslen;
|
|
delta = -1;
|
|
}
|
|
|
|
for (; start != last; start+= delta)
|
|
{
|
|
for (i = 0; i < sslen; i++)
|
|
{
|
|
if (str[start + i] != sstr[i])
|
|
break;
|
|
}
|
|
if (i == sslen)
|
|
return (start + 1);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Remove leading blanks from a string, padding at end. The src and dest
|
|
should not overlap. */
|
|
|
|
void
|
|
adjustl (CHARTYPE *dest, gfc_charlen_type len, const CHARTYPE *src)
|
|
{
|
|
gfc_charlen_type i;
|
|
|
|
i = 0;
|
|
while (i < len && src[i] == ' ')
|
|
i++;
|
|
|
|
if (i < len)
|
|
memcpy (dest, &src[i], (len - i) * sizeof (CHARTYPE));
|
|
if (i > 0)
|
|
MEMSET (&dest[len - i], ' ', i);
|
|
}
|
|
|
|
|
|
/* Remove trailing blanks from a string. */
|
|
|
|
void
|
|
adjustr (CHARTYPE *dest, gfc_charlen_type len, const CHARTYPE *src)
|
|
{
|
|
gfc_charlen_type i;
|
|
|
|
i = len;
|
|
while (i > 0 && src[i - 1] == ' ')
|
|
i--;
|
|
|
|
if (i < len)
|
|
MEMSET (dest, ' ', len - i);
|
|
memcpy (&dest[len - i], src, i * sizeof (CHARTYPE));
|
|
}
|
|
|
|
|
|
/* Scan a string for any one of the characters in a set of characters. */
|
|
|
|
gfc_charlen_type
|
|
string_scan (gfc_charlen_type slen, const CHARTYPE *str,
|
|
gfc_charlen_type setlen, const CHARTYPE *set, GFC_LOGICAL_4 back)
|
|
{
|
|
gfc_charlen_type i, j;
|
|
|
|
if (slen == 0 || setlen == 0)
|
|
return 0;
|
|
|
|
if (back)
|
|
{
|
|
for (i = slen; i != 0; i--)
|
|
{
|
|
for (j = 0; j < setlen; j++)
|
|
{
|
|
if (str[i - 1] == set[j])
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (i = 0; i < slen; i++)
|
|
{
|
|
for (j = 0; j < setlen; j++)
|
|
{
|
|
if (str[i] == set[j])
|
|
return (i + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Verify that a set of characters contains all the characters in a
|
|
string by identifying the position of the first character in a
|
|
characters that does not appear in a given set of characters. */
|
|
|
|
gfc_charlen_type
|
|
string_verify (gfc_charlen_type slen, const CHARTYPE *str,
|
|
gfc_charlen_type setlen, const CHARTYPE *set,
|
|
GFC_LOGICAL_4 back)
|
|
{
|
|
gfc_charlen_type start, last, delta, i;
|
|
|
|
if (slen == 0)
|
|
return 0;
|
|
|
|
if (back)
|
|
{
|
|
last = -1;
|
|
start = slen - 1;
|
|
delta = -1;
|
|
}
|
|
else
|
|
{
|
|
last = slen;
|
|
start = 0;
|
|
delta = 1;
|
|
}
|
|
for (; start != last; start += delta)
|
|
{
|
|
for (i = 0; i < setlen; i++)
|
|
{
|
|
if (str[start] == set[i])
|
|
break;
|
|
}
|
|
if (i == setlen)
|
|
return (start + 1);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* MIN and MAX intrinsics for strings. The front-end makes sure that
|
|
nargs is at least 2. */
|
|
|
|
void
|
|
string_minmax (gfc_charlen_type *rlen, CHARTYPE **dest, int op, int nargs, ...)
|
|
{
|
|
va_list ap;
|
|
int i;
|
|
CHARTYPE *next, *res;
|
|
gfc_charlen_type nextlen, reslen;
|
|
|
|
va_start (ap, nargs);
|
|
reslen = va_arg (ap, gfc_charlen_type);
|
|
res = va_arg (ap, CHARTYPE *);
|
|
*rlen = reslen;
|
|
|
|
if (res == NULL)
|
|
runtime_error ("First argument of '%s' intrinsic should be present",
|
|
op > 0 ? "MAX" : "MIN");
|
|
|
|
for (i = 1; i < nargs; i++)
|
|
{
|
|
nextlen = va_arg (ap, gfc_charlen_type);
|
|
next = va_arg (ap, CHARTYPE *);
|
|
|
|
if (next == NULL)
|
|
{
|
|
if (i == 1)
|
|
runtime_error ("Second argument of '%s' intrinsic should be "
|
|
"present", op > 0 ? "MAX" : "MIN");
|
|
else
|
|
continue;
|
|
}
|
|
|
|
if (nextlen > *rlen)
|
|
*rlen = nextlen;
|
|
|
|
if (op * compare_string (reslen, res, nextlen, next) < 0)
|
|
{
|
|
reslen = nextlen;
|
|
res = next;
|
|
}
|
|
}
|
|
va_end (ap);
|
|
|
|
if (*rlen == 0)
|
|
*dest = &zero_length_string;
|
|
else
|
|
{
|
|
CHARTYPE *tmp = xmallocarray (*rlen, sizeof (CHARTYPE));
|
|
memcpy (tmp, res, reslen * sizeof (CHARTYPE));
|
|
MEMSET (&tmp[reslen], ' ', *rlen - reslen);
|
|
*dest = tmp;
|
|
}
|
|
}
|