1 /* -----------------------------------------------------------------------------
2 * $Id: StgCRun.c,v 1.41 2003/12/10 11:35:26 wolfgang Exp $
4 * (c) The GHC Team, 1998-2003
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 register long fake_gp __asm__("$29");
58 #ifdef alpha_EXTRA_CAREFUL
59 register long fake_s6 __asm__("$15");
60 register double fake_f8 __asm__("$f8");
61 register double fake_f9 __asm__("$f9");
65 /* include Stg.h first because we want real machine regs in here: we
66 * have to get the value of R1 back from Stg land to C land intact.
78 #ifdef USE_MINIINTERPRETER
80 /* -----------------------------------------------------------------------------
81 any architecture (using miniinterpreter)
82 -------------------------------------------------------------------------- */
84 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg STG_UNUSED)
88 fprintf(stderr,"Jumping to ");
89 printPtr((P_)f); fflush(stdout);
92 f = (StgFunPtr) (f)();
94 return (StgThreadReturnCode)R1.i;
102 #else /* !USE_MINIINTERPRETER */
104 #ifdef LEADING_UNDERSCORE
105 #define STG_RETURN "_StgReturn"
107 #define STG_RETURN "StgReturn"
110 /* -----------------------------------------------------------------------------
112 -------------------------------------------------------------------------- */
114 #ifdef i386_TARGET_ARCH
117 StgRun(StgFunPtr f, StgRegTable *basereg) {
119 unsigned char space[ RESERVED_C_STACK_BYTES + 4*sizeof(void *) ];
120 StgThreadReturnCode r;
124 * save callee-saves registers on behalf of the STG code.
126 "movl %%esp, %%eax\n\t"
128 "movl %%ebx,0(%%eax)\n\t"
129 "movl %%esi,4(%%eax)\n\t"
130 "movl %%edi,8(%%eax)\n\t"
131 "movl %%ebp,12(%%eax)\n\t"
137 * grab the function argument from the stack, and jump to it.
142 ".global " STG_RETURN "\n"
145 "movl %%esi, %%eax\n\t" /* Return value in R1 */
148 * restore callee-saves registers. (Don't stomp on %%eax!)
150 "movl %%esp, %%edx\n\t"
152 "movl 0(%%edx),%%ebx\n\t" /* restore the registers saved above */
153 "movl 4(%%edx),%%esi\n\t"
154 "movl 8(%%edx),%%edi\n\t"
155 "movl 12(%%edx),%%ebp\n\t"
157 : "=&a" (r), "=m" (space)
158 : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES)
159 : "edx" /* stomps on %edx */
167 /* ----------------------------------------------------------------------------
168 x86-64 is almost the same as plain x86.
170 I've done it using entirely inline assembler, because I couldn't
171 get gcc to generate the correct subtraction from %rsp by using
172 the local array variable trick. It didn't seem to reserve
173 enough space. Oh well, it's not much harder this way.
175 ------------------------------------------------------------------------- */
177 #ifdef x86_64_TARGET_ARCH
179 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg);
181 static void StgRunIsImplementedInAssembler(void)
185 * save callee-saves registers on behalf of the STG code.
190 "movq %%rsp, %%rax\n\t"
191 "addq %0-48, %%rax\n\t"
192 "movq %%rbx,0(%%rax)\n\t"
193 "movq %%rbp,8(%%rax)\n\t"
194 "movq %%r12,16(%%rax)\n\t"
195 "movq %%r13,24(%%rax)\n\t"
196 "movq %%r14,32(%%rax)\n\t"
197 "movq %%r15,40(%%rax)\n\t"
201 "movq %%rsi,%%rbx\n\t"
203 * grab the function argument from the stack, and jump to it.
205 "movq %%rdi,%%rax\n\t"
208 ".global " STG_RETURN "\n"
211 "movq %%r13, %%rax\n\t" /* Return value in R1 */
214 * restore callee-saves registers. (Don't stomp on %%rax!)
216 "movq %%rsp, %%rdx\n\t"
217 "addq %0-48, %%rdx\n\t"
218 "movq 0(%%rdx),%%rbx\n\t" /* restore the registers saved above */
219 "movq 8(%%rdx),%%rbp\n\t"
220 "movq 16(%%rdx),%%r12\n\t"
221 "movq 24(%%rdx),%%r13\n\t"
222 "movq 32(%%rdx),%%r14\n\t"
223 "movq 40(%%rdx),%%r15\n\t"
227 : : "i"(RESERVED_C_STACK_BYTES+48 /*stack frame size*/));
232 /* -----------------------------------------------------------------------------
236 OLD COMMENT from GHC-3.02:
238 We want tailjumps to be calls, because `call xxx' is the only Sparc
239 branch that allows an arbitrary label as a target. (Gcc's ``goto
240 *target'' construct ends up loading the label into a register and
241 then jumping, at the cost of two extra instructions for the 32-bit
244 When entering the threaded world, we stash our return address in a
245 known location so that \tr{%i7} is available as an extra
246 callee-saves register. Of course, we have to restore this when
247 coming out of the threaded world.
249 I hate this god-forsaken architecture. Since the top of the
250 reserved stack space is used for globals and the bottom is reserved
251 for outgoing arguments, we have to stick our return address
252 somewhere in the middle. Currently, I'm allowing 100 extra
253 outgoing arguments beyond the first 6. --JSM
255 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
256 I'm not sure whether we still need to save it. Incedentally, what
257 does the last paragraph above mean when it says "the top of the
258 stack is used for globals"? What globals? --SDM
260 Updated info (GHC 4.08.2): not saving %i7 any more (see below).
261 -------------------------------------------------------------------------- */
263 #ifdef sparc_TARGET_ARCH
266 StgRun(StgFunPtr f, StgRegTable *basereg) {
268 unsigned char space[RESERVED_C_STACK_BYTES];
270 register void *i7 __asm__("%i7");
271 ((void **)(space))[100] = i7;
276 ".global " STG_RETURN "\n"
278 : : : "l0","l1","l2","l3","l4","l5","l6","l7");
279 /* we tell the C compiler that l0-l7 are clobbered on return to
280 * StgReturn, otherwise it tries to use these to save eg. the
281 * address of space[100] across the call. The correct thing
282 * to do would be to save all the callee-saves regs, but we
283 * can't be bothered to do that.
285 * The code that gcc generates for this little fragment is now
286 * terrible. We could do much better by coding it directly in
290 /* updated 4.08.2: we don't save %i7 in the middle of the reserved
291 * space any more, since gcc tries to save its address across the
292 * call to f(), this gets clobbered in STG land and we end up
293 * dereferencing a bogus pointer in StgReturn.
295 __asm__ volatile ("ld %1,%0"
296 : "=r" (i7) : "m" (((void **)(space))[100]));
298 return (StgThreadReturnCode)R1.i;
303 /* -----------------------------------------------------------------------------
306 "The stack pointer (SP) must at all times denote an address that has octaword
307 alignment. (This restriction has the side effect that the in-memory portion
308 of the argument list, if any, will start on an octaword boundary.) Note that
309 the stack grows toward lower addresses. During a procedure invocation, SP
310 can never be set to a value that is higher than the value of SP at entry to
311 that procedure invocation.
313 "The contents of the stack, located above the portion of the argument list
314 (if any) that is passed in memory, belong to the calling procedure. Because
315 they are part of the calling procedure, they should not be read or written
316 by the called procedure, except as specified by indirect arguments or
317 language-controlled up-level references.
319 "The SP value might be used by the hardware when raising exceptions and
320 asynchronous interrupts. It must be assumed that the contents of the stack
321 below the current SP value and within the stack for the current thread are
322 continually and unpredictably modified, as specified in the _Alpha
323 Architecture Reference Manual_, and as a result of asynchronous software
326 -- Compaq Computer Corporation, Houston. Tru64 UNIX Calling Standard for
327 Alpha Systems, 5.1 edition, August 2000, section 3.2.1. http://www.
328 tru64unix.compaq.com/docs/base_doc/DOCUMENTATION/V51_PDF/ARH9MBTE.PDF
329 -------------------------------------------------------------------------- */
331 #ifdef alpha_TARGET_ARCH
334 StgRun(StgFunPtr f, StgRegTable *basereg)
336 register long real_ra __asm__("$26"); volatile long save_ra;
337 register long real_gp __asm__("$29"); volatile long save_gp;
339 register long real_s0 __asm__("$9" ); volatile long save_s0;
340 register long real_s1 __asm__("$10"); volatile long save_s1;
341 register long real_s2 __asm__("$11"); volatile long save_s2;
342 register long real_s3 __asm__("$12"); volatile long save_s3;
343 register long real_s4 __asm__("$13"); volatile long save_s4;
344 register long real_s5 __asm__("$14"); volatile long save_s5;
345 #ifdef alpha_EXTRA_CAREFUL
346 register long real_s6 __asm__("$15"); volatile long save_s6;
349 register double real_f2 __asm__("$f2"); volatile double save_f2;
350 register double real_f3 __asm__("$f3"); volatile double save_f3;
351 register double real_f4 __asm__("$f4"); volatile double save_f4;
352 register double real_f5 __asm__("$f5"); volatile double save_f5;
353 register double real_f6 __asm__("$f6"); volatile double save_f6;
354 register double real_f7 __asm__("$f7"); volatile double save_f7;
355 #ifdef alpha_EXTRA_CAREFUL
356 register double real_f8 __asm__("$f8"); volatile double save_f8;
357 register double real_f9 __asm__("$f9"); volatile double save_f9;
360 register StgFunPtr real_pv __asm__("$27");
362 StgThreadReturnCode ret;
373 #ifdef alpha_EXTRA_CAREFUL
383 #ifdef alpha_EXTRA_CAREFUL
390 __asm__ volatile( "lda $30,-%0($30)" "\n"
391 "\t" "jmp ($27)" "\n"
393 ".globl " STG_RETURN "\n"
395 "\t" "lda $30,%0($30)" "\n"
396 : : "K" (RESERVED_C_STACK_BYTES));
406 #ifdef alpha_EXTRA_CAREFUL
416 #ifdef alpha_EXTRA_CAREFUL
427 #endif /* alpha_TARGET_ARCH */
429 /* -----------------------------------------------------------------------------
431 -------------------------------------------------------------------------- */
433 #ifdef hppa1_1_TARGET_ARCH
436 StgRun(StgFunPtr f, StgRegTable *basereg)
438 StgChar space[RESERVED_C_STACK_BYTES+16*sizeof(long)+10*sizeof(double)];
439 StgThreadReturnCode ret;
441 __asm__ volatile ("ldo %0(%%r30),%%r19\n"
442 "\tstw %%r3, 0(0,%%r19)\n"
443 "\tstw %%r4, 4(0,%%r19)\n"
444 "\tstw %%r5, 8(0,%%r19)\n"
445 "\tstw %%r6,12(0,%%r19)\n"
446 "\tstw %%r7,16(0,%%r19)\n"
447 "\tstw %%r8,20(0,%%r19)\n"
448 "\tstw %%r9,24(0,%%r19)\n"
449 "\tstw %%r10,28(0,%%r19)\n"
450 "\tstw %%r11,32(0,%%r19)\n"
451 "\tstw %%r12,36(0,%%r19)\n"
452 "\tstw %%r13,40(0,%%r19)\n"
453 "\tstw %%r14,44(0,%%r19)\n"
454 "\tstw %%r15,48(0,%%r19)\n"
455 "\tstw %%r16,52(0,%%r19)\n"
456 "\tstw %%r17,56(0,%%r19)\n"
457 "\tstw %%r18,60(0,%%r19)\n"
458 "\tldo 80(%%r19),%%r19\n"
459 "\tfstds %%fr12,-16(0,%%r19)\n"
460 "\tfstds %%fr13, -8(0,%%r19)\n"
461 "\tfstds %%fr14, 0(0,%%r19)\n"
462 "\tfstds %%fr15, 8(0,%%r19)\n"
463 "\tldo 32(%%r19),%%r19\n"
464 "\tfstds %%fr16,-16(0,%%r19)\n"
465 "\tfstds %%fr17, -8(0,%%r19)\n"
466 "\tfstds %%fr18, 0(0,%%r19)\n"
467 "\tfstds %%fr19, 8(0,%%r19)\n"
468 "\tldo 32(%%r19),%%r19\n"
469 "\tfstds %%fr20,-16(0,%%r19)\n"
470 "\tfstds %%fr21, -8(0,%%r19)\n" : :
471 "n" (-(116 * sizeof(long) + 10 * sizeof(double))) : "%r19"
476 __asm__ volatile (".align 4\n"
477 "\t.EXPORT " STG_RETURN ",CODE\n"
478 "\t.EXPORT " STG_RETURN ",ENTRY,PRIV_LEV=3\n"
480 /* "\tldo %0(%%r3),%%r19\n" */
481 "\tldo %1(%%r30),%%r19\n"
482 "\tcopy %%r11, %0\n" /* save R1 */
483 "\tldw 0(0,%%r19),%%r3\n"
484 "\tldw 4(0,%%r19),%%r4\n"
485 "\tldw 8(0,%%r19),%%r5\n"
486 "\tldw 12(0,%%r19),%%r6\n"
487 "\tldw 16(0,%%r19),%%r7\n"
488 "\tldw 20(0,%%r19),%%r8\n"
489 "\tldw 24(0,%%r19),%%r9\n"
490 "\tldw 28(0,%%r19),%%r10\n"
491 "\tldw 32(0,%%r19),%%r11\n"
492 "\tldw 36(0,%%r19),%%r12\n"
493 "\tldw 40(0,%%r19),%%r13\n"
494 "\tldw 44(0,%%r19),%%r14\n"
495 "\tldw 48(0,%%r19),%%r15\n"
496 "\tldw 52(0,%%r19),%%r16\n"
497 "\tldw 56(0,%%r19),%%r17\n"
498 "\tldw 60(0,%%r19),%%r18\n"
499 "\tldo 80(%%r19),%%r19\n"
500 "\tfldds -16(0,%%r19),%%fr12\n"
501 "\tfldds -8(0,%%r19),%%fr13\n"
502 "\tfldds 0(0,%%r19),%%fr14\n"
503 "\tfldds 8(0,%%r19),%%fr15\n"
504 "\tldo 32(%%r19),%%r19\n"
505 "\tfldds -16(0,%%r19),%%fr16\n"
506 "\tfldds -8(0,%%r19),%%fr17\n"
507 "\tfldds 0(0,%%r19),%%fr18\n"
508 "\tfldds 8(0,%%r19),%%fr19\n"
509 "\tldo 32(%%r19),%%r19\n"
510 "\tfldds -16(0,%%r19),%%fr20\n"
511 "\tfldds -8(0,%%r19),%%fr21\n"
513 : "n" (-(116 * sizeof(long) + 10 * sizeof(double)))
520 #endif /* hppa1_1_TARGET_ARCH */
522 /* -----------------------------------------------------------------------------
525 Everything is in assembler, so we don't have to deal with GCC...
527 -------------------------------------------------------------------------- */
529 #ifdef powerpc_TARGET_ARCH
531 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg);
533 #ifdef darwin_TARGET_OS
534 static void StgRunIsImplementedInAssembler(void)
540 "\tbl saveFP # f14\n"
541 "\tstmw r13,-220(r1)\n"
542 "\tstwu r1,-%0(r1)\n"
546 ".globl _StgReturn\n"
550 "\tlmw r13,-220(r1)\n"
552 : : "i"(RESERVED_C_STACK_BYTES+288 /*stack frame size*/));
556 // This version is for PowerPC Linux.
558 // Differences from the Darwin/Mac OS X version:
559 // *) Different Assembler Syntax
560 // *) Doesn't use Register Saving Helper Functions (although they exist somewhere)
561 // *) We may not access positive stack offsets
562 // (no "Red Zone" as in the Darwin ABI)
563 // *) The Link Register is saved to a different offset in the caller's stack frame
564 // (Linux: 4(r1), Darwin 8(r1))
566 static void StgRunIsImplementedInAssembler(void)
570 "\t.type StgRun,@function\n"
576 "\tstmw 13,-220(5)\n"
577 "\tstfd 14,-144(5)\n"
578 "\tstfd 15,-136(5)\n"
579 "\tstfd 16,-128(5)\n"
580 "\tstfd 17,-120(5)\n"
581 "\tstfd 18,-112(5)\n"
582 "\tstfd 19,-104(5)\n"
599 "\t.type StgReturn,@function\n"
626 : : "i"(RESERVED_C_STACK_BYTES+288 /*stack frame size*/));
632 /* -----------------------------------------------------------------------------
635 Again, in assembler - so we can fiddle with the register stack, and because
636 gcc doesn't handle asm-clobbered callee-saves correctly.
638 loc0 - loc15: preserved locals
639 loc16 - loc28: STG registers
642 loc31: saved gp (gcc 3.3 uses this slot)
643 -------------------------------------------------------------------------- */
645 #ifdef ia64_TARGET_ARCH
647 /* the memory stack is rarely used, so 16K is excessive */
648 #undef RESERVED_C_STACK_BYTES
649 #define RESERVED_C_STACK_BYTES 1024
651 #if ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 3)
652 /* gcc 3.3+: leave an extra slot for gp saves */
658 static void StgRunIsImplementedInAssembler(void)
663 "\talloc loc29 = ar.pfs, 0, %1, 8, 0\n" /* setup register frame */
664 "\tld8 r18 = [r32],8\n" /* get procedure address */
665 "\tadds sp = -%0, sp ;;\n" /* setup stack */
666 "\tld8 gp = [r32]\n" /* get procedure GP */
667 "\tadds r16 = %0-(6*16), sp\n"
668 "\tadds r17 = %0-(5*16), sp ;;\n"
669 "\tstf.spill [r16] = f16,32\n" /* spill callee-saved fp regs */
670 "\tstf.spill [r17] = f17,32\n"
671 "\tmov b6 = r18 ;;\n" /* set target address */
672 "\tstf.spill [r16] = f18,32\n"
673 "\tstf.spill [r17] = f19,32\n"
674 "\tmov loc30 = b0 ;;\n" /* save return address */
675 "\tstf.spill [r16] = f20,32\n"
676 "\tstf.spill [r17] = f21,32\n"
677 "\tbr.few b6 ;;\n" /* branch to function */
678 ".global StgReturn\n"
680 "\tmov r8 = loc16\n" /* return value in r8 */
681 "\tadds r16 = %0-(6*16), sp\n"
682 "\tadds r17 = %0-(5*16), sp ;;\n"
683 "\tldf.fill f16 = [r16],32\n" /* start restoring fp regs */
684 "\tldf.fill f17 = [r17],32\n"
685 "\tmov ar.pfs = loc29 ;;\n" /* restore register frame */
686 "\tldf.fill f18 = [r16],32\n"
687 "\tldf.fill f19 = [r17],32\n"
688 "\tmov b0 = loc30 ;;\n" /* restore return address */
689 "\tldf.fill f20 = [r16],32\n"
690 "\tldf.fill f21 = [r17],32\n"
691 "\tadds sp = %0, sp\n" /* restore stack */
692 "\tbr.ret.sptk.many b0 ;;\n" /* return */
693 : : "i"(RESERVED_C_STACK_BYTES + 6*16), "i"(LOCALS));
698 #endif /* !USE_MINIINTERPRETER */