upon failure. */
static int
-native_encode_int (const_tree expr, unsigned char *ptr, int len)
+native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
{
tree type = TREE_TYPE (expr);
int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
int byte, offset, word, words;
unsigned char value;
- if (total_bytes > len)
+ if ((off == -1 && total_bytes > len)
+ || off >= total_bytes)
return 0;
+ if (off == -1)
+ off = 0;
words = total_bytes / UNITS_PER_WORD;
for (byte = 0; byte < total_bytes; byte++)
}
else
offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
- ptr[offset] = value;
+ if (offset >= off
+ && offset - off < len)
+ ptr[offset - off] = value;
}
- return total_bytes;
+ return MIN (len, total_bytes - off);
}
upon failure. */
static int
-native_encode_fixed (const_tree expr, unsigned char *ptr, int len)
+native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
{
tree type = TREE_TYPE (expr);
enum machine_mode mode = TYPE_MODE (type);
value = TREE_FIXED_CST (expr);
i_value = double_int_to_tree (i_type, value.data);
- return native_encode_int (i_value, ptr, len);
+ return native_encode_int (i_value, ptr, len, off);
}
upon failure. */
static int
-native_encode_real (const_tree expr, unsigned char *ptr, int len)
+native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
{
tree type = TREE_TYPE (expr);
int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
up to 192 bits. */
long tmp[6];
- if (total_bytes > len)
+ if ((off == -1 && total_bytes > len)
+ || off >= total_bytes)
return 0;
+ if (off == -1)
+ off = 0;
words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
}
else
offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
- ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
+ offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
+ if (offset >= off
+ && offset - off < len)
+ ptr[offset - off] = value;
}
- return total_bytes;
+ return MIN (len, total_bytes - off);
}
/* Subroutine of native_encode_expr. Encode the COMPLEX_CST
upon failure. */
static int
-native_encode_complex (const_tree expr, unsigned char *ptr, int len)
+native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
{
int rsize, isize;
tree part;
part = TREE_REALPART (expr);
- rsize = native_encode_expr (part, ptr, len);
- if (rsize == 0)
+ rsize = native_encode_expr (part, ptr, len, off);
+ if (off == -1
+ && rsize == 0)
return 0;
part = TREE_IMAGPART (expr);
- isize = native_encode_expr (part, ptr+rsize, len-rsize);
- if (isize != rsize)
+ if (off != -1)
+ off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
+ isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
+ if (off == -1
+ && isize != rsize)
return 0;
return rsize + isize;
}
upon failure. */
static int
-native_encode_vector (const_tree expr, unsigned char *ptr, int len)
+native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
{
unsigned i, count;
int size, offset;
size = GET_MODE_SIZE (TYPE_MODE (itype));
for (i = 0; i < count; i++)
{
+ if (off >= size)
+ {
+ off -= size;
+ continue;
+ }
elem = VECTOR_CST_ELT (expr, i);
- if (native_encode_expr (elem, ptr+offset, len-offset) != size)
+ int res = native_encode_expr (elem, ptr+offset, len-offset, off);
+ if ((off == -1 && res != size)
+ || res == 0)
return 0;
- offset += size;
+ offset += res;
+ if (offset >= len)
+ return offset;
+ if (off != -1)
+ off = 0;
}
return offset;
}
upon failure. */
static int
-native_encode_string (const_tree expr, unsigned char *ptr, int len)
+native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
{
tree type = TREE_TYPE (expr);
HOST_WIDE_INT total_bytes;
|| !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
return 0;
total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
- if (total_bytes > len)
+ if ((off == -1 && total_bytes > len)
+ || off >= total_bytes)
return 0;
- if (TREE_STRING_LENGTH (expr) < total_bytes)
+ if (off == -1)
+ off = 0;
+ if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
{
- memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
- memset (ptr + TREE_STRING_LENGTH (expr), 0,
- total_bytes - TREE_STRING_LENGTH (expr));
+ int written = 0;
+ if (off < TREE_STRING_LENGTH (expr))
+ {
+ written = MIN (len, TREE_STRING_LENGTH (expr) - off);
+ memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
+ }
+ memset (ptr + written, 0,
+ MIN (total_bytes - written, len - written));
}
else
- memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
- return total_bytes;
+ memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
+ return MIN (total_bytes - off, len);
}
/* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
- buffer PTR of length LEN bytes. Return the number of bytes
- placed in the buffer, or zero upon failure. */
+ buffer PTR of length LEN bytes. If OFF is not -1 then start
+ the encoding at byte offset OFF and encode at most LEN bytes.
+ Return the number of bytes placed in the buffer, or zero upon failure. */
int
-native_encode_expr (const_tree expr, unsigned char *ptr, int len)
+native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
{
switch (TREE_CODE (expr))
{
case INTEGER_CST:
- return native_encode_int (expr, ptr, len);
+ return native_encode_int (expr, ptr, len, off);
case REAL_CST:
- return native_encode_real (expr, ptr, len);
+ return native_encode_real (expr, ptr, len, off);
case FIXED_CST:
- return native_encode_fixed (expr, ptr, len);
+ return native_encode_fixed (expr, ptr, len, off);
case COMPLEX_CST:
- return native_encode_complex (expr, ptr, len);
+ return native_encode_complex (expr, ptr, len, off);
case VECTOR_CST:
- return native_encode_vector (expr, ptr, len);
+ return native_encode_vector (expr, ptr, len, off);
case STRING_CST:
- return native_encode_string (expr, ptr, len);
+ return native_encode_string (expr, ptr, len, off);
default:
return 0;
}
}
-/* CTOR is STRING_CST. Fold reference of type TYPE and size SIZE
- to the memory at bit OFFSET.
-
- We do only simple job of folding byte accesses. */
-
-static tree
-fold_string_cst_ctor_reference (tree type, tree ctor,
- unsigned HOST_WIDE_INT offset,
- unsigned HOST_WIDE_INT size)
-{
- if (INTEGRAL_TYPE_P (type)
- && (TYPE_MODE (type)
- == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
- && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
- == MODE_INT)
- && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) == 1
- && size == BITS_PER_UNIT
- && !(offset % BITS_PER_UNIT))
- {
- offset /= BITS_PER_UNIT;
- if (offset < (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (ctor))
- return build_int_cst_type (type, (TREE_STRING_POINTER (ctor)
- [offset]));
- /* Folding
- const char a[20]="hello";
- return a[10];
-
- might lead to offset greater than string length. In this case we
- know value is either initialized to 0 or out of bounds. Return 0
- in both cases. */
- return build_zero_cst (type);
- }
- return NULL_TREE;
-}
-
/* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
SIZE to the memory at bit OFFSET. */
STRIP_NOPS (ret);
return ret;
}
- if (TREE_CODE (ctor) == STRING_CST)
- return fold_string_cst_ctor_reference (type, ctor, offset, size);
+ /* For constants and byte-aligned/sized reads try to go through
+ native_encode/interpret. */
+ if (CONSTANT_CLASS_P (ctor)
+ && BITS_PER_UNIT == 8
+ && offset % BITS_PER_UNIT == 0
+ && size % BITS_PER_UNIT == 0
+ && size <= MAX_BITSIZE_MODE_ANY_MODE)
+ {
+ unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
+ if (native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
+ offset / BITS_PER_UNIT) > 0)
+ return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
+ }
if (TREE_CODE (ctor) == CONSTRUCTOR)
{