1 /* -----------------------------------------------------------------------------
2 * $Id: StgCRun.c,v 1.30 2002/02/28 18:45:51 sof Exp $
4 * (c) The GHC Team, 1998-2000
8 * To run an STG function from C land, call
10 * rv = StgRun(f,BaseReg);
12 * where "f" is the STG function to call, and BaseReg is the address of the
13 * RegTable for this run (we might have separate RegTables if we're running
14 * multiple threads on an SMP machine).
16 * In the end, "f" must JMP to StgReturn (defined below),
17 * passing the return-value "rv" in R1,
18 * to return to the caller of StgRun returning "rv" in
19 * the whatever way C returns a value.
21 * NOTE: StgRun/StgReturn do *NOT* load or store Hp or any
22 * other registers (other than saving the C callee-saves
23 * registers). Instead, the called function "f" must do that
26 * GCC will have assumed that pushing/popping of C-stack frames is
27 * going on when it generated its code, and used stack space
28 * accordingly. However, we actually {\em post-process away} all
29 * such stack-framery (see \tr{ghc/driver/ghc-asm.lprl}). Things will
30 * be OK however, if we initially make sure there are
31 * @RESERVED_C_STACK_BYTES@ on the C-stack to begin with, for local
34 * -------------------------------------------------------------------------- */
36 #include "PosixSource.h"
40 * We define the following (unused) global register variables, because for
41 * some reason gcc generates sub-optimal code for StgRun() on the Alpha
42 * (unnecessarily saving extra registers on the stack) if we don't.
44 * Why do it at the top of this file, rather than near StgRun() below? Because
45 * gcc doesn't let us define global register variables after any function
46 * definition has been read. Any point after #include "Stg.h" would be too
49 * We define alpha_EXTRA_CAREFUL here to save $s6, $f8 and $f9 -- registers
50 * that we don't use but which are callee-save registers. The __divq() routine
51 * in libc.a clobbers $s6.
54 #ifdef alpha_TARGET_ARCH
55 #define alpha_EXTRA_CAREFUL
56 register long fake_ra __asm__("$26");
57 #ifdef alpha_EXTRA_CAREFUL
58 register long fake_s6 __asm__("$15");
59 register double fake_f8 __asm__("$f8");
60 register double fake_f9 __asm__("$f9");
64 /* include Stg.h first because we want real machine regs in here: we
65 * have to get the value of R1 back from Stg land to C land intact.
77 #ifdef USE_MINIINTERPRETER
79 /* -----------------------------------------------------------------------------
80 any architecture (using miniinterpreter)
81 -------------------------------------------------------------------------- */
83 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg STG_UNUSED)
87 fprintf(stderr,"Jumping to ");
88 printPtr((P_)f); fflush(stdout);
91 f = (StgFunPtr) (f)();
93 return (StgThreadReturnCode)R1.i;
101 #else /* !USE_MINIINTERPRETER */
103 #ifdef LEADING_UNDERSCORE
104 #define STG_RETURN "_StgReturn"
106 #define STG_RETURN "StgReturn"
109 /* -----------------------------------------------------------------------------
111 -------------------------------------------------------------------------- */
113 #ifdef i386_TARGET_ARCH
116 StgRun(StgFunPtr f, StgRegTable *basereg) {
118 unsigned char space[ RESERVED_C_STACK_BYTES + 4*sizeof(void *) ];
119 StgThreadReturnCode r;
123 * save callee-saves registers on behalf of the STG code.
125 "movl %%esp, %%eax\n\t"
127 "movl %%ebx,0(%%eax)\n\t"
128 "movl %%esi,4(%%eax)\n\t"
129 "movl %%edi,8(%%eax)\n\t"
130 "movl %%ebp,12(%%eax)\n\t"
136 * grab the function argument from the stack, and jump to it.
141 ".global " STG_RETURN "\n"
144 "movl %%esi, %%eax\n\t" /* Return value in R1 */
147 * restore callee-saves registers. (Don't stomp on %%eax!)
149 "movl %%esp, %%edx\n\t"
151 "movl 0(%%edx),%%ebx\n\t" /* restore the registers saved above */
152 "movl 4(%%edx),%%esi\n\t"
153 "movl 8(%%edx),%%edi\n\t"
154 "movl 12(%%edx),%%ebp\n\t"
156 : "=&a" (r), "=m" (space)
157 : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES)
158 : "edx" /* stomps on %edx */
166 /* -----------------------------------------------------------------------------
170 OLD COMMENT from GHC-3.02:
172 We want tailjumps to be calls, because `call xxx' is the only Sparc
173 branch that allows an arbitrary label as a target. (Gcc's ``goto
174 *target'' construct ends up loading the label into a register and
175 then jumping, at the cost of two extra instructions for the 32-bit
178 When entering the threaded world, we stash our return address in a
179 known location so that \tr{%i7} is available as an extra
180 callee-saves register. Of course, we have to restore this when
181 coming out of the threaded world.
183 I hate this god-forsaken architecture. Since the top of the
184 reserved stack space is used for globals and the bottom is reserved
185 for outgoing arguments, we have to stick our return address
186 somewhere in the middle. Currently, I'm allowing 100 extra
187 outgoing arguments beyond the first 6. --JSM
189 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
190 I'm not sure whether we still need to save it. Incedentally, what
191 does the last paragraph above mean when it says "the top of the
192 stack is used for globals"? What globals? --SDM
194 Updated info (GHC 4.08.2): not saving %i7 any more (see below).
195 -------------------------------------------------------------------------- */
197 #ifdef sparc_TARGET_ARCH
200 StgRun(StgFunPtr f, StgRegTable *basereg) {
202 unsigned char space[RESERVED_C_STACK_BYTES];
204 register void *i7 __asm__("%i7");
205 ((void **)(space))[100] = i7;
210 ".global " STG_RETURN "\n"
212 : : : "l0","l1","l2","l3","l4","l5","l6","l7");
213 /* we tell the C compiler that l0-l7 are clobbered on return to
214 * StgReturn, otherwise it tries to use these to save eg. the
215 * address of space[100] across the call. The correct thing
216 * to do would be to save all the callee-saves regs, but we
217 * can't be bothered to do that.
219 * The code that gcc generates for this little fragment is now
220 * terrible. We could do much better by coding it directly in
224 /* updated 4.08.2: we don't save %i7 in the middle of the reserved
225 * space any more, since gcc tries to save its address across the
226 * call to f(), this gets clobbered in STG land and we end up
227 * dereferencing a bogus pointer in StgReturn.
229 __asm__ volatile ("ld %1,%0"
230 : "=r" (i7) : "m" (((void **)(space))[100]));
232 return (StgThreadReturnCode)R1.i;
237 /* -----------------------------------------------------------------------------
240 "The stack pointer (SP) must at all times denote an address that has octaword
241 alignment. (This restriction has the side effect that the in-memory portion
242 of the argument list, if any, will start on an octaword boundary.) Note that
243 the stack grows toward lower addresses. During a procedure invocation, SP
244 can never be set to a value that is higher than the value of SP at entry to
245 that procedure invocation.
247 "The contents of the stack, located above the portion of the argument list
248 (if any) that is passed in memory, belong to the calling procedure. Because
249 they are part of the calling procedure, they should not be read or written
250 by the called procedure, except as specified by indirect arguments or
251 language-controlled up-level references.
253 "The SP value might be used by the hardware when raising exceptions and
254 asynchronous interrupts. It must be assumed that the contents of the stack
255 below the current SP value and within the stack for the current thread are
256 continually and unpredictably modified, as specified in the _Alpha
257 Architecture Reference Manual_, and as a result of asynchronous software
260 -- Compaq Computer Corporation, Houston. Tru64 UNIX Calling Standard for
261 Alpha Systems, 5.1 edition, August 2000, section 3.2.1. http://www.
262 tru64unix.compaq.com/docs/base_doc/DOCUMENTATION/V51_PDF/ARH9MBTE.PDF
263 -------------------------------------------------------------------------- */
265 #ifdef alpha_TARGET_ARCH
268 StgRun(StgFunPtr f, StgRegTable *basereg)
270 register long real_ra __asm__("$26"); volatile long save_ra;
272 register long real_s0 __asm__("$9" ); volatile long save_s0;
273 register long real_s1 __asm__("$10"); volatile long save_s1;
274 register long real_s2 __asm__("$11"); volatile long save_s2;
275 register long real_s3 __asm__("$12"); volatile long save_s3;
276 register long real_s4 __asm__("$13"); volatile long save_s4;
277 register long real_s5 __asm__("$14"); volatile long save_s5;
278 #ifdef alpha_EXTRA_CAREFUL
279 register long real_s6 __asm__("$15"); volatile long save_s6;
282 register double real_f2 __asm__("$f2"); volatile double save_f2;
283 register double real_f3 __asm__("$f3"); volatile double save_f3;
284 register double real_f4 __asm__("$f4"); volatile double save_f4;
285 register double real_f5 __asm__("$f5"); volatile double save_f5;
286 register double real_f6 __asm__("$f6"); volatile double save_f6;
287 register double real_f7 __asm__("$f7"); volatile double save_f7;
288 #ifdef alpha_EXTRA_CAREFUL
289 register double real_f8 __asm__("$f8"); volatile double save_f8;
290 register double real_f9 __asm__("$f9"); volatile double save_f9;
293 register StgFunPtr real_pv __asm__("$27");
295 StgThreadReturnCode ret;
305 #ifdef alpha_EXTRA_CAREFUL
315 #ifdef alpha_EXTRA_CAREFUL
322 __asm__ volatile( "lda $30,-%0($30)" "\n"
323 "\t" "jmp ($27)" "\n"
325 ".globl " STG_RETURN "\n"
327 "\t" "lda $30,%0($30)" "\n"
328 : : "K" (RESERVED_C_STACK_BYTES));
338 #ifdef alpha_EXTRA_CAREFUL
348 #ifdef alpha_EXTRA_CAREFUL
358 #endif /* alpha_TARGET_ARCH */
360 /* -----------------------------------------------------------------------------
362 -------------------------------------------------------------------------- */
364 #ifdef hppa1_1_TARGET_ARCH
367 StgRun(StgFunPtr f, StgRegTable *basereg)
369 StgChar space[RESERVED_C_STACK_BYTES+16*sizeof(long)+10*sizeof(double)];
370 StgThreadReturnCode ret;
372 __asm__ volatile ("ldo %0(%%r30),%%r19\n"
373 "\tstw %%r3, 0(0,%%r19)\n"
374 "\tstw %%r4, 4(0,%%r19)\n"
375 "\tstw %%r5, 8(0,%%r19)\n"
376 "\tstw %%r6,12(0,%%r19)\n"
377 "\tstw %%r7,16(0,%%r19)\n"
378 "\tstw %%r8,20(0,%%r19)\n"
379 "\tstw %%r9,24(0,%%r19)\n"
380 "\tstw %%r10,28(0,%%r19)\n"
381 "\tstw %%r11,32(0,%%r19)\n"
382 "\tstw %%r12,36(0,%%r19)\n"
383 "\tstw %%r13,40(0,%%r19)\n"
384 "\tstw %%r14,44(0,%%r19)\n"
385 "\tstw %%r15,48(0,%%r19)\n"
386 "\tstw %%r16,52(0,%%r19)\n"
387 "\tstw %%r17,56(0,%%r19)\n"
388 "\tstw %%r18,60(0,%%r19)\n"
389 "\tldo 80(%%r19),%%r19\n"
390 "\tfstds %%fr12,-16(0,%%r19)\n"
391 "\tfstds %%fr13, -8(0,%%r19)\n"
392 "\tfstds %%fr14, 0(0,%%r19)\n"
393 "\tfstds %%fr15, 8(0,%%r19)\n"
394 "\tldo 32(%%r19),%%r19\n"
395 "\tfstds %%fr16,-16(0,%%r19)\n"
396 "\tfstds %%fr17, -8(0,%%r19)\n"
397 "\tfstds %%fr18, 0(0,%%r19)\n"
398 "\tfstds %%fr19, 8(0,%%r19)\n"
399 "\tldo 32(%%r19),%%r19\n"
400 "\tfstds %%fr20,-16(0,%%r19)\n"
401 "\tfstds %%fr21, -8(0,%%r19)\n" : :
402 "n" (-(116 * sizeof(long) + 10 * sizeof(double))) : "%r19"
407 __asm__ volatile (".align 4\n"
408 "\t.EXPORT " STG_RETURN ",CODE\n"
409 "\t.EXPORT " STG_RETURN ",ENTRY,PRIV_LEV=3\n"
411 /* "\tldo %0(%%r3),%%r19\n" */
412 "\tldo %1(%%r30),%%r19\n"
413 "\tcopy %%r11, %0\n" /* save R1 */
414 "\tldw 0(0,%%r19),%%r3\n"
415 "\tldw 4(0,%%r19),%%r4\n"
416 "\tldw 8(0,%%r19),%%r5\n"
417 "\tldw 12(0,%%r19),%%r6\n"
418 "\tldw 16(0,%%r19),%%r7\n"
419 "\tldw 20(0,%%r19),%%r8\n"
420 "\tldw 24(0,%%r19),%%r9\n"
421 "\tldw 28(0,%%r19),%%r10\n"
422 "\tldw 32(0,%%r19),%%r11\n"
423 "\tldw 36(0,%%r19),%%r12\n"
424 "\tldw 40(0,%%r19),%%r13\n"
425 "\tldw 44(0,%%r19),%%r14\n"
426 "\tldw 48(0,%%r19),%%r15\n"
427 "\tldw 52(0,%%r19),%%r16\n"
428 "\tldw 56(0,%%r19),%%r17\n"
429 "\tldw 60(0,%%r19),%%r18\n"
430 "\tldo 80(%%r19),%%r19\n"
431 "\tfldds -16(0,%%r19),%%fr12\n"
432 "\tfldds -8(0,%%r19),%%fr13\n"
433 "\tfldds 0(0,%%r19),%%fr14\n"
434 "\tfldds 8(0,%%r19),%%fr15\n"
435 "\tldo 32(%%r19),%%r19\n"
436 "\tfldds -16(0,%%r19),%%fr16\n"
437 "\tfldds -8(0,%%r19),%%fr17\n"
438 "\tfldds 0(0,%%r19),%%fr18\n"
439 "\tfldds 8(0,%%r19),%%fr19\n"
440 "\tldo 32(%%r19),%%r19\n"
441 "\tfldds -16(0,%%r19),%%fr20\n"
442 "\tfldds -8(0,%%r19),%%fr21\n"
444 : "n" (-(116 * sizeof(long) + 10 * sizeof(double)))
451 #endif /* hppa1_1_TARGET_ARCH */
453 #endif /* !USE_MINIINTERPRETER */