X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fincludes%2FTailCalls.h;h=670da9546f75de9df447311bef7f9edbfd293f0e;hb=e6218fe7eff4e34e1a3c823cd4b7aebe09d2d4fb;hp=92d4f72c6b8b71263057160e2a9b677ffd982f74;hpb=4d69b633a31eb2b19141c984ffbe448b74bb5885;p=ghc-hetmet.git diff --git a/ghc/includes/TailCalls.h b/ghc/includes/TailCalls.h index 92d4f72..670da95 100644 --- a/ghc/includes/TailCalls.h +++ b/ghc/includes/TailCalls.h @@ -1,5 +1,4 @@ /* ----------------------------------------------------------------------------- - * $Id: TailCalls.h,v 1.8 2002/03/26 10:30:44 simonmar Exp $ * * (c) The GHC Team, 1998-1999 * @@ -16,7 +15,7 @@ #ifdef USE_MINIINTERPRETER -#define JMP_(cont) return(stgCast(StgFunPtr,cont)) +#define JMP_(cont) return((StgFunPtr)(cont)) #define FB_ #define FE_ @@ -28,7 +27,7 @@ extern void __DISCARD__(void); Tail calling on x86 -------------------------------------------------------------------------- */ -#if i386_TARGET_ARCH +#if i386_HOST_ARCH /* Note about discard: possibly there to fool GCC into clearing up before we do the jump eg. if there are some arguments left on the C @@ -51,19 +50,54 @@ extern void __DISCARD__(void); #define JMP_(cont) \ { \ - void *target; \ + void *__target; \ __DISCARD__(); \ - target = (void *)(cont); \ - goto *target; \ + __target = (void *)(cont); \ + goto *__target; \ + } + +#endif /* i386_HOST_ARCH */ + +/* ----------------------------------------------------------------------------- + Tail calling on x86_64 + -------------------------------------------------------------------------- */ + +#if x86_64_HOST_ARCH + +/* + NOTE about __DISCARD__(): + + On x86_64 this is necessary to work around bugs in the register + variable support in gcc. Without the __DISCARD__() call, gcc will + silently throw away assignements to global register variables that + happen before the jump. + + Here's the example: + + extern void g(void); + static void f(void) { + R1 = g; + __DISCARD__() + goto *R1; + } + + without the dummy function call, gcc throws away the assignment to R1 + (gcc 3.4.3) gcc bug #20359. +*/ + +#define JMP_(cont) \ + { \ + __DISCARD__(); \ + goto *(void *)(cont); \ } -#endif /* i386_TARGET_ARCH */ +#endif /* x86_64_HOST_ARCH */ /* ----------------------------------------------------------------------------- Tail calling on Sparc -------------------------------------------------------------------------- */ -#ifdef sparc_TARGET_ARCH +#ifdef sparc_HOST_ARCH #define JMP_(cont) ((F_) (cont))() /* Oh so happily, the above turns into a "call" instruction, @@ -75,26 +109,29 @@ extern void __DISCARD__(void); #define FB_ #define FE_ -#endif /* sparc_TARGET_ARCH */ +#endif /* sparc_HOST_ARCH */ /* ----------------------------------------------------------------------------- Tail calling on Alpha -------------------------------------------------------------------------- */ -#ifdef alpha_TARGET_ARCH +#ifdef alpha_HOST_ARCH +#if IN_STG_CODE register void *_procedure __asm__("$27"); +#endif -#define JMP_(cont) \ - do { _procedure = (void *)(cont); \ - goto *_procedure; \ +#define JMP_(cont) \ + do { _procedure = (void *)(cont); \ + __DISCARD__(); \ + goto *_procedure; \ } while(0) /* Don't need these for alpha mangling */ #define FB_ #define FE_ -#endif /* alpha_TARGET_ARCH */ +#endif /* alpha_HOST_ARCH */ /* ----------------------------------------------------------------------------- Tail calling on HP @@ -155,25 +192,54 @@ but uses $$dyncall if necessary to cope, just in case you aren't. Tail calling on PowerPC -------------------------------------------------------------------------- */ -#ifdef powerpc_TARGET_ARCH +#ifdef powerpc_HOST_ARCH #define JMP_(cont) \ { \ void *target; \ target = (void *)(cont); \ + __DISCARD__(); \ goto *target; \ } /* + The __DISCARD__ is there because Apple's April 2002 Beta of GCC 3.1 + sometimes generates incorrect code otherwise. + It tends to "forget" to update global register variables in the presence + of decrement/increment operators: + JMP_(*(--Sp)) is wrongly compiled as JMP_(Sp[-1]). + Calling __DISCARD__ in between works around this problem. +*/ + +/* I would _love_ to use the following instead, - but GCC fails to generate code for it if it is called for a casted - data pointer - which is exactly what we are going to do... + but some versions of Apple's GCC fail to generate code for it + if it is called for a casted data pointer - which is exactly what + we are going to do... #define JMP_(cont) ((F_) (cont))() */ +#endif /* powerpc_HOST_ARCH */ -#endif /* sparc_TARGET_ARCH */ +#ifdef powerpc64_HOST_ARCH +#define JMP_(cont) ((F_) (cont))() +#endif + +/* ----------------------------------------------------------------------------- + Tail calling on IA64 + -------------------------------------------------------------------------- */ + +#ifdef ia64_HOST_ARCH + +/* The compiler can more intelligently decide how to do this. We therefore + * implement it as a call and optimise to a jump at mangle time. */ +#define JMP_(cont) ((F_) (cont))(); __asm__ volatile ("--- TAILCALL ---"); + +/* Don't emit calls to __DISCARD__ as this causes hassles */ +#define __DISCARD__() + +#endif /* ----------------------------------------------------------------------------- FUNBEGIN and FUNEND.