X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Frts%2FStgCRun.c;h=09a8016afeb19bcb479321091926378735b5d804;hb=066dbe7804d1e0ca710d996f43628e88e0321be6;hp=c2eb50f9011ca6d8987b9aa428a110e9be3e5586;hpb=3d4dba8892d9d17e80cf7f4c76e61267876ab8f0;p=ghc-hetmet.git diff --git a/ghc/rts/StgCRun.c b/ghc/rts/StgCRun.c index c2eb50f..09a8016 100644 --- a/ghc/rts/StgCRun.c +++ b/ghc/rts/StgCRun.c @@ -1,10 +1,35 @@ /* ----------------------------------------------------------------------------- - * $Id: StgCRun.c,v 1.12 2000/02/24 14:05:55 sewardj Exp $ + * $Id: StgCRun.c,v 1.18 2000/04/17 14:46:31 sewardj Exp $ * - * (c) The GHC Team, 1998-1999 + * (c) The GHC Team, 1998-2000 * - * STG-to-C glue. Some architectures have this code written in - * straight assembler (see StgRun.S), some in C. + * STG-to-C glue. + * + * To run an STG function from C land, call + * + * rv = StgRun(f,BaseReg); + * + * where "f" is the STG function to call, and BaseReg is the address of the + * RegTable for this run (we might have separate RegTables if we're running + * multiple threads on an SMP machine). + * + * In the end, "f" must JMP to StgReturn (defined below), + * passing the return-value "rv" in R1, + * to return to the caller of StgRun returning "rv" in + * the whatever way C returns a value. + * + * NOTE: StgRun/StgReturn do *NOT* load or store Hp or any + * other registers (other than saving the C callee-saves + * registers). Instead, the called function "f" must do that + * in STG land. + * + * GCC will have assumed that pushing/popping of C-stack frames is + * going on when it generated its code, and used stack space + * accordingly. However, we actually {\em post-process away} all + * such stack-framery (see \tr{ghc/driver/ghc-asm.lprl}). Things will + * be OK however, if we initially make sure there are + * @RESERVED_C_STACK_BYTES@ on the C-stack to begin with, for local + * variables. * * -------------------------------------------------------------------------- */ @@ -42,36 +67,35 @@ static jmp_buf jmp_environment; extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg) { - jmp_buf save_buf; - /* Save jmp_environment for previous call to miniInterpret */ - memcpy((void *) jmp_environment, (void *) save_buf, sizeof(jmp_buf)); - if (setjmp(jmp_environment) == 0) { - while ( 1 ) { - IF_DEBUG(evaluator, - fprintf(stderr,"Jumping to "); - printPtr((P_)f); - fprintf(stderr,"\n"); - ); - f = (StgFunPtr) (f)(); - } - } - /* Restore jmp_environment for previous call */ - memcpy((void*) save_buf, (void*) jmp_environment, sizeof(jmp_buf)); - - return (StgThreadReturnCode)R1.i; + while (f) { + IF_DEBUG(evaluator, + fprintf(stderr,"Jumping to "); + printPtr((P_)f); + fprintf(stderr,"\n"); + ); + f = (StgFunPtr) (f)(); + } + return (StgThreadReturnCode)R1.i; } EXTFUN(StgReturn) { - longjmp(jmp_environment, 1); + return 0; } #else +#define CHECK_STACK 0 +#define STACK_DETAILS 0 + +static int enters = 0; + static void scanStackSeg ( W_* ptr, int nwords ) { W_ w; +#if CHECK_STACK int nwords0 = nwords; +#if STACK_DETAILS while (nwords > 0) { w = *ptr; if (IS_ARG_TAG(w)) { @@ -84,80 +108,104 @@ static void scanStackSeg ( W_* ptr, int nwords ) } } if (nwords < 0) fprintf(stderr, "erk: nwords < 0\n"); +#endif checkStackChunk ( ptr, ptr-nwords0 ); +#endif } - +extern StgFunPtr stg_enterStackTop; extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg) { char* nm; while (1) { -#define STACK_DETAILS 0 - -#if STACK_DETAILS +#if CHECK_STACK { int i; - StgWord* sp = basereg->rSp; - StgWord* su = basereg->rSu; StgTSO* tso = basereg->rCurrentTSO; StgWord* sb = tso->stack + tso->stack_size; + StgWord* sp; + StgWord* su; int ws; - fprintf(stderr, "== SP = %p SU = %p\n", sp,su); + if (f == &stg_enterStackTop) { + sp = tso->sp; + su = tso->su; + } else { + sp = basereg->rSp; + su = basereg->rSu; + } + +#if STACK_DETAILS + fprintf(stderr, + "== SB = %p SP = %p(%p) SU = %p SpLim = %p(%p)\n", + sb, sp, tso->sp, su, basereg->rSpLim, tso->splim); +#endif if (su >= sb) goto postloop; if (!sp || !su) goto postloop; - //printStack ( sp, sb, su); + printStack ( sp, sb, su); while (1) { ws = su - sp; switch (get_itbl((StgClosure*)su)->type) { case STOP_FRAME: scanStackSeg(sp,ws); +#if STACK_DETAILS fprintf(stderr, "S%d ",ws); fprintf(stderr, "\n"); +#endif goto postloop; case UPDATE_FRAME: scanStackSeg(sp,ws); +#if STACK_DETAILS fprintf(stderr,"U%d ",ws); +#endif sp = su + sizeofW(StgUpdateFrame); su = ((StgUpdateFrame*)su)->link; break; case SEQ_FRAME: scanStackSeg(sp,ws); +#if STACK_DETAILS fprintf(stderr,"Q%d ",ws); +#endif sp = su + sizeofW(StgSeqFrame); su = ((StgSeqFrame*)su)->link; break; case CATCH_FRAME: scanStackSeg(sp,ws); +#if STACK_DETAILS fprintf(stderr,"C%d ",ws); +#endif sp = su + sizeofW(StgCatchFrame); su = ((StgCatchFrame*)su)->link; break; default: fprintf(stderr, "?\nweird record on stack\n"); + assert(0); goto postloop; } } postloop: } -#endif - +#endif #if STACK_DETAILS fprintf(stderr,"\n"); #endif - fprintf(stderr,"-- enter: "); +#if 1 + fprintf(stderr,"-- enter %p ", f); nm = nameFromOPtr ( f ); - if (nm) - fprintf(stderr, "%s (%p)", nm, f); else - printPtr((P_)f); + if (nm) fprintf(stderr, "%s", nm); else + printPtr((P_)f); fprintf ( stderr, "\n"); +#endif #if STACK_DETAILS fprintf(stderr,"\n"); #endif + zzz: + if (enters % 1000 == 0) fprintf(stderr, "%d enters\n",enters); + enters++; f = (StgFunPtr) (f)(); if (!f) break; } @@ -182,7 +230,90 @@ EXTFUN(StgReturn) #endif /* ----------------------------------------------------------------------------- - sparc architecture + x86 architecture + -------------------------------------------------------------------------- */ + +#ifdef i386_TARGET_ARCH + +StgThreadReturnCode +StgRun(StgFunPtr f, StgRegTable *basereg) { + + StgChar space[ RESERVED_C_STACK_BYTES + 4*sizeof(void *) ]; + StgThreadReturnCode r; + + __asm__ volatile ( + /* + * save callee-saves registers on behalf of the STG code. + */ + "movl %%esp, %%eax\n\t" + "addl %4, %%eax\n\t" + "movl %%ebx,0(%%eax)\n\t" + "movl %%esi,4(%%eax)\n\t" + "movl %%edi,8(%%eax)\n\t" + "movl %%ebp,12(%%eax)\n\t" + /* + * Set BaseReg + */ + "movl %3,%%ebx\n\t" + /* + * grab the function argument from the stack, and jump to it. + */ + "movl %2,%%eax\n\t" + "jmp *%%eax\n\t" + + ".global " STG_RETURN "\n" + STG_RETURN ":\n\t" + + "movl %%esi, %%eax\n\t" /* Return value in R1 */ + + /* + * restore callee-saves registers. (Don't stomp on %%eax!) + */ + "movl %%esp, %%edx\n\t" + "addl %4, %%edx\n\t" + "movl 0(%%edx),%%ebx\n\t" /* restore the registers saved above */ + "movl 4(%%edx),%%esi\n\t" + "movl 8(%%edx),%%edi\n\t" + "movl 12(%%edx),%%ebp\n\t" + + : "=&a" (r), "=m" (space) + : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES) + : "edx" /* stomps on %edx */ + ); + + return r; +} + +#endif + +/* ----------------------------------------------------------------------------- + Sparc architecture + + -- + OLD COMMENT from GHC-3.02: + + We want tailjumps to be calls, because `call xxx' is the only Sparc + branch that allows an arbitrary label as a target. (Gcc's ``goto + *target'' construct ends up loading the label into a register and + then jumping, at the cost of two extra instructions for the 32-bit + load.) + + When entering the threaded world, we stash our return address in a + known location so that \tr{%i7} is available as an extra + callee-saves register. Of course, we have to restore this when + coming out of the threaded world. + + I hate this god-forsaken architecture. Since the top of the + reserved stack space is used for globals and the bottom is reserved + for outgoing arguments, we have to stick our return address + somewhere in the middle. Currently, I'm allowing 100 extra + outgoing arguments beyond the first 6. --JSM + + Updated info (GHC 4.06): we don't appear to use %i7 any more, so + I'm not sure whether we still need to save it. Incedentally, what + does the last paragraph above mean when it says "the top of the + stack is used for globals"? What globals? --SDM + -------------------------------------------------------------------------- */ #ifdef sparc_TARGET_ARCH @@ -194,10 +325,23 @@ StgRun(StgFunPtr f, StgRegTable *basereg) { register void *i7 __asm__("%i7"); ((void **)(space))[100] = i7; f(); - __asm__ volatile (".align 4\n" + __asm__ volatile ( + ".align 4\n" ".global " STG_RETURN "\n" - STG_RETURN ":\n" - "\tld %1,%0" : "=r" (i7) : "m" (((void **)(space))[100])); + STG_RETURN ":" + : : : "l0","l1","l2","l3","l4","l5","l6","l7"); + /* we tell the C compiler that l0-l7 are clobbered on return to + * StgReturn, otherwise it tries to use these to save eg. the + * address of space[100] across the call. The correct thing + * to do would be to save all the callee-saves regs, but we + * can't be bothered to do that. + * + * The code that gcc generates for this little fragment is now + * terrible. We could do much better by coding it directly in + * assembler. + */ + __asm__ volatile ("ld %1,%0" + : "=r" (i7) : "m" (((void **)(space))[100])); return (StgThreadReturnCode)R1.i; }