SH Target & sp_switch

Tony Bryant
Wed Sep 5 04:38:00 GMT 2001

"sp_switch" is a SH target specific function attribute that causes a load to
SP on entry to a function (and restore on exit). It allow a function to run
with a different stack. A godsend on the SH2, where you only have a small
amount of fast internal ram.

Except that it doesn't work.

#1. The optimizer will gladly move the SP load and restore code to a random
place in the function. Which is of course a problem, it needs to be exactly
the first and final (except the RTE) in the function.

I've fixed this one, by changing the machine description. I'll submit that fix
to someone "in the loop" eventually.

#2. How is it supposed to work anyway? Here's an example:

// C code input

volatile int x;

#pragma interrupt
void timerinterrupt() 	__attribute__ ((sp_switch ("blah")));
void timerinterrupt()

// result of "cc1 -S -Os test.c"

	.file	"test.c"
	.align 1
	.global	_timerinterrupt
	.type	_timerinterrupt,@function
	mov.l r0,@-r15
	mov.l #_blah,r0
	mov.l @r0,r0
	mov.l r15,@-r0
	mov r0,r15
	mov.l	r1,@-r15
	mov.l	r2,@-r15
	mov.l	.L4,r2
	mov.l	@r2,r1
	mov.l	r14,@-r15
	add	#1,r1
	mov.l	r1,@r2
	mov	r15,r14
	mov	r14,r15
	mov.l	@r15+,r14
	mov.l	@r15+,r2
	mov.l	@r15+,r1
	mov.l @r15+,r15
	mov.l @r15+,r0
	.align 2
	.long	_x
	.size	_timerinterrupt,.Lfe1-_timerinterrupt
	.comm	_x,4,4
	.ident	"GCC: (GNU) 3.0"

The BIG question I have is what was the writer of this feature expecting
"blah" to be? Considering the SH can only load constants of values -128 to
127? So if "blah" is a global variable it needs to be in this range. Unlikely 
I think.

Can any one think of anything workable to put in place of "blah" here?

I'm seriously considering changing the definition to remove the #, thus
allowing "blah" be a label close somewhere nearby. 

But where?

More information about the Gcc-bugs mailing list