This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: A question on POINTERS_EXTEND_UNSIGNED


One way to solve it is to check if there is an overflow when computing
array index. We do sign-extension when there is an overflow and zero-
extension when there is no overflow. Does anyone have some pointers how
to do it?

Thanks.


H.J.
---
On Sun, Aug 17, 2003 at 12:37:28AM -0700, H. J. Lu wrote:
> Gcc uses some clever number theory for array indexing:
> 
> http://gcc.gnu.org/ml/gcc/2003-07/msg01382.html
> 
> For the code enclosed here, gcc depends on overflow to get
> it right. However, when POINTERS_EXTEND_UNSIGNED is defined,
> there are
> 
> #ifdef POINTERS_EXTEND_UNSIGNED
> 
> /* Given X, a memory address in ptr_mode, convert it to an address
>    in Pmode, or vice versa (TO_MODE says which way).  We take advantage of
>    the fact that pointers are not allowed to overflow by commuting arithmetic
>    operations over conversions so that address arithmetic insns can be
>    used.  */
> 
> rtx
> convert_memory_address (to_mode, x)
>      enum machine_mode to_mode;
>      rtx x;
> {
> ...
>   return convert_modes (to_mode, from_mode,
>                         x, POINTERS_EXTEND_UNSIGNED);
> }
> #endif
> 
> in explow.c. For targets with
> 
> #define POINTERS_EXTEND_UNSIGNED 1
> 
> if to_mode is wider than from_mode, the index will be zero-extended,
> which may lead to the wrong index computed due to overflow. How should
> such a target deal with it?
> 
> 
> H.J.
> ----
> #include <stdio.h>
> 
> typedef struct
> {
>   int i;
> } foo;
> 
> 
> static void
> bar (foo *x, foo **y)
> {
>   while (x->i != 0)
>     {
>       y [x->i - 0x70000000 + 34] = x++;
>     }
> }
> 
> static foo *f [35];
> 
> int
> main ()
> {
>   foo i[2];
>   i [0].i = 0x70000000;
>   i [1].i = 0;
> 
>   bar (i, f);
>   printf ("%p\n", f[34]);
>   printf ("%p\n", &i[0]);
>   return 0;
> }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]