[project @ 2002-06-13 21:14:51 by wolfgang]
[ghc-hetmet.git] / ghc / rts / StgCRun.c
1 /* -----------------------------------------------------------------------------
2  * $Id: StgCRun.c,v 1.36 2002/06/13 21:14:51 wolfgang Exp $
3  *
4  * (c) The GHC Team, 1998-2000
5  *
6  * STG-to-C glue.
7  *
8  * To run an STG function from C land, call
9  *
10  *              rv = StgRun(f,BaseReg);
11  *
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).
15  *
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.
20  *
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
24  * in STG land.
25  *
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
32  * variables.
33  *
34  * -------------------------------------------------------------------------- */
35
36 #include "PosixSource.h"
37
38
39 /*
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.
43  *
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
47  * late.
48  *
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.
52  */
53 #include "config.h"
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");
62 #endif
63 #endif
64
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.
67  */
68 #include "Stg.h"
69 #include "Rts.h"
70 #include "StgRun.h"
71
72 #ifdef DEBUG
73 #include "RtsFlags.h"
74 #include "RtsUtils.h"
75 #include "Printer.h"
76 #endif
77
78 #ifdef USE_MINIINTERPRETER
79
80 /* -----------------------------------------------------------------------------
81    any architecture (using miniinterpreter)
82    -------------------------------------------------------------------------- */
83
84 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg STG_UNUSED)
85 {
86    while (f) {
87       IF_DEBUG(evaluator,
88                fprintf(stderr,"Jumping to ");
89                printPtr((P_)f); fflush(stdout);
90                fprintf(stderr,"\n");
91               );
92       f = (StgFunPtr) (f)();
93    }
94    return (StgThreadReturnCode)R1.i;
95 }
96
97 EXTFUN(StgReturn)
98 {
99    return 0;
100 }
101
102 #else /* !USE_MINIINTERPRETER */
103
104 #ifdef LEADING_UNDERSCORE
105 #define STG_RETURN "_StgReturn"
106 #else
107 #define STG_RETURN "StgReturn"
108 #endif
109
110 /* -----------------------------------------------------------------------------
111    x86 architecture
112    -------------------------------------------------------------------------- */
113
114 #ifdef i386_TARGET_ARCH
115
116 StgThreadReturnCode
117 StgRun(StgFunPtr f, StgRegTable *basereg) {
118
119     unsigned char space[ RESERVED_C_STACK_BYTES + 4*sizeof(void *) ];
120     StgThreadReturnCode r;
121
122     __asm__ volatile (
123         /*
124          * save callee-saves registers on behalf of the STG code.
125          */
126         "movl %%esp, %%eax\n\t"
127         "addl %4, %%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"
132         /*
133          * Set BaseReg
134          */
135         "movl %3,%%ebx\n\t"
136         /*
137          * grab the function argument from the stack, and jump to it.
138          */
139         "movl %2,%%eax\n\t"
140         "jmp *%%eax\n\t"
141
142         ".global " STG_RETURN "\n"
143         STG_RETURN ":\n\t"
144
145         "movl %%esi, %%eax\n\t"   /* Return value in R1  */
146
147         /*
148          * restore callee-saves registers.  (Don't stomp on %%eax!)
149          */
150         "movl %%esp, %%edx\n\t"
151         "addl %4, %%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"
156
157       : "=&a" (r), "=m" (space)
158       : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES)
159       : "edx" /* stomps on %edx */
160     );
161
162     return r;
163 }
164
165 #endif
166
167 /* -----------------------------------------------------------------------------
168    Sparc architecture
169
170    --
171    OLD COMMENT from GHC-3.02:
172
173    We want tailjumps to be calls, because `call xxx' is the only Sparc
174    branch that allows an arbitrary label as a target.  (Gcc's ``goto
175    *target'' construct ends up loading the label into a register and
176    then jumping, at the cost of two extra instructions for the 32-bit
177    load.)
178
179    When entering the threaded world, we stash our return address in a
180    known location so that \tr{%i7} is available as an extra
181    callee-saves register.  Of course, we have to restore this when
182    coming out of the threaded world.
183
184    I hate this god-forsaken architecture.  Since the top of the
185    reserved stack space is used for globals and the bottom is reserved
186    for outgoing arguments, we have to stick our return address
187    somewhere in the middle.  Currently, I'm allowing 100 extra
188    outgoing arguments beyond the first 6.  --JSM
189
190    Updated info (GHC 4.06): we don't appear to use %i7 any more, so
191    I'm not sure whether we still need to save it.  Incedentally, what
192    does the last paragraph above mean when it says "the top of the
193    stack is used for globals"?  What globals?  --SDM
194
195    Updated info (GHC 4.08.2): not saving %i7 any more (see below).
196    -------------------------------------------------------------------------- */
197
198 #ifdef sparc_TARGET_ARCH
199
200 StgThreadReturnCode
201 StgRun(StgFunPtr f, StgRegTable *basereg) {
202
203     unsigned char space[RESERVED_C_STACK_BYTES];
204 #if 0
205     register void *i7 __asm__("%i7");
206     ((void **)(space))[100] = i7;
207 #endif
208     f();
209     __asm__ volatile (
210             ".align 4\n"
211             ".global " STG_RETURN "\n"
212             STG_RETURN ":"
213             : : : "l0","l1","l2","l3","l4","l5","l6","l7");
214     /* we tell the C compiler that l0-l7 are clobbered on return to
215      * StgReturn, otherwise it tries to use these to save eg. the
216      * address of space[100] across the call.  The correct thing
217      * to do would be to save all the callee-saves regs, but we
218      * can't be bothered to do that.
219      *
220      * The code that gcc generates for this little fragment is now
221      * terrible.  We could do much better by coding it directly in
222      * assembler.
223      */
224 #if 0
225     /* updated 4.08.2: we don't save %i7 in the middle of the reserved
226      * space any more, since gcc tries to save its address across the
227      * call to f(), this gets clobbered in STG land and we end up
228      * dereferencing a bogus pointer in StgReturn.
229      */
230     __asm__ volatile ("ld %1,%0"
231                       : "=r" (i7) : "m" (((void **)(space))[100]));
232 #endif
233     return (StgThreadReturnCode)R1.i;
234 }
235
236 #endif
237
238 /* -----------------------------------------------------------------------------
239    alpha architecture
240
241    "The stack pointer (SP) must at all times denote an address that has octaword
242     alignment. (This restriction has the side effect that the in-memory portion
243     of the argument list, if any, will start on an octaword boundary.) Note that
244     the stack grows toward lower addresses. During a procedure invocation, SP
245     can never be set to a value that is higher than the value of SP at entry to
246     that procedure invocation.
247
248    "The contents of the stack, located above the portion of the argument list
249     (if any) that is passed in memory, belong to the calling procedure. Because
250     they are part of the calling procedure, they should not be read or written
251     by the called procedure, except as specified by indirect arguments or
252     language-controlled up-level references.
253
254    "The SP value might be used by the hardware when raising exceptions and
255     asynchronous interrupts. It must be assumed that the contents of the stack
256     below the current SP value and within the stack for the current thread are
257     continually and unpredictably modified, as specified in the _Alpha
258     Architecture Reference Manual_, and as a result of asynchronous software
259     actions."
260
261    -- Compaq Computer Corporation, Houston. Tru64 UNIX Calling Standard for
262       Alpha Systems, 5.1 edition, August 2000, section 3.2.1.  http://www.
263       tru64unix.compaq.com/docs/base_doc/DOCUMENTATION/V51_PDF/ARH9MBTE.PDF
264    -------------------------------------------------------------------------- */
265
266 #ifdef alpha_TARGET_ARCH
267
268 StgThreadReturnCode
269 StgRun(StgFunPtr f, StgRegTable *basereg)
270 {
271     register long   real_ra __asm__("$26"); volatile long   save_ra;
272     register long   real_gp __asm__("$29"); volatile long   save_gp;
273
274     register long   real_s0 __asm__("$9" ); volatile long   save_s0;
275     register long   real_s1 __asm__("$10"); volatile long   save_s1;
276     register long   real_s2 __asm__("$11"); volatile long   save_s2;
277     register long   real_s3 __asm__("$12"); volatile long   save_s3;
278     register long   real_s4 __asm__("$13"); volatile long   save_s4;
279     register long   real_s5 __asm__("$14"); volatile long   save_s5;
280 #ifdef alpha_EXTRA_CAREFUL
281     register long   real_s6 __asm__("$15"); volatile long   save_s6;
282 #endif
283
284     register double real_f2 __asm__("$f2"); volatile double save_f2;
285     register double real_f3 __asm__("$f3"); volatile double save_f3;
286     register double real_f4 __asm__("$f4"); volatile double save_f4;
287     register double real_f5 __asm__("$f5"); volatile double save_f5;
288     register double real_f6 __asm__("$f6"); volatile double save_f6;
289     register double real_f7 __asm__("$f7"); volatile double save_f7;
290 #ifdef alpha_EXTRA_CAREFUL
291     register double real_f8 __asm__("$f8"); volatile double save_f8;
292     register double real_f9 __asm__("$f9"); volatile double save_f9;
293 #endif
294
295     register StgFunPtr real_pv __asm__("$27");
296
297     StgThreadReturnCode ret;
298
299     save_ra = real_ra;
300     save_gp = real_gp;
301
302     save_s0 = real_s0;
303     save_s1 = real_s1;
304     save_s2 = real_s2;
305     save_s3 = real_s3;
306     save_s4 = real_s4;
307     save_s5 = real_s5;
308 #ifdef alpha_EXTRA_CAREFUL
309     save_s6 = real_s6;
310 #endif
311
312     save_f2 = real_f2;
313     save_f3 = real_f3;
314     save_f4 = real_f4;
315     save_f5 = real_f5;
316     save_f6 = real_f6;
317     save_f7 = real_f7;
318 #ifdef alpha_EXTRA_CAREFUL
319     save_f8 = real_f8;
320     save_f9 = real_f9;
321 #endif
322
323     real_pv = f;
324
325     __asm__ volatile(   "lda $30,-%0($30)"      "\n"
326                 "\t"    "jmp ($27)"             "\n"
327                 "\t"    ".align 3"              "\n"
328                 ".globl " STG_RETURN            "\n"
329                 STG_RETURN ":"                  "\n"
330                 "\t"    "lda $30,%0($30)"       "\n"
331                 : : "K" (RESERVED_C_STACK_BYTES));
332
333     ret = real_s5;
334
335     real_s0 = save_s0;
336     real_s1 = save_s1;
337     real_s2 = save_s2;
338     real_s3 = save_s3;
339     real_s4 = save_s4;
340     real_s5 = save_s5;
341 #ifdef alpha_EXTRA_CAREFUL
342     real_s6 = save_s6;
343 #endif
344
345     real_f2 = save_f2;
346     real_f3 = save_f3;
347     real_f4 = save_f4;
348     real_f5 = save_f5;
349     real_f6 = save_f6;
350     real_f7 = save_f7;
351 #ifdef alpha_EXTRA_CAREFUL
352     real_f8 = save_f8;
353     real_f9 = save_f9;
354 #endif
355
356     real_ra = save_ra;
357     real_gp = save_gp;
358
359     return ret;
360 }
361
362 #endif /* alpha_TARGET_ARCH */
363
364 /* -----------------------------------------------------------------------------
365    HP-PA architecture
366    -------------------------------------------------------------------------- */
367
368 #ifdef hppa1_1_TARGET_ARCH
369
370 StgThreadReturnCode
371 StgRun(StgFunPtr f, StgRegTable *basereg)
372 {
373     StgChar space[RESERVED_C_STACK_BYTES+16*sizeof(long)+10*sizeof(double)];
374     StgThreadReturnCode ret;
375
376     __asm__ volatile ("ldo %0(%%r30),%%r19\n"
377                       "\tstw %%r3, 0(0,%%r19)\n"
378                       "\tstw %%r4, 4(0,%%r19)\n"
379                       "\tstw %%r5, 8(0,%%r19)\n"
380                       "\tstw %%r6,12(0,%%r19)\n"
381                       "\tstw %%r7,16(0,%%r19)\n"
382                       "\tstw %%r8,20(0,%%r19)\n"
383                       "\tstw %%r9,24(0,%%r19)\n"
384                       "\tstw %%r10,28(0,%%r19)\n"
385                       "\tstw %%r11,32(0,%%r19)\n"
386                       "\tstw %%r12,36(0,%%r19)\n"
387                       "\tstw %%r13,40(0,%%r19)\n"
388                       "\tstw %%r14,44(0,%%r19)\n"
389                       "\tstw %%r15,48(0,%%r19)\n"
390                       "\tstw %%r16,52(0,%%r19)\n"
391                       "\tstw %%r17,56(0,%%r19)\n"
392                       "\tstw %%r18,60(0,%%r19)\n"
393                       "\tldo 80(%%r19),%%r19\n"
394                       "\tfstds %%fr12,-16(0,%%r19)\n"
395                       "\tfstds %%fr13, -8(0,%%r19)\n"
396                       "\tfstds %%fr14,  0(0,%%r19)\n"
397                       "\tfstds %%fr15,  8(0,%%r19)\n"
398                       "\tldo 32(%%r19),%%r19\n"
399                       "\tfstds %%fr16,-16(0,%%r19)\n"
400                       "\tfstds %%fr17, -8(0,%%r19)\n"
401                       "\tfstds %%fr18,  0(0,%%r19)\n"
402                       "\tfstds %%fr19,  8(0,%%r19)\n"
403                       "\tldo 32(%%r19),%%r19\n"
404                       "\tfstds %%fr20,-16(0,%%r19)\n"
405                       "\tfstds %%fr21, -8(0,%%r19)\n" : :
406                       "n" (-(116 * sizeof(long) + 10 * sizeof(double))) : "%r19"
407                       );
408
409     f();
410
411     __asm__ volatile (".align 4\n"
412                       "\t.EXPORT " STG_RETURN ",CODE\n"
413                       "\t.EXPORT " STG_RETURN ",ENTRY,PRIV_LEV=3\n"
414                       STG_RETURN "\n"
415                       /* "\tldo %0(%%r3),%%r19\n" */
416                       "\tldo %1(%%r30),%%r19\n"
417                       "\tcopy %%r11, %0\n"  /* save R1 */
418                       "\tldw  0(0,%%r19),%%r3\n"
419                       "\tldw  4(0,%%r19),%%r4\n"
420                       "\tldw  8(0,%%r19),%%r5\n"
421                       "\tldw 12(0,%%r19),%%r6\n"
422                       "\tldw 16(0,%%r19),%%r7\n"
423                       "\tldw 20(0,%%r19),%%r8\n"
424                       "\tldw 24(0,%%r19),%%r9\n"
425                       "\tldw 28(0,%%r19),%%r10\n"
426                       "\tldw 32(0,%%r19),%%r11\n"
427                       "\tldw 36(0,%%r19),%%r12\n"
428                       "\tldw 40(0,%%r19),%%r13\n"
429                       "\tldw 44(0,%%r19),%%r14\n"
430                       "\tldw 48(0,%%r19),%%r15\n"
431                       "\tldw 52(0,%%r19),%%r16\n"
432                       "\tldw 56(0,%%r19),%%r17\n"
433                       "\tldw 60(0,%%r19),%%r18\n"
434                       "\tldo 80(%%r19),%%r19\n"
435                       "\tfldds -16(0,%%r19),%%fr12\n"
436                       "\tfldds  -8(0,%%r19),%%fr13\n"
437                       "\tfldds   0(0,%%r19),%%fr14\n"
438                       "\tfldds   8(0,%%r19),%%fr15\n"
439                       "\tldo 32(%%r19),%%r19\n"
440                       "\tfldds -16(0,%%r19),%%fr16\n"
441                       "\tfldds  -8(0,%%r19),%%fr17\n"
442                       "\tfldds   0(0,%%r19),%%fr18\n"
443                       "\tfldds   8(0,%%r19),%%fr19\n"
444                       "\tldo 32(%%r19),%%r19\n"
445                       "\tfldds -16(0,%%r19),%%fr20\n"
446                       "\tfldds  -8(0,%%r19),%%fr21\n"
447                          : "=r" (ret)
448                          : "n" (-(116 * sizeof(long) + 10 * sizeof(double)))
449                          : "%r19"
450                       );
451
452     return ret;
453 }
454
455 #endif /* hppa1_1_TARGET_ARCH */
456
457 /* -----------------------------------------------------------------------------
458    PowerPC architecture
459
460    Everything is in assembler, so we don't have to deal with GCC...
461    
462    -------------------------------------------------------------------------- */
463
464 #ifdef powerpc_TARGET_ARCH
465
466 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg);
467
468 static void StgRunIsImplementedInAssembler(void)
469 {
470         __asm__ volatile (
471                 "\n.globl _StgRun\n"
472                 "_StgRun:\n"
473                 "\tmflr r0\n"
474                 "\tbl saveFP # f14\n"
475                 "\tstmw r14,-216(r1)\n"
476                 "\tstwu r1,-8480(r1)\n"
477                 "\tmtctr r3\n"
478                 "\tmr r12,r3\n"
479                 "\tbctr\n"
480                 ".globl _StgReturn\n"
481                 "_StgReturn:\n"
482                 "\tmr r3,r14\n"
483                 "\tla r1,8480(r1)\n"
484                 "\tlmw r14,-216(r1)\n"
485                 "\tb restFP # f14\n"
486         );      /* RESERVED_C_STACK_BYTES + stack frame size == 8192 + 288 == 8480 */
487 }
488
489 #endif
490
491 /* -----------------------------------------------------------------------------
492    IA64 architecture
493
494    Again, in assembler - so we can fiddle with the register stack, and because
495    gcc doesn't handle asm-clobbered callee-saves correctly.
496
497    loc0  - loc15: preserved locals
498    loc16 - loc28: STG registers
499            loc29: saved ar.pfs
500            loc30: saved b0
501    -------------------------------------------------------------------------- */
502
503 #ifdef ia64_TARGET_ARCH
504
505 /* the memory stack is rarely used, so 16K is excessive */
506 #undef RESERVED_C_STACK_BYTES
507 #define RESERVED_C_STACK_BYTES 1024
508
509 static void StgRunIsImplementedInAssembler(void)
510 {
511     __asm__ volatile(
512                 ".global StgRun\n"
513                 "StgRun:\n"
514                 "\talloc loc29 = ar.pfs, 0, 31, 8, 0\n" /* setup register frame */
515                 "\tld8 r18 = [r32],8\n"                 /* get procedure address */
516                 "\tadds sp = -%0, sp ;;\n"              /* setup stack */
517                 "\tld8 gp = [r32]\n"                    /* get procedure GP */
518                 "\tadds r16 = %0-(6*16), sp\n"
519                 "\tadds r17 = %0-(5*16), sp ;;\n"
520                 "\tstf.spill [r16] = f16,32\n"          /* spill callee-saved fp regs */
521                 "\tstf.spill [r17] = f17,32\n"
522                 "\tmov b6 = r18 ;;\n"                   /* set target address */
523                 "\tstf.spill [r16] = f18,32\n"
524                 "\tstf.spill [r17] = f19,32\n"
525                 "\tmov loc30 = b0 ;;\n"                 /* save return address */
526                 "\tstf.spill [r16] = f20,32\n"
527                 "\tstf.spill [r17] = f21,32\n"
528                 "\tbr.few b6 ;;\n"                      /* branch to function */
529                 ".global StgReturn\n"
530                 "StgReturn:\n"
531                 "\tmov r8 = loc16\n"            /* return value in r8 */
532                 "\tadds r16 = %0-(6*16), sp\n"
533                 "\tadds r17 = %0-(5*16), sp ;;\n"
534                 "\tldf.fill f16 = [r16],32\n"   /* start restoring fp regs */
535                 "\tldf.fill f17 = [r17],32\n"
536                 "\tmov ar.pfs = loc29 ;;\n"     /* restore register frame */
537                 "\tldf.fill f18 = [r16],32\n"
538                 "\tldf.fill f19 = [r17],32\n"
539                 "\tmov b0 = loc30 ;;\n"         /* restore return address */
540                 "\tldf.fill f20 = [r16],32\n"
541                 "\tldf.fill f21 = [r17],32\n"
542                 "\tadds sp = %0, sp\n"          /* restore stack */
543                 "\tbr.ret.sptk.many b0 ;;\n"    /* return */
544         : : "i"(RESERVED_C_STACK_BYTES + 6*16));
545 }
546
547 #endif
548
549 #endif /* !USE_MINIINTERPRETER */
550