1 /* -----------------------------------------------------------------------------
2 * $Id: StgCRun.c,v 1.23 2000/12/04 12:31:21 simonmar 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 Stg.h first because we want real machine regs in here: we
37 * have to get the value of R1 back from Stg land to C land intact.
49 #ifdef USE_MINIINTERPRETER
51 /* -----------------------------------------------------------------------------
52 any architecture (using miniinterpreter)
53 -------------------------------------------------------------------------- */
55 /* The static @jmp_environment@ variable allows @miniInterpret@ to
56 * communicate with @StgReturn@.
58 * Because @StgRun@ may be used recursively, we carefully
59 * save and restore the whole of @jmp_environment@.
62 #include <string.h> /* for memcpy */
64 static jmp_buf jmp_environment;
68 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg)
72 fprintf(stderr,"Jumping to ");
76 f = (StgFunPtr) (f)();
78 return (StgThreadReturnCode)R1.i;
89 #define STACK_DETAILS 0
91 static int enters = 0;
93 static void scanStackSeg ( W_* ptr, int nwords )
102 fprintf ( stderr, "%d",w ); nwords--; ptr++;
103 while (w) { fprintf(stderr, "_"); w--; nwords--; ptr++; }
106 fprintf(stderr, "p");
110 if (nwords < 0) fprintf(stderr, "erk: nwords < 0\n");
112 checkStackChunk ( ptr, ptr-nwords0 );
116 extern StgFunPtr stg_enterStackTop;
117 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg)
125 StgTSO* tso = basereg->rCurrentTSO;
126 StgWord* sb = tso->stack + tso->stack_size;
131 if (f == &stg_enterStackTop) {
141 "== SB = %p SP = %p(%p) SU = %p SpLim = %p(%p)\n",
142 sb, sp, tso->sp, su, basereg->rSpLim, tso->stack + RESERVED_STACK_WORDS);
145 if (su >= sb) goto postloop;
146 if (!sp || !su) goto postloop;
148 printStack ( sp, sb, su);
152 switch (get_itbl((StgClosure*)su)->type) {
156 fprintf(stderr, "S%d ",ws);
157 fprintf(stderr, "\n");
163 fprintf(stderr,"U%d ",ws);
165 sp = su + sizeofW(StgUpdateFrame);
166 su = ((StgUpdateFrame*)su)->link;
171 fprintf(stderr,"Q%d ",ws);
173 sp = su + sizeofW(StgSeqFrame);
174 su = ((StgSeqFrame*)su)->link;
179 fprintf(stderr,"C%d ",ws);
181 sp = su + sizeofW(StgCatchFrame);
182 su = ((StgCatchFrame*)su)->link;
185 fprintf(stderr, "?\nweird record on stack\n");
194 fprintf(stderr,"\n");
197 fprintf(stderr,"-- enter %p ", f);
198 nm = nameFromOPtr ( f );
199 if (nm) fprintf(stderr, "%s", nm); else
201 fprintf ( stderr, "\n");
204 fprintf(stderr,"\n");
207 if (enters % 1000 == 0) fprintf(stderr, "%d enters\n",enters);
209 f = (StgFunPtr) (f)();
212 fprintf (stderr, "miniInterpreter: bye!\n\n" );
213 return (StgThreadReturnCode)R1.i;
224 #else /* !USE_MINIINTERPRETER */
226 #ifdef LEADING_UNDERSCORE
227 #define STG_RETURN "_StgReturn"
229 #define STG_RETURN "StgReturn"
232 /* -----------------------------------------------------------------------------
234 -------------------------------------------------------------------------- */
236 #ifdef i386_TARGET_ARCH
239 StgRun(StgFunPtr f, StgRegTable *basereg) {
241 unsigned char space[ RESERVED_C_STACK_BYTES + 4*sizeof(void *) ];
242 StgThreadReturnCode r;
246 * save callee-saves registers on behalf of the STG code.
248 "movl %%esp, %%eax\n\t"
250 "movl %%ebx,0(%%eax)\n\t"
251 "movl %%esi,4(%%eax)\n\t"
252 "movl %%edi,8(%%eax)\n\t"
253 "movl %%ebp,12(%%eax)\n\t"
259 * grab the function argument from the stack, and jump to it.
264 ".global " STG_RETURN "\n"
267 "movl %%esi, %%eax\n\t" /* Return value in R1 */
270 * restore callee-saves registers. (Don't stomp on %%eax!)
272 "movl %%esp, %%edx\n\t"
274 "movl 0(%%edx),%%ebx\n\t" /* restore the registers saved above */
275 "movl 4(%%edx),%%esi\n\t"
276 "movl 8(%%edx),%%edi\n\t"
277 "movl 12(%%edx),%%ebp\n\t"
279 : "=&a" (r), "=m" (space)
280 : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES)
281 : "edx" /* stomps on %edx */
289 /* -----------------------------------------------------------------------------
293 OLD COMMENT from GHC-3.02:
295 We want tailjumps to be calls, because `call xxx' is the only Sparc
296 branch that allows an arbitrary label as a target. (Gcc's ``goto
297 *target'' construct ends up loading the label into a register and
298 then jumping, at the cost of two extra instructions for the 32-bit
301 When entering the threaded world, we stash our return address in a
302 known location so that \tr{%i7} is available as an extra
303 callee-saves register. Of course, we have to restore this when
304 coming out of the threaded world.
306 I hate this god-forsaken architecture. Since the top of the
307 reserved stack space is used for globals and the bottom is reserved
308 for outgoing arguments, we have to stick our return address
309 somewhere in the middle. Currently, I'm allowing 100 extra
310 outgoing arguments beyond the first 6. --JSM
312 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
313 I'm not sure whether we still need to save it. Incedentally, what
314 does the last paragraph above mean when it says "the top of the
315 stack is used for globals"? What globals? --SDM
317 Updated info (GHC 4.08.2): not saving %i7 any more (see below).
318 -------------------------------------------------------------------------- */
320 #ifdef sparc_TARGET_ARCH
323 StgRun(StgFunPtr f, StgRegTable *basereg) {
325 unsigned char space[RESERVED_C_STACK_BYTES];
327 register void *i7 __asm__("%i7");
328 ((void **)(space))[100] = i7;
333 ".global " STG_RETURN "\n"
335 : : : "l0","l1","l2","l3","l4","l5","l6","l7");
336 /* we tell the C compiler that l0-l7 are clobbered on return to
337 * StgReturn, otherwise it tries to use these to save eg. the
338 * address of space[100] across the call. The correct thing
339 * to do would be to save all the callee-saves regs, but we
340 * can't be bothered to do that.
342 * The code that gcc generates for this little fragment is now
343 * terrible. We could do much better by coding it directly in
347 /* updated 4.08.2: we don't save %i7 in the middle of the reserved
348 * space any more, since gcc tries to save its address across the
349 * call to f(), this gets clobbered in STG land and we end up
350 * dereferencing a bogus pointer in StgReturn.
352 __asm__ volatile ("ld %1,%0"
353 : "=r" (i7) : "m" (((void **)(space))[100]));
355 return (StgThreadReturnCode)R1.i;
360 /* -----------------------------------------------------------------------------
362 -------------------------------------------------------------------------- */
364 #ifdef alpha_TARGET_ARCH
367 StgRun(StgFunPtr f, StgRegTable *basereg)
369 StgThreadReturnCode ret;
371 __asm__ volatile ("stq $9,-8($30)\n\t"
372 "stq $10,-16($30)\n\t"
373 "stq $11,-24($30)\n\t"
374 "stq $12,-32($30)\n\t"
375 "stq $13,-40($30)\n\t"
376 "stq $14,-48($30)\n\t"
377 "stq $15,-56($30)\n\t"
378 "stt $f2,-64($30)\n\t"
379 "stt $f3,-72($30)\n\t"
380 "stt $f4,-80($30)\n\t"
381 "stt $f5,-88($30)\n\t"
382 "stt $f6,-96($30)\n\t"
383 "stt $f7,-104($30)\n\t"
384 "stt $f8,-112($30)\n\t"
385 "stt $f9,-120($30)\n\t"
386 "lda $30,-%0($30)" : :
387 "K" (RESERVED_C_STACK_BYTES+
388 8*sizeof(double)+8*sizeof(long)));
392 __asm__ volatile (".align 3\n"
393 ".globl " STG_RETURN "\n"
395 "lda %0,($14)\n\t" /* save R1 */
396 "lda $30,%0($30)\n\t"
398 "ldq $10,-16($30)\n\t"
399 "ldq $11,-24($30)\n\t"
400 "ldq $12,-32($30)\n\t"
401 "ldq $13,-40($30)\n\t"
402 "ldq $14,-48($30)\n\t"
403 "ldq $15,-56($30)\n\t"
404 "ldt $f2,-64($30)\n\t"
405 "ldt $f3,-72($30)\n\t"
406 "ldt $f4,-80($30)\n\t"
407 "ldt $f5,-88($30)\n\t"
408 "ldt $f6,-96($30)\n\t"
409 "ldt $f7,-104($30)\n\t"
410 "ldt $f8,-112($30)\n\t"
413 : "K" (RESERVED_C_STACK_BYTES+
414 8*sizeof(double)+8*sizeof(long)));
419 #endif /* alpha_TARGET_ARCH */
421 /* -----------------------------------------------------------------------------
423 -------------------------------------------------------------------------- */
425 #ifdef hppa1_1_TARGET_ARCH
428 StgRun(StgFunPtr f, StgRegTable *basereg)
430 StgChar space[RESERVED_C_STACK_BYTES+16*sizeof(long)+10*sizeof(double)];
431 StgThreadReturnCode ret;
433 __asm__ volatile ("ldo %0(%%r30),%%r19\n"
434 "\tstw %%r3, 0(0,%%r19)\n"
435 "\tstw %%r4, 4(0,%%r19)\n"
436 "\tstw %%r5, 8(0,%%r19)\n"
437 "\tstw %%r6,12(0,%%r19)\n"
438 "\tstw %%r7,16(0,%%r19)\n"
439 "\tstw %%r8,20(0,%%r19)\n"
440 "\tstw %%r9,24(0,%%r19)\n"
441 "\tstw %%r10,28(0,%%r19)\n"
442 "\tstw %%r11,32(0,%%r19)\n"
443 "\tstw %%r12,36(0,%%r19)\n"
444 "\tstw %%r13,40(0,%%r19)\n"
445 "\tstw %%r14,44(0,%%r19)\n"
446 "\tstw %%r15,48(0,%%r19)\n"
447 "\tstw %%r16,52(0,%%r19)\n"
448 "\tstw %%r17,56(0,%%r19)\n"
449 "\tstw %%r18,60(0,%%r19)\n"
450 "\tldo 80(%%r19),%%r19\n"
451 "\tfstds %%fr12,-16(0,%%r19)\n"
452 "\tfstds %%fr13, -8(0,%%r19)\n"
453 "\tfstds %%fr14, 0(0,%%r19)\n"
454 "\tfstds %%fr15, 8(0,%%r19)\n"
455 "\tldo 32(%%r19),%%r19\n"
456 "\tfstds %%fr16,-16(0,%%r19)\n"
457 "\tfstds %%fr17, -8(0,%%r19)\n"
458 "\tfstds %%fr18, 0(0,%%r19)\n"
459 "\tfstds %%fr19, 8(0,%%r19)\n"
460 "\tldo 32(%%r19),%%r19\n"
461 "\tfstds %%fr20,-16(0,%%r19)\n"
462 "\tfstds %%fr21, -8(0,%%r19)\n" : :
463 "n" (-(116 * sizeof(long) + 10 * sizeof(double))) : "%r19"
468 __asm__ volatile (".align 4\n"
469 "\t.EXPORT " STG_RETURN ",CODE\n"
470 "\t.EXPORT " STG_RETURN ",ENTRY,PRIV_LEV=3\n"
472 /* "\tldo %0(%%r3),%%r19\n" */
473 "\tldo %1(%%r30),%%r19\n"
474 "\tcopy %%r11, %0\n" /* save R1 */
475 "\tldw 0(0,%%r19),%%r3\n"
476 "\tldw 4(0,%%r19),%%r4\n"
477 "\tldw 8(0,%%r19),%%r5\n"
478 "\tldw 12(0,%%r19),%%r6\n"
479 "\tldw 16(0,%%r19),%%r7\n"
480 "\tldw 20(0,%%r19),%%r8\n"
481 "\tldw 24(0,%%r19),%%r9\n"
482 "\tldw 28(0,%%r19),%%r10\n"
483 "\tldw 32(0,%%r19),%%r11\n"
484 "\tldw 36(0,%%r19),%%r12\n"
485 "\tldw 40(0,%%r19),%%r13\n"
486 "\tldw 44(0,%%r19),%%r14\n"
487 "\tldw 48(0,%%r19),%%r15\n"
488 "\tldw 52(0,%%r19),%%r16\n"
489 "\tldw 56(0,%%r19),%%r17\n"
490 "\tldw 60(0,%%r19),%%r18\n"
491 "\tldo 80(%%r19),%%r19\n"
492 "\tfldds -16(0,%%r19),%%fr12\n"
493 "\tfldds -8(0,%%r19),%%fr13\n"
494 "\tfldds 0(0,%%r19),%%fr14\n"
495 "\tfldds 8(0,%%r19),%%fr15\n"
496 "\tldo 32(%%r19),%%r19\n"
497 "\tfldds -16(0,%%r19),%%fr16\n"
498 "\tfldds -8(0,%%r19),%%fr17\n"
499 "\tfldds 0(0,%%r19),%%fr18\n"
500 "\tfldds 8(0,%%r19),%%fr19\n"
501 "\tldo 32(%%r19),%%r19\n"
502 "\tfldds -16(0,%%r19),%%fr20\n"
503 "\tfldds -8(0,%%r19),%%fr21\n"
505 : "n" (-(116 * sizeof(long) + 10 * sizeof(double)))
512 #endif /* hppa1_1_TARGET_ARCH */
514 #endif /* !USE_MINIINTERPRETER */