1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-1999
5 * Stuff for implementing proper tail jumps.
7 * ---------------------------------------------------------------------------*/
12 /* -----------------------------------------------------------------------------
13 Unmangled tail-jumping: use the mini interpretter.
14 -------------------------------------------------------------------------- */
16 #ifdef USE_MINIINTERPRETER
18 #define JMP_(cont) return((StgFunPtr)(cont))
24 extern void __DISCARD__(void);
26 /* -----------------------------------------------------------------------------
28 -------------------------------------------------------------------------- */
32 /* Note about discard: possibly there to fool GCC into clearing up
33 before we do the jump eg. if there are some arguments left on the C
34 stack that GCC hasn't popped yet. Also possibly to fool any
35 optimisations (a function call often acts as a barrier). Not sure
36 if any of this is necessary now -- SDM
38 Comment to above note: I don't think the __DISCARD__() in JMP_ is
39 necessary. Arguments should be popped from the C stack immediately
40 after returning from a function, as long as we pass -fno-defer-pop
41 to gcc. Moreover, a goto to a first-class label acts as a barrier
42 for optimisations in the same way a function call does.
46 /* The goto here seems to cause gcc -O2 to delete all the code after
47 it - including the FE_ marker and the epilogue code - exactly what
55 __target = (void *)(cont); \
59 #endif /* i386_HOST_ARCH */
61 /* -----------------------------------------------------------------------------
62 Tail calling on x86_64
63 -------------------------------------------------------------------------- */
68 NOTE about __DISCARD__():
70 On x86_64 this is necessary to work around bugs in the register
71 variable support in gcc. Without the __DISCARD__() call, gcc will
72 silently throw away assignements to global register variables that
73 happen before the jump.
84 without the dummy function call, gcc throws away the assignment to R1
85 (gcc 3.4.3) gcc bug #20359.
91 goto *(void *)(cont); \
94 #endif /* x86_64_HOST_ARCH */
96 /* -----------------------------------------------------------------------------
98 -------------------------------------------------------------------------- */
100 #ifdef sparc_HOST_ARCH
102 #define JMP_(cont) ((F_) (cont))()
103 /* Oh so happily, the above turns into a "call" instruction,
104 which, on a SPARC, is nothing but a "jmpl" with the
105 return address in %o7 [which we don't care about].
108 /* Don't need these for sparc mangling */
112 #endif /* sparc_HOST_ARCH */
114 /* -----------------------------------------------------------------------------
115 Tail calling on Alpha
116 -------------------------------------------------------------------------- */
118 #ifdef alpha_HOST_ARCH
121 register void *_procedure __asm__("$27");
125 do { _procedure = (void *)(cont); \
130 /* Don't need these for alpha mangling */
134 #endif /* alpha_HOST_ARCH */
136 /* -----------------------------------------------------------------------------
139 Description of HP's weird procedure linkage, many thanks to Andy Bennet
140 <andy_bennett@hp.com>:
142 I've been digging a little further into the problem of how HP-UX does
143 dynamic procedure calls. My solution in the last e-mail inserting an extra
144 'if' statement into the JMP_ I think is probably the best general solution I
145 can come up with. There are still a few problems with it however: It wont
146 work, if JMP_ ever has to call anything in a shared library, if this is
147 likely to be required it'll need something more elaborate. It also wont work
148 with PA-RISC 2.0 wide mode (64-bit) which uses a different format PLT.
150 I had some feedback from someone in HP's compiler lab and the problem
151 relates to the linker on HP-UX, not gcc as I first suspected. The reason the
152 'hsc' executable works is most likely due to a change in 'ld's behaviour for
153 performance reasons between your revision and mine.
155 The major issue relating to this is shared libraries and how they are
156 implented under HP-UX. The whole point of the Procedure Label Table (PLT) is
157 to allow a function pointer to hold the address of the function and a
158 pointer to the library's global data lookup table (DLT) used by position
159 independent code (PIC). This makes the PLT absolutely essential for shared
160 library calls. HP has two linker introduced assembly functions for dealing
161 with dynamic calls, $$dyncall and $$dyncall_external. The former does a
162 check to see if the address is a PLT pointer and dereferences if necessary
163 or just calls the address otherwise; the latter skips the check and just
164 does the indirect jump no matter what.
166 Since $$dyncall_external runs faster due to its not having the test, the
167 linker nowadays prefers to generate calls to that, rather than $$dyncall. It
168 makes this decision based on the presence of any shared library. If it even
169 smells an sl's existence at link time, it rigs the runtime system to
170 generate PLT references for everything on the assumption that the result
171 will be slightly more efficient. This is what is crashing GHC since the
172 calls it is generating have no understanding of the procedure label proper.
173 The only way to get real addresses is to link everything archive, including
174 system libraries, at which point it assumes you probably are going to be
175 using calls similar to GHC's (its rigged for HP's +ESfic compiler option)
176 but uses $$dyncall if necessary to cope, just in case you aren't.
178 -------------------------------------------------------------------------- */
180 #ifdef hppa1_1_hp_hpux_TARGET
183 do { void *_procedure = (void *)(cont); \
184 if (((int) _procedure) & 2) \
185 _procedure = (void *)(*((int *) (_procedure - 2))); \
189 #endif /* hppa1_1_hp_hpux_TARGET */
191 /* -----------------------------------------------------------------------------
192 Tail calling on PowerPC
193 -------------------------------------------------------------------------- */
195 #ifdef powerpc_HOST_ARCH
200 target = (void *)(cont); \
206 The __DISCARD__ is there because Apple's April 2002 Beta of GCC 3.1
207 sometimes generates incorrect code otherwise.
208 It tends to "forget" to update global register variables in the presence
209 of decrement/increment operators:
210 JMP_(*(--Sp)) is wrongly compiled as JMP_(Sp[-1]).
211 Calling __DISCARD__ in between works around this problem.
215 I would _love_ to use the following instead,
216 but some versions of Apple's GCC fail to generate code for it
217 if it is called for a casted data pointer - which is exactly what
218 we are going to do...
220 #define JMP_(cont) ((F_) (cont))()
223 #endif /* powerpc_HOST_ARCH */
225 #ifdef powerpc64_HOST_ARCH
226 #define JMP_(cont) ((F_) (cont))()
229 /* -----------------------------------------------------------------------------
231 -------------------------------------------------------------------------- */
233 #ifdef ia64_HOST_ARCH
235 /* The compiler can more intelligently decide how to do this. We therefore
236 * implement it as a call and optimise to a jump at mangle time.
238 * Sometimes GCC likes to move instructions between the function call and
239 * the "--- TAILCALL ---". To stop it from finding instructions to put
240 * there, we insert a jump to the end of the function after the TAILCALL. */
243 __asm__ volatile ("--- TAILCALL ---"); \
246 #define FE_ _function_end: __asm__ volatile ("--- END ---");
248 /* Don't emit calls to __DISCARD__ as this causes hassles */
249 #define __DISCARD__()
253 /* -----------------------------------------------------------------------------
255 -------------------------------------------------------------------------- */
257 #ifdef mips_HOST_ARCH
260 register void *_procedure __asm__("$25");
265 _procedure = (void *)(cont); \
270 /* Don't need these for MIPS mangling */
274 #endif /* mips_HOST_ARCH */
276 /* -----------------------------------------------------------------------------
279 These are markers indicating the start and end of Real Code in a
280 function. All instructions between the actual start and end of the
281 function and these markers is shredded by the mangler.
282 -------------------------------------------------------------------------- */
284 /* The following __DISCARD__() has become necessary with gcc 2.96 on x86.
285 * It prevents gcc from moving stack manipulation code from the function
286 * body (aka the Real Code) into the function prologue, ie, from moving it
287 * over the --- BEGIN --- marker. It should be noted that (like some
288 * other black magic in GHC's code), there is no essential reason why gcc
289 * could not move some stack manipulation code across the __DISCARD__() -
290 * it just doesn't choose to do it at the moment.
295 #define FB_ __asm__ volatile ("--- BEGIN ---"); __DISCARD__ ();
299 #define FE_ __asm__ volatile ("--- END ---");
302 #endif /* !USE_MINIINTERPRETER */
304 #endif /* TAILCALLS_H */