1 /* -----------------------------------------------------------------------------
2 * $Id: TailCalls.h,v 1.12 2003/01/06 13:11:26 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * Stuff for implementing proper tail jumps.
8 * ---------------------------------------------------------------------------*/
13 /* -----------------------------------------------------------------------------
14 Unmangled tail-jumping: use the mini interpretter.
15 -------------------------------------------------------------------------- */
17 #ifdef USE_MINIINTERPRETER
19 #define JMP_(cont) return(stgCast(StgFunPtr,cont))
25 extern void __DISCARD__(void);
27 /* -----------------------------------------------------------------------------
29 -------------------------------------------------------------------------- */
33 /* Note about discard: possibly there to fool GCC into clearing up
34 before we do the jump eg. if there are some arguments left on the C
35 stack that GCC hasn't popped yet. Also possibly to fool any
36 optimisations (a function call often acts as a barrier). Not sure
37 if any of this is necessary now -- SDM
39 Comment to above note: I don't think the __DISCARD__() in JMP_ is
40 necessary. Arguments should be popped from the C stack immediately
41 after returning from a function, as long as we pass -fno-defer-pop
42 to gcc. Moreover, a goto to a first-class label acts as a barrier
43 for optimisations in the same way a function call does.
47 /* The goto here seems to cause gcc -O2 to delete all the code after
48 it - including the FE_ marker and the epilogue code - exactly what
56 __target = (void *)(cont); \
60 #endif /* i386_TARGET_ARCH */
62 /* -----------------------------------------------------------------------------
64 -------------------------------------------------------------------------- */
66 #ifdef sparc_TARGET_ARCH
68 #define JMP_(cont) ((F_) (cont))()
69 /* Oh so happily, the above turns into a "call" instruction,
70 which, on a SPARC, is nothing but a "jmpl" with the
71 return address in %o7 [which we don't care about].
74 /* Don't need these for sparc mangling */
78 #endif /* sparc_TARGET_ARCH */
80 /* -----------------------------------------------------------------------------
82 -------------------------------------------------------------------------- */
84 #ifdef alpha_TARGET_ARCH
86 register void *_procedure __asm__("$27");
89 do { _procedure = (void *)(cont); \
94 /* Don't need these for alpha mangling */
98 #endif /* alpha_TARGET_ARCH */
100 /* -----------------------------------------------------------------------------
103 Description of HP's weird procedure linkage, many thanks to Andy Bennet
104 <andy_bennett@hp.com>:
106 I've been digging a little further into the problem of how HP-UX does
107 dynamic procedure calls. My solution in the last e-mail inserting an extra
108 'if' statement into the JMP_ I think is probably the best general solution I
109 can come up with. There are still a few problems with it however: It wont
110 work, if JMP_ ever has to call anything in a shared library, if this is
111 likely to be required it'll need something more elaborate. It also wont work
112 with PA-RISC 2.0 wide mode (64-bit) which uses a different format PLT.
114 I had some feedback from someone in HP's compiler lab and the problem
115 relates to the linker on HP-UX, not gcc as I first suspected. The reason the
116 'hsc' executable works is most likely due to a change in 'ld's behaviour for
117 performance reasons between your revision and mine.
119 The major issue relating to this is shared libraries and how they are
120 implented under HP-UX. The whole point of the Procedure Label Table (PLT) is
121 to allow a function pointer to hold the address of the function and a
122 pointer to the library's global data lookup table (DLT) used by position
123 independent code (PIC). This makes the PLT absolutely essential for shared
124 library calls. HP has two linker introduced assembly functions for dealing
125 with dynamic calls, $$dyncall and $$dyncall_external. The former does a
126 check to see if the address is a PLT pointer and dereferences if necessary
127 or just calls the address otherwise; the latter skips the check and just
128 does the indirect jump no matter what.
130 Since $$dyncall_external runs faster due to its not having the test, the
131 linker nowadays prefers to generate calls to that, rather than $$dyncall. It
132 makes this decision based on the presence of any shared library. If it even
133 smells an sl's existence at link time, it rigs the runtime system to
134 generate PLT references for everything on the assumption that the result
135 will be slightly more efficient. This is what is crashing GHC since the
136 calls it is generating have no understanding of the procedure label proper.
137 The only way to get real addresses is to link everything archive, including
138 system libraries, at which point it assumes you probably are going to be
139 using calls similar to GHC's (its rigged for HP's +ESfic compiler option)
140 but uses $$dyncall if necessary to cope, just in case you aren't.
142 -------------------------------------------------------------------------- */
144 #ifdef hppa1_1_hp_hpux_TARGET
147 do { void *_procedure = (void *)(cont); \
148 if (((int) _procedure) & 2) \
149 _procedure = (void *)(*((int *) (_procedure - 2))); \
153 #endif /* hppa1_1_hp_hpux_TARGET */
155 /* -----------------------------------------------------------------------------
156 Tail calling on PowerPC
157 -------------------------------------------------------------------------- */
159 #ifdef powerpc_TARGET_ARCH
164 target = (void *)(cont); \
170 The __DISCARD__ is there because Apple's April 2002 Beta of GCC 3.1
171 sometimes generates incorrect code otherwise.
172 It tends to "forget" to update global register variables in the presence
173 of decrement/increment operators:
174 JMP_(*(--Sp)) is wrongly compiled as JMP_(Sp[-1]).
175 Calling __DISCARD__ in between works around this problem.
179 I would _love_ to use the following instead,
180 but some versions of Apple's GCC fail to generate code for it
181 if it is called for a casted data pointer - which is exactly what
182 we are going to do...
184 #define JMP_(cont) ((F_) (cont))()
187 #endif /* powerpc_TARGET_ARCH */
189 /* -----------------------------------------------------------------------------
191 -------------------------------------------------------------------------- */
193 #ifdef ia64_TARGET_ARCH
195 /* The compiler can more intelligently decide how to do this. We therefore
196 * implement it as a call and optimise to a jump at mangle time. */
197 #define JMP_(cont) ((F_) (cont))(); __asm__ volatile ("--- TAILCALL ---");
199 /* Don't emit calls to __DISCARD__ as this causes hassles */
200 #define __DISCARD__()
204 /* -----------------------------------------------------------------------------
207 These are markers indicating the start and end of Real Code in a
208 function. All instructions between the actual start and end of the
209 function and these markers is shredded by the mangler.
210 -------------------------------------------------------------------------- */
212 /* The following __DISCARD__() has become necessary with gcc 2.96 on x86.
213 * It prevents gcc from moving stack manipulation code from the function
214 * body (aka the Real Code) into the function prologue, ie, from moving it
215 * over the --- BEGIN --- marker. It should be noted that (like some
216 * other black magic in GHC's code), there is no essential reason why gcc
217 * could not move some stack manipulation code across the __DISCARD__() -
218 * it just doesn't choose to do it at the moment.
223 #define FB_ __asm__ volatile ("--- BEGIN ---"); __DISCARD__ ();
227 #define FE_ __asm__ volatile ("--- END ---");
230 #endif /* !USE_MINIINTERPRETER */
232 #endif /* TAILCALLS_H */