diff mbox

paflof: Silence gcc's -Warray-bounds warning for stack pointers

Message ID 20170717031102.44352-1-aik@ozlabs.ru
State Accepted
Headers show

Commit Message

Alexey Kardashevskiy July 17, 2017, 3:11 a.m. UTC
The SLOF stack pointers - dp/rp - point to the top used element which
means for an empty stack they point to an element below the stack.
This means that for pushing to the stack we can use a store-with-update
instruction (stdu). This generates good code for most primitives,
better than the other stack pointer offsets.

However, with -Warray-bounds enabled, this produces warnings like below:

At the moment SLOF is  gcc produces a warning:
/home/aik/p/slof/slof/paflof.c: In function ‘engine’:
/home/aik/p/slof/slof/paflof.c:84:23: warning: array subscript is below array bounds [-Warray-bounds]
   dp = the_data_stack - 1;
        ~~~~~~~~~~~~~~~^~~

This silences gcc by doing c-cast.

Suggested-by: Segher Boessenkool <segher@kernel.crashing.org>
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---

uintptr_t is not used anywhere in SLOF, hence type_u.
---
 slof/paflof.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Thomas Huth July 17, 2017, 7:41 a.m. UTC | #1
On 17.07.2017 05:11, Alexey Kardashevskiy wrote:
> The SLOF stack pointers - dp/rp - point to the top used element which
> means for an empty stack they point to an element below the stack.
> This means that for pushing to the stack we can use a store-with-update
> instruction (stdu). This generates good code for most primitives,
> better than the other stack pointer offsets.
> 
> However, with -Warray-bounds enabled, this produces warnings like below:
> 
> At the moment SLOF is  gcc produces a warning:
> /home/aik/p/slof/slof/paflof.c: In function ‘engine’:
> /home/aik/p/slof/slof/paflof.c:84:23: warning: array subscript is below array bounds [-Warray-bounds]
>    dp = the_data_stack - 1;
>         ~~~~~~~~~~~~~~~^~~
> 
> This silences gcc by doing c-cast.
> 
> Suggested-by: Segher Boessenkool <segher@kernel.crashing.org>
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
> ---
> 
> uintptr_t is not used anywhere in SLOF, hence type_u.
> ---
>  slof/paflof.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/slof/paflof.c b/slof/paflof.c
> index 50b4adf..e70f601 100644
> --- a/slof/paflof.c
> +++ b/slof/paflof.c
> @@ -81,8 +81,8 @@ long engine(int mode, long param_1, long param_2)
>  		LAST_ELEMENT(xt_FORTH_X2d_WORDLIST).a = xt_LASTWORD;
>  
>  		// stack-pointers
> -		dp = the_data_stack - 1;
> -		rp = handler_stack - 1;
> +		dp = (cell *)((type_u)the_data_stack - CELLSIZE);
> +		rp = (cell *)((type_u)handler_stack - CELLSIZE);
>  
>  		// return-address for "evaluate" personality
>  		dummy.a = &&over;

As mentioned in the other e-mail thread already, that solution looks
fine to me.

Reviewed-by: Thomas Huth <thuth@redhat.com>
diff mbox

Patch

diff --git a/slof/paflof.c b/slof/paflof.c
index 50b4adf..e70f601 100644
--- a/slof/paflof.c
+++ b/slof/paflof.c
@@ -81,8 +81,8 @@  long engine(int mode, long param_1, long param_2)
 		LAST_ELEMENT(xt_FORTH_X2d_WORDLIST).a = xt_LASTWORD;
 
 		// stack-pointers
-		dp = the_data_stack - 1;
-		rp = handler_stack - 1;
+		dp = (cell *)((type_u)the_data_stack - CELLSIZE);
+		rp = (cell *)((type_u)handler_stack - CELLSIZE);
 
 		// return-address for "evaluate" personality
 		dummy.a = &&over;