1 /* -----------------------------------------------------------------------------
2 * $Id: StgCRun.c,v 1.16 2000/04/11 16:36:54 sewardj 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)
71 /* Save jmp_environment for previous call to miniInterpret */
72 memcpy((void *) jmp_environment, (void *) save_buf, sizeof(jmp_buf));
73 if (setjmp(jmp_environment) == 0) {
77 fprintf(stderr,"Jumping to ");
82 f = (StgFunPtr) (f)();
83 if (!IS_CODE_PTR(f)) {
84 fprintf ( stderr,"bad ptr given by %p %s\n", f_old, nameFromOPtr(f_old) );
85 assert(IS_CODE_PTR(f));
90 /* Restore jmp_environment for previous call */
91 memcpy((void*) save_buf, (void*) jmp_environment, sizeof(jmp_buf));
93 return (StgThreadReturnCode)R1.i;
98 longjmp(jmp_environment, 1);
103 #define CHECK_STACK 0
104 #define STACK_DETAILS 0
106 static int enters = 0;
108 static void scanStackSeg ( W_* ptr, int nwords )
112 int nwords0 = nwords;
117 fprintf ( stderr, "%d",w ); nwords--; ptr++;
118 while (w) { fprintf(stderr, "_"); w--; nwords--; ptr++; }
121 fprintf(stderr, "p");
125 if (nwords < 0) fprintf(stderr, "erk: nwords < 0\n");
127 checkStackChunk ( ptr, ptr-nwords0 );
131 extern StgFunPtr stg_enterStackTop;
132 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg)
140 StgTSO* tso = basereg->rCurrentTSO;
141 StgWord* sb = tso->stack + tso->stack_size;
146 if (f == &stg_enterStackTop) {
156 "== SB = %p SP = %p(%p) SU = %p SpLim = %p(%p)\n",
157 sb, sp, tso->sp, su, basereg->rSpLim, tso->splim);
160 if (su >= sb) goto postloop;
161 if (!sp || !su) goto postloop;
163 printStack ( sp, sb, su);
167 switch (get_itbl((StgClosure*)su)->type) {
171 fprintf(stderr, "S%d ",ws);
172 fprintf(stderr, "\n");
178 fprintf(stderr,"U%d ",ws);
180 sp = su + sizeofW(StgUpdateFrame);
181 su = ((StgUpdateFrame*)su)->link;
186 fprintf(stderr,"Q%d ",ws);
188 sp = su + sizeofW(StgSeqFrame);
189 su = ((StgSeqFrame*)su)->link;
194 fprintf(stderr,"C%d ",ws);
196 sp = su + sizeofW(StgCatchFrame);
197 su = ((StgCatchFrame*)su)->link;
200 fprintf(stderr, "?\nweird record on stack\n");
209 fprintf(stderr,"\n");
212 fprintf(stderr,"-- enter %p ", f);
213 nm = nameFromOPtr ( f );
214 if (nm) fprintf(stderr, "%s", nm); else
216 fprintf ( stderr, "\n");
219 fprintf(stderr,"\n");
222 if (enters % 1000 == 0) fprintf(stderr, "%d enters\n",enters);
224 f = (StgFunPtr) (f)();
227 fprintf (stderr, "miniInterpreter: bye!\n\n" );
228 return (StgThreadReturnCode)R1.i;
239 #else /* !USE_MINIINTERPRETER */
241 #ifdef LEADING_UNDERSCORE
242 #define STG_RETURN "_StgReturn"
244 #define STG_RETURN "StgReturn"
247 /* -----------------------------------------------------------------------------
249 -------------------------------------------------------------------------- */
251 #ifdef i386_TARGET_ARCH
254 StgRun(StgFunPtr f, StgRegTable *basereg) {
256 StgChar space[ RESERVED_C_STACK_BYTES + 4*sizeof(void *) ];
257 StgThreadReturnCode r;
261 * save callee-saves registers on behalf of the STG code.
263 "movl %%esp, %%eax\n\t"
265 "movl %%ebx,0(%%eax)\n\t"
266 "movl %%esi,4(%%eax)\n\t"
267 "movl %%edi,8(%%eax)\n\t"
268 "movl %%ebp,12(%%eax)\n\t"
274 * grab the function argument from the stack, and jump to it.
279 ".global " STG_RETURN "\n"
282 "movl %%esi, %%eax\n\t" /* Return value in R1 */
285 * restore callee-saves registers. (Don't stomp on %%eax!)
287 "movl %%esp, %%edx\n\t"
289 "movl 0(%%edx),%%ebx\n\t" /* restore the registers saved above */
290 "movl 4(%%edx),%%esi\n\t"
291 "movl 8(%%edx),%%edi\n\t"
292 "movl 12(%%edx),%%ebp\n\t"
294 : "=&a" (r), "=m" (space)
295 : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES)
296 : "edx" /* stomps on %edx */
304 /* -----------------------------------------------------------------------------
308 OLD COMMENT from GHC-3.02:
310 We want tailjumps to be calls, because `call xxx' is the only Sparc
311 branch that allows an arbitrary label as a target. (Gcc's ``goto
312 *target'' construct ends up loading the label into a register and
313 then jumping, at the cost of two extra instructions for the 32-bit
316 When entering the threaded world, we stash our return address in a
317 known location so that \tr{%i7} is available as an extra
318 callee-saves register. Of course, we have to restore this when
319 coming out of the threaded world.
321 I hate this god-forsaken architecture. Since the top of the
322 reserved stack space is used for globals and the bottom is reserved
323 for outgoing arguments, we have to stick our return address
324 somewhere in the middle. Currently, I'm allowing 100 extra
325 outgoing arguments beyond the first 6. --JSM
327 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
328 I'm not sure whether we still need to save it. Incedentally, what
329 does the last paragraph above mean when it says "the top of the
330 stack is used for globals"? What globals? --SDM
332 -------------------------------------------------------------------------- */
334 #ifdef sparc_TARGET_ARCH
337 StgRun(StgFunPtr f, StgRegTable *basereg) {
339 StgChar space[RESERVED_C_STACK_BYTES+sizeof(void *)];
340 register void *i7 __asm__("%i7");
341 ((void **)(space))[100] = i7;
345 ".global " STG_RETURN "\n"
347 : : : "l0","l1","l2","l3","l4","l5","l6","l7");
348 /* we tell the C compiler that l0-l7 are clobbered on return to
349 * StgReturn, otherwise it tries to use these to save eg. the
350 * address of space[100] across the call. The correct thing
351 * to do would be to save all the callee-saves regs, but we
352 * can't be bothered to do that.
354 * The code that gcc generates for this little fragment is now
355 * terrible. We could do much better by coding it directly in
358 __asm__ volatile ("ld %1,%0"
359 : "=r" (i7) : "m" (((void **)(space))[100]));
360 return (StgThreadReturnCode)R1.i;
365 /* -----------------------------------------------------------------------------
367 -------------------------------------------------------------------------- */
369 #ifdef alpha_TARGET_ARCH
372 StgRun(StgFunPtr f, StgRegTable *basereg)
374 StgThreadReturnCode ret;
376 __asm__ volatile ("stq $9,-8($30)\n\t"
377 "stq $10,-16($30)\n\t"
378 "stq $11,-24($30)\n\t"
379 "stq $12,-32($30)\n\t"
380 "stq $13,-40($30)\n\t"
381 "stq $14,-48($30)\n\t"
382 "stq $15,-56($30)\n\t"
383 "stt $f2,-64($30)\n\t"
384 "stt $f3,-72($30)\n\t"
385 "stt $f4,-80($30)\n\t"
386 "stt $f5,-88($30)\n\t"
387 "stt $f6,-96($30)\n\t"
388 "stt $f7,-104($30)\n\t"
389 "stt $f8,-112($30)\n\t"
390 "stt $f9,-120($30)\n\t"
391 "lda $30,-%0($30)" : :
392 "K" (RESERVED_C_STACK_BYTES+
393 8*sizeof(double)+8*sizeof(long)));
397 __asm__ volatile (".align 3\n"
398 ".globl " STG_RETURN "\n"
400 "lda %0,($14)\n\t" /* save R1 */
401 "lda $30,%0($30)\n\t"
403 "ldq $10,-16($30)\n\t"
404 "ldq $11,-24($30)\n\t"
405 "ldq $12,-32($30)\n\t"
406 "ldq $13,-40($30)\n\t"
407 "ldq $14,-48($30)\n\t"
408 "ldq $15,-56($30)\n\t"
409 "ldt $f2,-64($30)\n\t"
410 "ldt $f3,-72($30)\n\t"
411 "ldt $f4,-80($30)\n\t"
412 "ldt $f5,-88($30)\n\t"
413 "ldt $f6,-96($30)\n\t"
414 "ldt $f7,-104($30)\n\t"
415 "ldt $f8,-112($30)\n\t"
418 : "K" (RESERVED_C_STACK_BYTES+
419 8*sizeof(double)+8*sizeof(long)));
424 #endif /* alpha_TARGET_ARCH */
426 /* -----------------------------------------------------------------------------
428 -------------------------------------------------------------------------- */
430 #ifdef hppa1_1_TARGET_ARCH
433 StgRun(StgFunPtr f, StgRegTable *basereg)
435 StgChar space[RESERVED_C_STACK_BYTES+16*sizeof(long)+10*sizeof(double)];
436 StgThreadReturnCode ret;
438 __asm__ volatile ("ldo %0(%%r30),%%r19\n"
439 "\tstw %%r3, 0(0,%%r19)\n"
440 "\tstw %%r4, 4(0,%%r19)\n"
441 "\tstw %%r5, 8(0,%%r19)\n"
442 "\tstw %%r6,12(0,%%r19)\n"
443 "\tstw %%r7,16(0,%%r19)\n"
444 "\tstw %%r8,20(0,%%r19)\n"
445 "\tstw %%r9,24(0,%%r19)\n"
446 "\tstw %%r10,28(0,%%r19)\n"
447 "\tstw %%r11,32(0,%%r19)\n"
448 "\tstw %%r12,36(0,%%r19)\n"
449 "\tstw %%r13,40(0,%%r19)\n"
450 "\tstw %%r14,44(0,%%r19)\n"
451 "\tstw %%r15,48(0,%%r19)\n"
452 "\tstw %%r16,52(0,%%r19)\n"
453 "\tstw %%r17,56(0,%%r19)\n"
454 "\tstw %%r18,60(0,%%r19)\n"
455 "\tldo 80(%%r19),%%r19\n"
456 "\tfstds %%fr12,-16(0,%%r19)\n"
457 "\tfstds %%fr13, -8(0,%%r19)\n"
458 "\tfstds %%fr14, 0(0,%%r19)\n"
459 "\tfstds %%fr15, 8(0,%%r19)\n"
460 "\tldo 32(%%r19),%%r19\n"
461 "\tfstds %%fr16,-16(0,%%r19)\n"
462 "\tfstds %%fr17, -8(0,%%r19)\n"
463 "\tfstds %%fr18, 0(0,%%r19)\n"
464 "\tfstds %%fr19, 8(0,%%r19)\n"
465 "\tldo 32(%%r19),%%r19\n"
466 "\tfstds %%fr20,-16(0,%%r19)\n"
467 "\tfstds %%fr21, -8(0,%%r19)\n" : :
468 "n" (-(116 * sizeof(long) + 10 * sizeof(double))) : "%r19"
473 __asm__ volatile (".align 4\n"
474 "\t.EXPORT " STG_RETURN ",CODE\n"
475 "\t.EXPORT " STG_RETURN ",ENTRY,PRIV_LEV=3\n"
477 /* "\tldo %0(%%r3),%%r19\n" */
478 "\tldo %1(%%r30),%%r19\n"
479 "\tcopy %%r11, %0\n" /* save R1 */
480 "\tldw 0(0,%%r19),%%r3\n"
481 "\tldw 4(0,%%r19),%%r4\n"
482 "\tldw 8(0,%%r19),%%r5\n"
483 "\tldw 12(0,%%r19),%%r6\n"
484 "\tldw 16(0,%%r19),%%r7\n"
485 "\tldw 20(0,%%r19),%%r8\n"
486 "\tldw 24(0,%%r19),%%r9\n"
487 "\tldw 28(0,%%r19),%%r10\n"
488 "\tldw 32(0,%%r19),%%r11\n"
489 "\tldw 36(0,%%r19),%%r12\n"
490 "\tldw 40(0,%%r19),%%r13\n"
491 "\tldw 44(0,%%r19),%%r14\n"
492 "\tldw 48(0,%%r19),%%r15\n"
493 "\tldw 52(0,%%r19),%%r16\n"
494 "\tldw 56(0,%%r19),%%r17\n"
495 "\tldw 60(0,%%r19),%%r18\n"
496 "\tldo 80(%%r19),%%r19\n"
497 "\tfldds -16(0,%%r19),%%fr12\n"
498 "\tfldds -8(0,%%r19),%%fr13\n"
499 "\tfldds 0(0,%%r19),%%fr14\n"
500 "\tfldds 8(0,%%r19),%%fr15\n"
501 "\tldo 32(%%r19),%%r19\n"
502 "\tfldds -16(0,%%r19),%%fr16\n"
503 "\tfldds -8(0,%%r19),%%fr17\n"
504 "\tfldds 0(0,%%r19),%%fr18\n"
505 "\tfldds 8(0,%%r19),%%fr19\n"
506 "\tldo 32(%%r19),%%r19\n"
507 "\tfldds -16(0,%%r19),%%fr20\n"
508 "\tfldds -8(0,%%r19),%%fr21\n"
510 : "n" (-(116 * sizeof(long) + 10 * sizeof(double)))
517 #endif /* hppa1_1_TARGET_ARCH */
519 #endif /* !USE_MINIINTERPRETER */