[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / includes / TailCalls.h
1 /* -----------------------------------------------------------------------------
2  * $Id: TailCalls.h,v 1.15 2003/10/12 13:24:52 igloo Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Stuff for implementing proper tail jumps.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef TAILCALLS_H
11 #define TAILCALLS_H
12
13 /* -----------------------------------------------------------------------------
14    Unmangled tail-jumping: use the mini interpretter.
15    -------------------------------------------------------------------------- */
16
17 #ifdef USE_MINIINTERPRETER
18
19 #define JMP_(cont) return(stgCast(StgFunPtr,cont))
20 #define FB_
21 #define FE_
22
23 #else
24
25 extern void __DISCARD__(void);
26
27 /* -----------------------------------------------------------------------------
28    Tail calling on x86
29    -------------------------------------------------------------------------- */
30
31 #if i386_TARGET_ARCH
32
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
38
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. 
44    -= chak
45    */
46
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
49    we want! -- SDM
50    */
51
52 #define JMP_(cont)                      \
53     {                                   \
54       void *__target;                   \
55       __DISCARD__();                    \
56       __target = (void *)(cont);        \
57       goto *__target;                   \
58     }
59
60 #endif /* i386_TARGET_ARCH */
61
62 /* -----------------------------------------------------------------------------
63    Tail calling on x86_64
64    -------------------------------------------------------------------------- */
65
66 #if x86_64_TARGET_ARCH
67
68 #define JMP_(cont)                      \
69     {                                   \
70       void *__target;                   \
71       __target = (void *)(cont);        \
72       goto *__target;                   \
73     }
74
75 #endif /* x86_64_TARGET_ARCH */
76
77 /* -----------------------------------------------------------------------------
78    Tail calling on Sparc
79    -------------------------------------------------------------------------- */
80
81 #ifdef sparc_TARGET_ARCH
82
83 #define JMP_(cont)      ((F_) (cont))()
84         /* Oh so happily, the above turns into a "call" instruction,
85            which, on a SPARC, is nothing but a "jmpl" with the
86            return address in %o7 [which we don't care about].
87         */
88
89 /* Don't need these for sparc mangling */
90 #define FB_
91 #define FE_
92
93 #endif /* sparc_TARGET_ARCH */
94
95 /* -----------------------------------------------------------------------------
96    Tail calling on Alpha
97    -------------------------------------------------------------------------- */
98
99 #ifdef alpha_TARGET_ARCH
100
101 #if IN_STG_CODE
102 register void *_procedure __asm__("$27");
103 #endif
104
105 #define JMP_(cont)                              \
106     do { _procedure = (void *)(cont);           \
107          __DISCARD__();                         \
108          goto *_procedure;                      \
109        } while(0)
110
111 /* Don't need these for alpha mangling */
112 #define FB_
113 #define FE_
114
115 #endif /* alpha_TARGET_ARCH */
116
117 /* -----------------------------------------------------------------------------
118    Tail calling on HP
119
120 Description of HP's weird procedure linkage, many thanks to Andy Bennet
121 <andy_bennett@hp.com>:
122
123 I've been digging a little further into the problem of how HP-UX does
124 dynamic procedure calls. My solution in the last e-mail inserting an extra
125 'if' statement into the JMP_ I think is probably the best general solution I
126 can come up with. There are still a few problems with it however: It wont
127 work, if JMP_ ever has to call anything in a shared library, if this is
128 likely to be required it'll need something more elaborate. It also wont work
129 with PA-RISC 2.0 wide mode (64-bit) which uses a different format PLT.
130
131 I had some feedback from someone in HP's compiler lab and the problem
132 relates to the linker on HP-UX, not gcc as I first suspected. The reason the
133 'hsc' executable works is most likely due to a change in 'ld's behaviour for
134 performance reasons between your revision and mine.
135
136 The major issue relating to this is shared libraries and how they are
137 implented under HP-UX. The whole point of the Procedure Label Table (PLT) is
138 to allow a function pointer to hold the address of the function and a
139 pointer to the library's global data lookup table (DLT) used by position
140 independent code (PIC). This makes the PLT absolutely essential for shared
141 library calls. HP has two linker introduced assembly functions for dealing
142 with dynamic calls, $$dyncall and $$dyncall_external. The former does a
143 check to see if the address is a PLT pointer and dereferences if necessary
144 or just calls the address otherwise; the latter skips the check and just
145 does the indirect jump no matter what.
146
147 Since $$dyncall_external runs faster due to its not having the test, the
148 linker nowadays prefers to generate calls to that, rather than $$dyncall. It
149 makes this decision based on the presence of any shared library. If it even
150 smells an sl's existence at link time, it rigs the runtime system to
151 generate PLT references for everything on the assumption that the result
152 will be slightly more efficient. This is what is crashing GHC since the
153 calls it is generating have no understanding of the procedure label proper.
154 The only way to get real addresses is to link everything archive, including
155 system libraries, at which point it assumes you probably are going to be
156 using calls similar to GHC's (its rigged for HP's +ESfic compiler option)
157 but uses $$dyncall if necessary to cope, just in case you aren't.
158
159    -------------------------------------------------------------------------- */
160
161 #ifdef hppa1_1_hp_hpux_TARGET
162
163 #define JMP_(cont)                              \
164     do { void *_procedure = (void *)(cont);     \
165          if (((int) _procedure) & 2)            \
166             _procedure = (void *)(*((int *) (_procedure - 2))); \
167          goto *_procedure;                      \
168        } while(0)
169
170 #endif /* hppa1_1_hp_hpux_TARGET */
171
172 /* -----------------------------------------------------------------------------
173    Tail calling on PowerPC
174    -------------------------------------------------------------------------- */
175
176 #ifdef powerpc_TARGET_ARCH
177
178 #define JMP_(cont)                      \
179     {                                   \
180       void *target;                     \
181       target = (void *)(cont);          \
182       __DISCARD__();                    \
183       goto *target;                     \
184     }
185
186 /*
187         The __DISCARD__ is there because Apple's April 2002 Beta of GCC 3.1
188         sometimes generates incorrect code otherwise.
189         It tends to "forget" to update global register variables in the presence
190         of decrement/increment operators:
191         JMP_(*(--Sp)) is wrongly compiled as JMP_(Sp[-1]).
192         Calling __DISCARD__ in between works around this problem.
193 */
194
195 /*
196         I would _love_ to use the following instead,
197         but some versions of Apple's GCC fail to generate code for it
198         if it is called for a casted data pointer - which is exactly what
199         we are going to do...
200
201         #define JMP_(cont)      ((F_) (cont))()
202 */
203
204 #endif /* powerpc_TARGET_ARCH */
205
206 /* -----------------------------------------------------------------------------
207    Tail calling on IA64
208    -------------------------------------------------------------------------- */
209
210 #ifdef ia64_TARGET_ARCH
211
212 /* The compiler can more intelligently decide how to do this.  We therefore
213  * implement it as a call and optimise to a jump at mangle time. */
214 #define JMP_(cont)      ((F_) (cont))(); __asm__ volatile ("--- TAILCALL ---");
215
216 /* Don't emit calls to __DISCARD__ as this causes hassles */
217 #define __DISCARD__()
218
219 #endif
220
221 /* -----------------------------------------------------------------------------
222   FUNBEGIN and FUNEND.
223
224   These are markers indicating the start and end of Real Code in a
225   function.  All instructions between the actual start and end of the
226   function and these markers is shredded by the mangler.
227   -------------------------------------------------------------------------- */
228
229 /*  The following __DISCARD__() has become necessary with gcc 2.96 on x86.
230  *  It prevents gcc from moving stack manipulation code from the function
231  *  body (aka the Real Code) into the function prologue, ie, from moving it
232  *  over the --- BEGIN --- marker.  It should be noted that (like some
233  *  other black magic in GHC's code), there is no essential reason why gcc
234  *  could not move some stack manipulation code across the __DISCARD__() -
235  *  it just doesn't choose to do it at the moment.
236  *  -= chak
237  */
238  
239 #ifndef FB_
240 #define FB_    __asm__ volatile ("--- BEGIN ---"); __DISCARD__ ();
241 #endif
242
243 #ifndef FE_
244 #define FE_    __asm__ volatile ("--- END ---");
245 #endif
246
247 #endif /* !USE_MINIINTERPRETER */
248
249 #endif /* TAILCALLS_H */