1 /* -----------------------------------------------------------------------------
2 * $Id: StgCRun.c,v 1.25 2001/08/07 20:06:41 ken 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 * -------------------------------------------------------------------------- */
38 * We define the following (unused) global register variables, because for
39 * some reason gcc generates sub-optimal code for StgRun() on the Alpha
40 * (unnecessarily saving extra registers on the stack) if we don't.
42 * Why do it at the top of this file, rather than near StgRun() below? Because
43 * gcc doesn't let us define global register variables after any function
44 * definition has been read. Any point after #include "Stg.h" would be too
47 * You can define alpha_EXTRA_CAREFUL here to save $s6, $f8 and $f9 -- registers
48 * that we don't use but which are callee-save registers. It shouldn't be
52 #ifdef alpha_TARGET_ARCH
53 #undef alpha_EXTRA_CAREFUL
54 register long fake_ra __asm__("$26");
55 #ifdef alpha_EXTRA_CAREFUL
56 register long fake_s6 __asm__("$15");
57 register double fake_f8 __asm__("$f8");
58 register double fake_f9 __asm__("$f9");
62 /* include Stg.h first because we want real machine regs in here: we
63 * have to get the value of R1 back from Stg land to C land intact.
75 #ifdef USE_MINIINTERPRETER
77 /* -----------------------------------------------------------------------------
78 any architecture (using miniinterpreter)
79 -------------------------------------------------------------------------- */
81 /* The static @jmp_environment@ variable allows @miniInterpret@ to
82 * communicate with @StgReturn@.
84 * Because @StgRun@ may be used recursively, we carefully
85 * save and restore the whole of @jmp_environment@.
88 #include <string.h> /* for memcpy */
90 static jmp_buf jmp_environment;
94 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg)
98 fprintf(stderr,"Jumping to ");
100 fprintf(stderr,"\n");
102 f = (StgFunPtr) (f)();
104 return (StgThreadReturnCode)R1.i;
114 #define CHECK_STACK 0
115 #define STACK_DETAILS 0
117 static int enters = 0;
119 static void scanStackSeg ( W_* ptr, int nwords )
123 int nwords0 = nwords;
128 fprintf ( stderr, "%d",w ); nwords--; ptr++;
129 while (w) { fprintf(stderr, "_"); w--; nwords--; ptr++; }
132 fprintf(stderr, "p");
136 if (nwords < 0) fprintf(stderr, "erk: nwords < 0\n");
138 checkStackChunk ( ptr, ptr-nwords0 );
142 extern StgFunPtr stg_enterStackTop;
143 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg)
151 StgTSO* tso = basereg->rCurrentTSO;
152 StgWord* sb = tso->stack + tso->stack_size;
157 if (f == &stg_enterStackTop) {
167 "== SB = %p SP = %p(%p) SU = %p SpLim = %p(%p)\n",
168 sb, sp, tso->sp, su, basereg->rSpLim, tso->stack + RESERVED_STACK_WORDS);
171 if (su >= sb) goto postloop;
172 if (!sp || !su) goto postloop;
174 printStack ( sp, sb, su);
178 switch (get_itbl((StgClosure*)su)->type) {
182 fprintf(stderr, "S%d ",ws);
183 fprintf(stderr, "\n");
189 fprintf(stderr,"U%d ",ws);
191 sp = su + sizeofW(StgUpdateFrame);
192 su = ((StgUpdateFrame*)su)->link;
197 fprintf(stderr,"Q%d ",ws);
199 sp = su + sizeofW(StgSeqFrame);
200 su = ((StgSeqFrame*)su)->link;
205 fprintf(stderr,"C%d ",ws);
207 sp = su + sizeofW(StgCatchFrame);
208 su = ((StgCatchFrame*)su)->link;
211 fprintf(stderr, "?\nweird record on stack\n");
220 fprintf(stderr,"\n");
223 fprintf(stderr,"-- enter %p ", f);
224 nm = nameFromOPtr ( f );
225 if (nm) fprintf(stderr, "%s", nm); else
227 fprintf ( stderr, "\n");
230 fprintf(stderr,"\n");
233 if (enters % 1000 == 0) fprintf(stderr, "%d enters\n",enters);
235 f = (StgFunPtr) (f)();
238 fprintf (stderr, "miniInterpreter: bye!\n\n" );
239 return (StgThreadReturnCode)R1.i;
250 #else /* !USE_MINIINTERPRETER */
252 #ifdef LEADING_UNDERSCORE
253 #define STG_RETURN "_StgReturn"
255 #define STG_RETURN "StgReturn"
258 /* -----------------------------------------------------------------------------
260 -------------------------------------------------------------------------- */
262 #ifdef i386_TARGET_ARCH
265 StgRun(StgFunPtr f, StgRegTable *basereg) {
267 unsigned char space[ RESERVED_C_STACK_BYTES + 4*sizeof(void *) ];
268 StgThreadReturnCode r;
272 * save callee-saves registers on behalf of the STG code.
274 "movl %%esp, %%eax\n\t"
276 "movl %%ebx,0(%%eax)\n\t"
277 "movl %%esi,4(%%eax)\n\t"
278 "movl %%edi,8(%%eax)\n\t"
279 "movl %%ebp,12(%%eax)\n\t"
285 * grab the function argument from the stack, and jump to it.
290 ".global " STG_RETURN "\n"
293 "movl %%esi, %%eax\n\t" /* Return value in R1 */
296 * restore callee-saves registers. (Don't stomp on %%eax!)
298 "movl %%esp, %%edx\n\t"
300 "movl 0(%%edx),%%ebx\n\t" /* restore the registers saved above */
301 "movl 4(%%edx),%%esi\n\t"
302 "movl 8(%%edx),%%edi\n\t"
303 "movl 12(%%edx),%%ebp\n\t"
305 : "=&a" (r), "=m" (space)
306 : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES)
307 : "edx" /* stomps on %edx */
315 /* -----------------------------------------------------------------------------
319 OLD COMMENT from GHC-3.02:
321 We want tailjumps to be calls, because `call xxx' is the only Sparc
322 branch that allows an arbitrary label as a target. (Gcc's ``goto
323 *target'' construct ends up loading the label into a register and
324 then jumping, at the cost of two extra instructions for the 32-bit
327 When entering the threaded world, we stash our return address in a
328 known location so that \tr{%i7} is available as an extra
329 callee-saves register. Of course, we have to restore this when
330 coming out of the threaded world.
332 I hate this god-forsaken architecture. Since the top of the
333 reserved stack space is used for globals and the bottom is reserved
334 for outgoing arguments, we have to stick our return address
335 somewhere in the middle. Currently, I'm allowing 100 extra
336 outgoing arguments beyond the first 6. --JSM
338 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
339 I'm not sure whether we still need to save it. Incedentally, what
340 does the last paragraph above mean when it says "the top of the
341 stack is used for globals"? What globals? --SDM
343 Updated info (GHC 4.08.2): not saving %i7 any more (see below).
344 -------------------------------------------------------------------------- */
346 #ifdef sparc_TARGET_ARCH
349 StgRun(StgFunPtr f, StgRegTable *basereg) {
351 unsigned char space[RESERVED_C_STACK_BYTES];
353 register void *i7 __asm__("%i7");
354 ((void **)(space))[100] = i7;
359 ".global " STG_RETURN "\n"
361 : : : "l0","l1","l2","l3","l4","l5","l6","l7");
362 /* we tell the C compiler that l0-l7 are clobbered on return to
363 * StgReturn, otherwise it tries to use these to save eg. the
364 * address of space[100] across the call. The correct thing
365 * to do would be to save all the callee-saves regs, but we
366 * can't be bothered to do that.
368 * The code that gcc generates for this little fragment is now
369 * terrible. We could do much better by coding it directly in
373 /* updated 4.08.2: we don't save %i7 in the middle of the reserved
374 * space any more, since gcc tries to save its address across the
375 * call to f(), this gets clobbered in STG land and we end up
376 * dereferencing a bogus pointer in StgReturn.
378 __asm__ volatile ("ld %1,%0"
379 : "=r" (i7) : "m" (((void **)(space))[100]));
381 return (StgThreadReturnCode)R1.i;
386 /* -----------------------------------------------------------------------------
389 "The stack pointer (SP) must at all times denote an address that has octaword
390 alignment. (This restriction has the side effect that the in-memory portion
391 of the argument list, if any, will start on an octaword boundary.) Note that
392 the stack grows toward lower addresses. During a procedure invocation, SP
393 can never be set to a value that is higher than the value of SP at entry to
394 that procedure invocation.
396 "The contents of the stack, located above the portion of the argument list
397 (if any) that is passed in memory, belong to the calling procedure. Because
398 they are part of the calling procedure, they should not be read or written
399 by the called procedure, except as specified by indirect arguments or
400 language-controlled up-level references.
402 "The SP value might be used by the hardware when raising exceptions and
403 asynchronous interrupts. It must be assumed that the contents of the stack
404 below the current SP value and within the stack for the current thread are
405 continually and unpredictably modified, as specified in the _Alpha
406 Architecture Reference Manual_, and as a result of asynchronous software
409 -- Compaq Computer Corporation, Houston. Tru64 UNIX Calling Standard for
410 Alpha Systems, 5.1 edition, August 2000, section 3.2.1. http://www.
411 tru64unix.compaq.com/docs/base_doc/DOCUMENTATION/V51_PDF/ARH9MBTE.PDF
412 -------------------------------------------------------------------------- */
414 #ifdef alpha_TARGET_ARCH
417 StgRun(StgFunPtr f, StgRegTable *basereg)
419 register long real_ra __asm__("$26"); volatile long save_ra;
421 register long real_s0 __asm__("$9" ); volatile long save_s0;
422 register long real_s1 __asm__("$10"); volatile long save_s1;
423 register long real_s2 __asm__("$11"); volatile long save_s2;
424 register long real_s3 __asm__("$12"); volatile long save_s3;
425 register long real_s4 __asm__("$13"); volatile long save_s4;
426 register long real_s5 __asm__("$14"); volatile long save_s5;
427 #ifdef alpha_EXTRA_CAREFUL
428 register long real_s6 __asm__("$15"); volatile long save_s6;
431 register double real_f2 __asm__("$f2"); volatile double save_f2;
432 register double real_f3 __asm__("$f3"); volatile double save_f3;
433 register double real_f4 __asm__("$f4"); volatile double save_f4;
434 register double real_f5 __asm__("$f5"); volatile double save_f5;
435 register double real_f6 __asm__("$f6"); volatile double save_f6;
436 register double real_f7 __asm__("$f7"); volatile double save_f7;
437 #ifdef alpha_EXTRA_CAREFUL
438 register double real_f8 __asm__("$f8"); volatile double save_f8;
439 register double real_f9 __asm__("$f9"); volatile double save_f9;
442 register StgFunPtr real_pv __asm__("$27");
444 StgThreadReturnCode ret;
454 #ifdef alpha_EXTRA_CAREFUL
464 #ifdef alpha_EXTRA_CAREFUL
471 __asm__ volatile( "lda $30,-%0($30)" "\n"
472 "\t" "jmp ($27)" "\n"
474 ".globl " STG_RETURN "\n"
476 "\t" "lda $30,%0($30)" "\n"
477 : : "K" (RESERVED_C_STACK_BYTES));
487 #ifdef alpha_EXTRA_CAREFUL
497 #ifdef alpha_EXTRA_CAREFUL
507 #endif /* alpha_TARGET_ARCH */
509 /* -----------------------------------------------------------------------------
511 -------------------------------------------------------------------------- */
513 #ifdef hppa1_1_TARGET_ARCH
516 StgRun(StgFunPtr f, StgRegTable *basereg)
518 StgChar space[RESERVED_C_STACK_BYTES+16*sizeof(long)+10*sizeof(double)];
519 StgThreadReturnCode ret;
521 __asm__ volatile ("ldo %0(%%r30),%%r19\n"
522 "\tstw %%r3, 0(0,%%r19)\n"
523 "\tstw %%r4, 4(0,%%r19)\n"
524 "\tstw %%r5, 8(0,%%r19)\n"
525 "\tstw %%r6,12(0,%%r19)\n"
526 "\tstw %%r7,16(0,%%r19)\n"
527 "\tstw %%r8,20(0,%%r19)\n"
528 "\tstw %%r9,24(0,%%r19)\n"
529 "\tstw %%r10,28(0,%%r19)\n"
530 "\tstw %%r11,32(0,%%r19)\n"
531 "\tstw %%r12,36(0,%%r19)\n"
532 "\tstw %%r13,40(0,%%r19)\n"
533 "\tstw %%r14,44(0,%%r19)\n"
534 "\tstw %%r15,48(0,%%r19)\n"
535 "\tstw %%r16,52(0,%%r19)\n"
536 "\tstw %%r17,56(0,%%r19)\n"
537 "\tstw %%r18,60(0,%%r19)\n"
538 "\tldo 80(%%r19),%%r19\n"
539 "\tfstds %%fr12,-16(0,%%r19)\n"
540 "\tfstds %%fr13, -8(0,%%r19)\n"
541 "\tfstds %%fr14, 0(0,%%r19)\n"
542 "\tfstds %%fr15, 8(0,%%r19)\n"
543 "\tldo 32(%%r19),%%r19\n"
544 "\tfstds %%fr16,-16(0,%%r19)\n"
545 "\tfstds %%fr17, -8(0,%%r19)\n"
546 "\tfstds %%fr18, 0(0,%%r19)\n"
547 "\tfstds %%fr19, 8(0,%%r19)\n"
548 "\tldo 32(%%r19),%%r19\n"
549 "\tfstds %%fr20,-16(0,%%r19)\n"
550 "\tfstds %%fr21, -8(0,%%r19)\n" : :
551 "n" (-(116 * sizeof(long) + 10 * sizeof(double))) : "%r19"
556 __asm__ volatile (".align 4\n"
557 "\t.EXPORT " STG_RETURN ",CODE\n"
558 "\t.EXPORT " STG_RETURN ",ENTRY,PRIV_LEV=3\n"
560 /* "\tldo %0(%%r3),%%r19\n" */
561 "\tldo %1(%%r30),%%r19\n"
562 "\tcopy %%r11, %0\n" /* save R1 */
563 "\tldw 0(0,%%r19),%%r3\n"
564 "\tldw 4(0,%%r19),%%r4\n"
565 "\tldw 8(0,%%r19),%%r5\n"
566 "\tldw 12(0,%%r19),%%r6\n"
567 "\tldw 16(0,%%r19),%%r7\n"
568 "\tldw 20(0,%%r19),%%r8\n"
569 "\tldw 24(0,%%r19),%%r9\n"
570 "\tldw 28(0,%%r19),%%r10\n"
571 "\tldw 32(0,%%r19),%%r11\n"
572 "\tldw 36(0,%%r19),%%r12\n"
573 "\tldw 40(0,%%r19),%%r13\n"
574 "\tldw 44(0,%%r19),%%r14\n"
575 "\tldw 48(0,%%r19),%%r15\n"
576 "\tldw 52(0,%%r19),%%r16\n"
577 "\tldw 56(0,%%r19),%%r17\n"
578 "\tldw 60(0,%%r19),%%r18\n"
579 "\tldo 80(%%r19),%%r19\n"
580 "\tfldds -16(0,%%r19),%%fr12\n"
581 "\tfldds -8(0,%%r19),%%fr13\n"
582 "\tfldds 0(0,%%r19),%%fr14\n"
583 "\tfldds 8(0,%%r19),%%fr15\n"
584 "\tldo 32(%%r19),%%r19\n"
585 "\tfldds -16(0,%%r19),%%fr16\n"
586 "\tfldds -8(0,%%r19),%%fr17\n"
587 "\tfldds 0(0,%%r19),%%fr18\n"
588 "\tfldds 8(0,%%r19),%%fr19\n"
589 "\tldo 32(%%r19),%%r19\n"
590 "\tfldds -16(0,%%r19),%%fr20\n"
591 "\tfldds -8(0,%%r19),%%fr21\n"
593 : "n" (-(116 * sizeof(long) + 10 * sizeof(double)))
600 #endif /* hppa1_1_TARGET_ARCH */
602 #endif /* !USE_MINIINTERPRETER */