[project @ 2005-05-27 14:47:08 by tharris]
[ghc-hetmet.git] / ghc / rts / StgCRun.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2003
4  *
5  * STG-to-C glue.
6  *
7  * To run an STG function from C land, call
8  *
9  *              rv = StgRun(f,BaseReg);
10  *
11  * where "f" is the STG function to call, and BaseReg is the address of the
12  * RegTable for this run (we might have separate RegTables if we're running
13  * multiple threads on an SMP machine).
14  *
15  * In the end, "f" must JMP to StgReturn (defined below),
16  * passing the return-value "rv" in R1,
17  * to return to the caller of StgRun returning "rv" in
18  * the whatever way C returns a value.
19  *
20  * NOTE: StgRun/StgReturn do *NOT* load or store Hp or any
21  * other registers (other than saving the C callee-saves
22  * registers).  Instead, the called function "f" must do that
23  * in STG land.
24  *
25  * GCC will have assumed that pushing/popping of C-stack frames is
26  * going on when it generated its code, and used stack space
27  * accordingly.  However, we actually {\em post-process away} all
28  * such stack-framery (see \tr{ghc/driver/ghc-asm.lprl}). Things will
29  * be OK however, if we initially make sure there are
30  * @RESERVED_C_STACK_BYTES@ on the C-stack to begin with, for local
31  * variables.
32  *
33  * -------------------------------------------------------------------------- */
34
35 #include "PosixSource.h"
36
37
38 /*
39  * We define the following (unused) global register variables, because for
40  * some reason gcc generates sub-optimal code for StgRun() on the Alpha
41  * (unnecessarily saving extra registers on the stack) if we don't.
42  *
43  * Why do it at the top of this file, rather than near StgRun() below?  Because
44  * gcc doesn't let us define global register variables after any function
45  * definition has been read.  Any point after #include "Stg.h" would be too
46  * late.
47  *
48  * We define alpha_EXTRA_CAREFUL here to save $s6, $f8 and $f9 -- registers
49  * that we don't use but which are callee-save registers.  The __divq() routine
50  * in libc.a clobbers $s6.
51  */
52 #include "ghcconfig.h"
53 #ifdef alpha_HOST_ARCH
54 #define alpha_EXTRA_CAREFUL
55 register long   fake_ra __asm__("$26");
56 register long   fake_gp __asm__("$29");
57 #ifdef alpha_EXTRA_CAREFUL
58 register long   fake_s6 __asm__("$15");
59 register double fake_f8 __asm__("$f8");
60 register double fake_f9 __asm__("$f9");
61 #endif
62 #endif
63
64 /* include Stg.h first because we want real machine regs in here: we
65  * have to get the value of R1 back from Stg land to C land intact.
66  */
67 #include "Stg.h"
68 #include "Rts.h"
69 #include "StgRun.h"
70 #include "RtsFlags.h"
71 #include "OSThreads.h"
72 #include "Capability.h"
73
74 #ifdef DEBUG
75 #include "RtsUtils.h"
76 #include "Printer.h"
77 #endif
78
79 #ifdef USE_MINIINTERPRETER
80
81 /* -----------------------------------------------------------------------------
82    any architecture (using miniinterpreter)
83    -------------------------------------------------------------------------- */
84
85 StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg STG_UNUSED)
86 {
87     while (f) {
88         if (RtsFlags[0].DebugFlags.interpreter) {
89             debugBelch("Jumping to ");
90             printPtr((P_)f); fflush(stdout);
91             debugBelch("\n");
92         }
93         f = (StgFunPtr) (f)();
94     }
95     return (StgThreadReturnCode)R1.i;
96 }
97
98 StgFunPtr StgReturn(void)
99 {
100     return 0;
101 }
102
103 #else /* !USE_MINIINTERPRETER */
104
105 #ifdef LEADING_UNDERSCORE
106 #define STG_RETURN "_StgReturn"
107 #else
108 #define STG_RETURN "StgReturn"
109 #endif
110
111 /* -----------------------------------------------------------------------------
112    x86 architecture
113    -------------------------------------------------------------------------- */
114
115 #ifdef i386_HOST_ARCH
116
117 StgThreadReturnCode
118 StgRun(StgFunPtr f, StgRegTable *basereg) {
119
120     unsigned char space[ RESERVED_C_STACK_BYTES + 4*sizeof(void *) ];
121     StgThreadReturnCode r;
122
123     __asm__ volatile (
124         /*
125          * save callee-saves registers on behalf of the STG code.
126          */
127         "movl %%esp, %%eax\n\t"
128         "addl %4, %%eax\n\t"
129         "movl %%ebx,0(%%eax)\n\t"
130         "movl %%esi,4(%%eax)\n\t"
131         "movl %%edi,8(%%eax)\n\t"
132         "movl %%ebp,12(%%eax)\n\t"
133         /*
134          * Set BaseReg
135          */
136         "movl %3,%%ebx\n\t"
137         /*
138          * grab the function argument from the stack, and jump to it.
139          */
140         "movl %2,%%eax\n\t"
141         "jmp *%%eax\n\t"
142
143         ".global " STG_RETURN "\n"
144         STG_RETURN ":\n\t"
145
146         "movl %%esi, %%eax\n\t"   /* Return value in R1  */
147
148         /*
149          * restore callee-saves registers.  (Don't stomp on %%eax!)
150          */
151         "movl %%esp, %%edx\n\t"
152         "addl %4, %%edx\n\t"
153         "movl 0(%%edx),%%ebx\n\t"       /* restore the registers saved above */
154         "movl 4(%%edx),%%esi\n\t"
155         "movl 8(%%edx),%%edi\n\t"
156         "movl 12(%%edx),%%ebp\n\t"
157
158       : "=&a" (r), "=m" (space)
159       : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES)
160       : "edx" /* stomps on %edx */
161     );
162
163     return r;
164 }
165
166 #endif
167
168 /* ----------------------------------------------------------------------------
169    x86-64 is almost the same as plain x86.
170
171    I've done it using entirely inline assembler, because I couldn't
172    get gcc to generate the correct subtraction from %rsp by using
173    the local array variable trick.  It didn't seem to reserve
174    enough space.  Oh well, it's not much harder this way.
175
176    ------------------------------------------------------------------------- */
177
178 #ifdef x86_64_HOST_ARCH
179
180 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg);
181
182 static void StgRunIsImplementedInAssembler(void)
183 {
184     __asm__ volatile (
185         /*
186          * save callee-saves registers on behalf of the STG code.
187          */
188         ".globl StgRun\n"
189         "StgRun:\n\t"
190         "subq %0, %%rsp\n\t"
191         "movq %%rsp, %%rax\n\t"
192         "addq %0-48, %%rax\n\t"
193         "movq %%rbx,0(%%rax)\n\t"
194         "movq %%rbp,8(%%rax)\n\t"
195         "movq %%r12,16(%%rax)\n\t"
196         "movq %%r13,24(%%rax)\n\t"
197         "movq %%r14,32(%%rax)\n\t"
198         "movq %%r15,40(%%rax)\n\t"
199         /*
200          * Set BaseReg
201          */
202         "movq %%rsi,%%rbx\n\t"
203         /*
204          * grab the function argument from the stack, and jump to it.
205          */
206         "movq %%rdi,%%rax\n\t"
207         "jmp *%%rax\n\t"
208
209         ".global " STG_RETURN "\n"
210         STG_RETURN ":\n\t"
211
212         "movq %%r13, %%rax\n\t"   /* Return value in R1  */
213
214         /*
215          * restore callee-saves registers.  (Don't stomp on %%rax!)
216          */
217         "movq %%rsp, %%rdx\n\t"
218         "addq %0-48, %%rdx\n\t"
219         "movq 0(%%rdx),%%rbx\n\t"       /* restore the registers saved above */
220         "movq 8(%%rdx),%%rbp\n\t"
221         "movq 16(%%rdx),%%r12\n\t"
222         "movq 24(%%rdx),%%r13\n\t"
223         "movq 32(%%rdx),%%r14\n\t"
224         "movq 40(%%rdx),%%r15\n\t"
225         "addq %0, %%rsp\n\t"
226         "retq"
227
228         : : "i"(RESERVED_C_STACK_BYTES+48+8 /*stack frame size*/));
229     /* 
230        HACK alert!
231
232        The x86_64 ABI specifies that on a procedure call, %rsp is
233        aligned on a 16-byte boundary + 8.  That is, the first
234        argument on the stack after the return address will be
235        16-byte aligned.  
236        
237        Which should be fine: RESERVED_C_STACK_BYTES+48 is a multiple
238        of 16 bytes.  
239        
240        BUT... when we do a C-call from STG land, gcc likes to put the
241        stack alignment adjustment in the prolog.  eg. if we're calling
242        a function with arguments in regs, gcc will insert 'subq $8,%rsp'
243        in the prolog, to keep %rsp aligned (the return address is 8
244        bytes, remember).  The mangler throws away the prolog, so we
245        lose the stack alignment.
246
247        The hack is to add this extra 8 bytes to our %rsp adjustment
248        here, so that throughout STG code, %rsp is 16-byte aligned,
249        ready for a C-call.  
250
251        A quick way to see if this is wrong is to compile this code:
252
253           main = System.Exit.exitWith ExitSuccess
254
255        And run it with +RTS -sstderr.  The stats code in the RTS, in
256        particular statsPrintf(), relies on the stack alignment because
257        it saves the %xmm regs on the stack, so it'll fall over if the
258        stack isn't aligned, and calling exitWith from Haskell invokes
259        shutdownHaskellAndExit using a C call.
260
261        Future gcc releases will almost certainly break this hack...
262     */
263 }
264
265 #endif /* x86-64 */
266
267 /* -----------------------------------------------------------------------------
268    Sparc architecture
269
270    --
271    OLD COMMENT from GHC-3.02:
272
273    We want tailjumps to be calls, because `call xxx' is the only Sparc
274    branch that allows an arbitrary label as a target.  (Gcc's ``goto
275    *target'' construct ends up loading the label into a register and
276    then jumping, at the cost of two extra instructions for the 32-bit
277    load.)
278
279    When entering the threaded world, we stash our return address in a
280    known location so that \tr{%i7} is available as an extra
281    callee-saves register.  Of course, we have to restore this when
282    coming out of the threaded world.
283
284    I hate this god-forsaken architecture.  Since the top of the
285    reserved stack space is used for globals and the bottom is reserved
286    for outgoing arguments, we have to stick our return address
287    somewhere in the middle.  Currently, I'm allowing 100 extra
288    outgoing arguments beyond the first 6.  --JSM
289
290    Updated info (GHC 4.06): we don't appear to use %i7 any more, so
291    I'm not sure whether we still need to save it.  Incedentally, what
292    does the last paragraph above mean when it says "the top of the
293    stack is used for globals"?  What globals?  --SDM
294
295    Updated info (GHC 4.08.2): not saving %i7 any more (see below).
296    -------------------------------------------------------------------------- */
297
298 #ifdef sparc_HOST_ARCH
299
300 StgThreadReturnCode
301 StgRun(StgFunPtr f, StgRegTable *basereg) {
302
303     unsigned char space[RESERVED_C_STACK_BYTES];
304 #if 0
305     register void *i7 __asm__("%i7");
306     ((void **)(space))[100] = i7;
307 #endif
308     f();
309     __asm__ volatile (
310             ".align 4\n"
311             ".global " STG_RETURN "\n"
312             STG_RETURN ":"
313             : : : "l0","l1","l2","l3","l4","l5","l6","l7");
314     /* we tell the C compiler that l0-l7 are clobbered on return to
315      * StgReturn, otherwise it tries to use these to save eg. the
316      * address of space[100] across the call.  The correct thing
317      * to do would be to save all the callee-saves regs, but we
318      * can't be bothered to do that.
319      *
320      * The code that gcc generates for this little fragment is now
321      * terrible.  We could do much better by coding it directly in
322      * assembler.
323      */
324 #if 0
325     /* updated 4.08.2: we don't save %i7 in the middle of the reserved
326      * space any more, since gcc tries to save its address across the
327      * call to f(), this gets clobbered in STG land and we end up
328      * dereferencing a bogus pointer in StgReturn.
329      */
330     __asm__ volatile ("ld %1,%0"
331                       : "=r" (i7) : "m" (((void **)(space))[100]));
332 #endif
333     return (StgThreadReturnCode)R1.i;
334 }
335
336 #endif
337
338 /* -----------------------------------------------------------------------------
339    alpha architecture
340
341    "The stack pointer (SP) must at all times denote an address that has octaword
342     alignment. (This restriction has the side effect that the in-memory portion
343     of the argument list, if any, will start on an octaword boundary.) Note that
344     the stack grows toward lower addresses. During a procedure invocation, SP
345     can never be set to a value that is higher than the value of SP at entry to
346     that procedure invocation.
347
348    "The contents of the stack, located above the portion of the argument list
349     (if any) that is passed in memory, belong to the calling procedure. Because
350     they are part of the calling procedure, they should not be read or written
351     by the called procedure, except as specified by indirect arguments or
352     language-controlled up-level references.
353
354    "The SP value might be used by the hardware when raising exceptions and
355     asynchronous interrupts. It must be assumed that the contents of the stack
356     below the current SP value and within the stack for the current thread are
357     continually and unpredictably modified, as specified in the _Alpha
358     Architecture Reference Manual_, and as a result of asynchronous software
359     actions."
360
361    -- Compaq Computer Corporation, Houston. Tru64 UNIX Calling Standard for
362       Alpha Systems, 5.1 edition, August 2000, section 3.2.1.  http://www.
363       tru64unix.compaq.com/docs/base_doc/DOCUMENTATION/V51_PDF/ARH9MBTE.PDF
364    -------------------------------------------------------------------------- */
365
366 #ifdef alpha_HOST_ARCH
367
368 StgThreadReturnCode
369 StgRun(StgFunPtr f, StgRegTable *basereg)
370 {
371     register long   real_ra __asm__("$26"); volatile long   save_ra;
372     register long   real_gp __asm__("$29"); volatile long   save_gp;
373
374     register long   real_s0 __asm__("$9" ); volatile long   save_s0;
375     register long   real_s1 __asm__("$10"); volatile long   save_s1;
376     register long   real_s2 __asm__("$11"); volatile long   save_s2;
377     register long   real_s3 __asm__("$12"); volatile long   save_s3;
378     register long   real_s4 __asm__("$13"); volatile long   save_s4;
379     register long   real_s5 __asm__("$14"); volatile long   save_s5;
380 #ifdef alpha_EXTRA_CAREFUL
381     register long   real_s6 __asm__("$15"); volatile long   save_s6;
382 #endif
383
384     register double real_f2 __asm__("$f2"); volatile double save_f2;
385     register double real_f3 __asm__("$f3"); volatile double save_f3;
386     register double real_f4 __asm__("$f4"); volatile double save_f4;
387     register double real_f5 __asm__("$f5"); volatile double save_f5;
388     register double real_f6 __asm__("$f6"); volatile double save_f6;
389     register double real_f7 __asm__("$f7"); volatile double save_f7;
390 #ifdef alpha_EXTRA_CAREFUL
391     register double real_f8 __asm__("$f8"); volatile double save_f8;
392     register double real_f9 __asm__("$f9"); volatile double save_f9;
393 #endif
394
395     register StgFunPtr real_pv __asm__("$27");
396
397     StgThreadReturnCode ret;
398
399     save_ra = real_ra;
400     save_gp = real_gp;
401
402     save_s0 = real_s0;
403     save_s1 = real_s1;
404     save_s2 = real_s2;
405     save_s3 = real_s3;
406     save_s4 = real_s4;
407     save_s5 = real_s5;
408 #ifdef alpha_EXTRA_CAREFUL
409     save_s6 = real_s6;
410 #endif
411
412     save_f2 = real_f2;
413     save_f3 = real_f3;
414     save_f4 = real_f4;
415     save_f5 = real_f5;
416     save_f6 = real_f6;
417     save_f7 = real_f7;
418 #ifdef alpha_EXTRA_CAREFUL
419     save_f8 = real_f8;
420     save_f9 = real_f9;
421 #endif
422
423     real_pv = f;
424
425     __asm__ volatile(   "lda $30,-%0($30)"      "\n"
426                 "\t"    "jmp ($27)"             "\n"
427                 "\t"    ".align 3"              "\n"
428                 ".globl " STG_RETURN            "\n"
429                 STG_RETURN ":"                  "\n"
430                 "\t"    "lda $30,%0($30)"       "\n"
431                 : : "K" (RESERVED_C_STACK_BYTES));
432
433     ret = real_s5;
434
435     real_s0 = save_s0;
436     real_s1 = save_s1;
437     real_s2 = save_s2;
438     real_s3 = save_s3;
439     real_s4 = save_s4;
440     real_s5 = save_s5;
441 #ifdef alpha_EXTRA_CAREFUL
442     real_s6 = save_s6;
443 #endif
444
445     real_f2 = save_f2;
446     real_f3 = save_f3;
447     real_f4 = save_f4;
448     real_f5 = save_f5;
449     real_f6 = save_f6;
450     real_f7 = save_f7;
451 #ifdef alpha_EXTRA_CAREFUL
452     real_f8 = save_f8;
453     real_f9 = save_f9;
454 #endif
455
456     real_ra = save_ra;
457     real_gp = save_gp;
458
459     return ret;
460 }
461
462 #endif /* alpha_HOST_ARCH */
463
464 /* -----------------------------------------------------------------------------
465    HP-PA architecture
466    -------------------------------------------------------------------------- */
467
468 #ifdef hppa1_1_HOST_ARCH
469
470 StgThreadReturnCode
471 StgRun(StgFunPtr f, StgRegTable *basereg)
472 {
473     StgChar space[RESERVED_C_STACK_BYTES+16*sizeof(long)+10*sizeof(double)];
474     StgThreadReturnCode ret;
475
476     __asm__ volatile ("ldo %0(%%r30),%%r19\n"
477                       "\tstw %%r3, 0(0,%%r19)\n"
478                       "\tstw %%r4, 4(0,%%r19)\n"
479                       "\tstw %%r5, 8(0,%%r19)\n"
480                       "\tstw %%r6,12(0,%%r19)\n"
481                       "\tstw %%r7,16(0,%%r19)\n"
482                       "\tstw %%r8,20(0,%%r19)\n"
483                       "\tstw %%r9,24(0,%%r19)\n"
484                       "\tstw %%r10,28(0,%%r19)\n"
485                       "\tstw %%r11,32(0,%%r19)\n"
486                       "\tstw %%r12,36(0,%%r19)\n"
487                       "\tstw %%r13,40(0,%%r19)\n"
488                       "\tstw %%r14,44(0,%%r19)\n"
489                       "\tstw %%r15,48(0,%%r19)\n"
490                       "\tstw %%r16,52(0,%%r19)\n"
491                       "\tstw %%r17,56(0,%%r19)\n"
492                       "\tstw %%r18,60(0,%%r19)\n"
493                       "\tldo 80(%%r19),%%r19\n"
494                       "\tfstds %%fr12,-16(0,%%r19)\n"
495                       "\tfstds %%fr13, -8(0,%%r19)\n"
496                       "\tfstds %%fr14,  0(0,%%r19)\n"
497                       "\tfstds %%fr15,  8(0,%%r19)\n"
498                       "\tldo 32(%%r19),%%r19\n"
499                       "\tfstds %%fr16,-16(0,%%r19)\n"
500                       "\tfstds %%fr17, -8(0,%%r19)\n"
501                       "\tfstds %%fr18,  0(0,%%r19)\n"
502                       "\tfstds %%fr19,  8(0,%%r19)\n"
503                       "\tldo 32(%%r19),%%r19\n"
504                       "\tfstds %%fr20,-16(0,%%r19)\n"
505                       "\tfstds %%fr21, -8(0,%%r19)\n" : :
506                       "n" (-(116 * sizeof(long) + 10 * sizeof(double))) : "%r19"
507                       );
508
509     f();
510
511     __asm__ volatile (".align 4\n"
512                       "\t.EXPORT " STG_RETURN ",CODE\n"
513                       "\t.EXPORT " STG_RETURN ",ENTRY,PRIV_LEV=3\n"
514                       STG_RETURN "\n"
515                       /* "\tldo %0(%%r3),%%r19\n" */
516                       "\tldo %1(%%r30),%%r19\n"
517                       "\tcopy %%r11, %0\n"  /* save R1 */
518                       "\tldw  0(0,%%r19),%%r3\n"
519                       "\tldw  4(0,%%r19),%%r4\n"
520                       "\tldw  8(0,%%r19),%%r5\n"
521                       "\tldw 12(0,%%r19),%%r6\n"
522                       "\tldw 16(0,%%r19),%%r7\n"
523                       "\tldw 20(0,%%r19),%%r8\n"
524                       "\tldw 24(0,%%r19),%%r9\n"
525                       "\tldw 28(0,%%r19),%%r10\n"
526                       "\tldw 32(0,%%r19),%%r11\n"
527                       "\tldw 36(0,%%r19),%%r12\n"
528                       "\tldw 40(0,%%r19),%%r13\n"
529                       "\tldw 44(0,%%r19),%%r14\n"
530                       "\tldw 48(0,%%r19),%%r15\n"
531                       "\tldw 52(0,%%r19),%%r16\n"
532                       "\tldw 56(0,%%r19),%%r17\n"
533                       "\tldw 60(0,%%r19),%%r18\n"
534                       "\tldo 80(%%r19),%%r19\n"
535                       "\tfldds -16(0,%%r19),%%fr12\n"
536                       "\tfldds  -8(0,%%r19),%%fr13\n"
537                       "\tfldds   0(0,%%r19),%%fr14\n"
538                       "\tfldds   8(0,%%r19),%%fr15\n"
539                       "\tldo 32(%%r19),%%r19\n"
540                       "\tfldds -16(0,%%r19),%%fr16\n"
541                       "\tfldds  -8(0,%%r19),%%fr17\n"
542                       "\tfldds   0(0,%%r19),%%fr18\n"
543                       "\tfldds   8(0,%%r19),%%fr19\n"
544                       "\tldo 32(%%r19),%%r19\n"
545                       "\tfldds -16(0,%%r19),%%fr20\n"
546                       "\tfldds  -8(0,%%r19),%%fr21\n"
547                          : "=r" (ret)
548                          : "n" (-(116 * sizeof(long) + 10 * sizeof(double)))
549                          : "%r19"
550                       );
551
552     return ret;
553 }
554
555 #endif /* hppa1_1_HOST_ARCH */
556
557 /* -----------------------------------------------------------------------------
558    PowerPC architecture
559
560    Everything is in assembler, so we don't have to deal with GCC...
561    
562    -------------------------------------------------------------------------- */
563
564 #ifdef powerpc_HOST_ARCH
565
566 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg);
567
568 #ifdef darwin_HOST_OS
569 static void StgRunIsImplementedInAssembler(void)
570 {
571 #if HAVE_SUBSECTIONS_VIA_SYMBOLS
572             // if the toolchain supports deadstripping, we have to
573             // prevent it here (it tends to get confused here).
574         __asm__ volatile (".no_dead_strip _StgRunIsImplementedInAssembler");
575 #endif
576         __asm__ volatile (
577                 "\n.globl _StgRun\n"
578                 "_StgRun:\n"
579                 "\tmflr r0\n"
580                 "\tbl saveFP # f14\n"
581                 "\tstmw r13,-220(r1)\n"
582                 "\tstwu r1,-%0(r1)\n"
583                 "\tmr r27,r4\n" // BaseReg == r27
584                 "\tmtctr r3\n"
585                 "\tmr r12,r3\n"
586                 "\tbctr\n"
587                 ".globl _StgReturn\n"
588                 "_StgReturn:\n"
589                 "\tmr r3,r14\n"
590                 "\tla r1,%0(r1)\n"
591                 "\tlmw r13,-220(r1)\n"
592                 "\tb restFP # f14\n"
593         : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
594 }
595 #else
596
597 // This version is for PowerPC Linux.
598
599 // Differences from the Darwin/Mac OS X version:
600 // *) Different Assembler Syntax
601 // *) Doesn't use Register Saving Helper Functions (although they exist somewhere)
602 // *) We may not access positive stack offsets
603 //    (no "Red Zone" as in the Darwin ABI)
604 // *) The Link Register is saved to a different offset in the caller's stack frame
605 //    (Linux: 4(r1), Darwin 8(r1))
606
607 static void StgRunIsImplementedInAssembler(void)
608 {
609         __asm__ volatile (
610                 "\t.globl StgRun\n"
611                 "\t.type StgRun,@function\n"
612                 "StgRun:\n"
613                 "\tmflr 0\n"
614                 "\tstw 0,4(1)\n"
615                 "\tmr 5,1\n"
616                 "\tstwu 1,-%0(1)\n"
617                 "\tstmw 13,-220(5)\n"
618                 "\tstfd 14,-144(5)\n"
619                 "\tstfd 15,-136(5)\n"
620                 "\tstfd 16,-128(5)\n"
621                 "\tstfd 17,-120(5)\n"
622                 "\tstfd 18,-112(5)\n"
623                 "\tstfd 19,-104(5)\n"
624                 "\tstfd 20,-96(5)\n"
625                 "\tstfd 21,-88(5)\n"
626                 "\tstfd 22,-80(5)\n"
627                 "\tstfd 23,-72(5)\n"
628                 "\tstfd 24,-64(5)\n"
629                 "\tstfd 25,-56(5)\n"
630                 "\tstfd 26,-48(5)\n"
631                 "\tstfd 27,-40(5)\n"
632                 "\tstfd 28,-32(5)\n"
633                 "\tstfd 29,-24(5)\n"
634                 "\tstfd 30,-16(5)\n"
635                 "\tstfd 31,-8(5)\n"
636                 "\tmr 27,4\n"  // BaseReg == r27
637                 "\tmtctr 3\n"
638                 "\tmr 12,3\n"
639                 "\tbctr\n"
640                 ".globl StgReturn\n"
641                 "\t.type StgReturn,@function\n"
642                 "StgReturn:\n"
643                 "\tmr 3,14\n"
644                 "\tla 5,%0(1)\n"
645                 "\tlmw 13,-220(5)\n"
646                 "\tlfd 14,-144(5)\n"
647                 "\tlfd 15,-136(5)\n"
648                 "\tlfd 16,-128(5)\n"
649                 "\tlfd 17,-120(5)\n"
650                 "\tlfd 18,-112(5)\n"
651                 "\tlfd 19,-104(5)\n"
652                 "\tlfd 20,-96(5)\n"
653                 "\tlfd 21,-88(5)\n"
654                 "\tlfd 22,-80(5)\n"
655                 "\tlfd 23,-72(5)\n"
656                 "\tlfd 24,-64(5)\n"
657                 "\tlfd 25,-56(5)\n"
658                 "\tlfd 26,-48(5)\n"
659                 "\tlfd 27,-40(5)\n"
660                 "\tlfd 28,-32(5)\n"
661                 "\tlfd 29,-24(5)\n"
662                 "\tlfd 30,-16(5)\n"
663                 "\tlfd 31,-8(5)\n"
664                 "\tmr 1,5\n"
665                 "\tlwz 0,4(1)\n"
666                 "\tmtlr 0\n"
667                 "\tblr\n"
668         : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
669 }
670 #endif
671
672 #endif
673
674 /* -----------------------------------------------------------------------------
675    PowerPC 64 architecture
676
677    Everything is in assembler, so we don't have to deal with GCC...
678    
679    -------------------------------------------------------------------------- */
680
681 #ifdef powerpc64_HOST_ARCH
682
683 #ifdef linux_HOST_OS
684 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg);
685
686 static void StgRunIsImplementedInAssembler(void)
687 {
688         // r0 volatile
689         // r1 stack pointer
690         // r2 toc - needs to be saved
691         // r3-r10 argument passing, volatile
692         // r11, r12 very volatile (not saved across cross-module calls)
693         // r13 thread local state (never modified, don't need to save)
694         // r14-r31 callee-save
695         __asm__ volatile (
696                 ".section \".opd\",\"aw\"\n"
697                 ".align 3\n"
698                 ".globl StgRun\n"
699                 "StgRun:\n"
700                         "\t.quad\t.StgRun,.TOC.@tocbase,0\n"
701                         "\t.size StgRun,24\n"
702                 ".globl StgReturn\n"
703                 "StgReturn:\n"
704                         "\t.quad\t.StgReturn,.TOC.@tocbase,0\n"
705                         "\t.size StgReturn,24\n"
706                 ".previous\n"
707                 ".globl .StgRun\n"
708                 ".type .StgRun,@function\n"
709                 ".StgRun:\n"
710                         "\tmflr 0\n"
711                         "\tmr 5, 1\n"
712                         "\tstd 0, 16(1)\n"
713                         "\tstdu 1, -%0(1)\n"
714                         "\tstd 2, -296(5)\n"
715                         "\tstd 14, -288(5)\n"
716                         "\tstd 15, -280(5)\n"
717                         "\tstd 16, -272(5)\n"
718                         "\tstd 17, -264(5)\n"
719                         "\tstd 18, -256(5)\n"
720                         "\tstd 19, -248(5)\n"
721                         "\tstd 20, -240(5)\n"
722                         "\tstd 21, -232(5)\n"
723                         "\tstd 22, -224(5)\n"
724                         "\tstd 23, -216(5)\n"
725                         "\tstd 24, -208(5)\n"
726                         "\tstd 25, -200(5)\n"
727                         "\tstd 26, -192(5)\n"
728                         "\tstd 27, -184(5)\n"
729                         "\tstd 28, -176(5)\n"
730                         "\tstd 29, -168(5)\n"
731                         "\tstd 30, -160(5)\n"
732                         "\tstd 31, -152(5)\n"
733                         "\tstfd 14, -144(5)\n"
734                         "\tstfd 15, -136(5)\n"
735                         "\tstfd 16, -128(5)\n"
736                         "\tstfd 17, -120(5)\n"
737                         "\tstfd 18, -112(5)\n"
738                         "\tstfd 19, -104(5)\n"
739                         "\tstfd 20, -96(5)\n"
740                         "\tstfd 21, -88(5)\n"
741                         "\tstfd 22, -80(5)\n"
742                         "\tstfd 23, -72(5)\n"
743                         "\tstfd 24, -64(5)\n"
744                         "\tstfd 25, -56(5)\n"
745                         "\tstfd 26, -48(5)\n"
746                         "\tstfd 27, -40(5)\n"
747                         "\tstfd 28, -32(5)\n"
748                         "\tstfd 29, -24(5)\n"
749                         "\tstfd 30, -16(5)\n"
750                         "\tstfd 31, -8(5)\n"
751                         "\tmr 27, 4\n"  // BaseReg == r27
752                         "\tld 2, 8(3)\n"
753                         "\tld 3, 0(3)\n"
754                         "\tmtctr 3\n"
755                         "\tbctr\n"
756                 ".globl .StgReturn\n"
757                 ".type .StgReturn,@function\n"
758                 ".StgReturn:\n"
759                         "\tmr 3,14\n"
760                         "\tla 5, %0(1)\n" // load address == addi r5, r1, %0
761                         "\tld 2, -296(5)\n"
762                         "\tld 14, -288(5)\n"
763                         "\tld 15, -280(5)\n"
764                         "\tld 16, -272(5)\n"
765                         "\tld 17, -264(5)\n"
766                         "\tld 18, -256(5)\n"
767                         "\tld 19, -248(5)\n"
768                         "\tld 20, -240(5)\n"
769                         "\tld 21, -232(5)\n"
770                         "\tld 22, -224(5)\n"
771                         "\tld 23, -216(5)\n"
772                         "\tld 24, -208(5)\n"
773                         "\tld 25, -200(5)\n"
774                         "\tld 26, -192(5)\n"
775                         "\tld 27, -184(5)\n"
776                         "\tld 28, -176(5)\n"
777                         "\tld 29, -168(5)\n"
778                         "\tld 30, -160(5)\n"
779                         "\tld 31, -152(5)\n"
780                         "\tlfd 14, -144(5)\n"
781                         "\tlfd 15, -136(5)\n"
782                         "\tlfd 16, -128(5)\n"
783                         "\tlfd 17, -120(5)\n"
784                         "\tlfd 18, -112(5)\n"
785                         "\tlfd 19, -104(5)\n"
786                         "\tlfd 20, -96(5)\n"
787                         "\tlfd 21, -88(5)\n"
788                         "\tlfd 22, -80(5)\n"
789                         "\tlfd 23, -72(5)\n"
790                         "\tlfd 24, -64(5)\n"
791                         "\tlfd 25, -56(5)\n"
792                         "\tlfd 26, -48(5)\n"
793                         "\tlfd 27, -40(5)\n"
794                         "\tlfd 28, -32(5)\n"
795                         "\tlfd 29, -24(5)\n"
796                         "\tlfd 30, -16(5)\n"
797                         "\tlfd 31, -8(5)\n"
798                         "\tmr 1, 5\n"
799                         "\tld 0, 16(1)\n"
800                         "\tmtlr 0\n"
801                         "\tblr\n"
802         : : "i"(RESERVED_C_STACK_BYTES+304 /*stack frame size*/));
803 }
804 #else // linux_HOST_OS
805 #error Only linux support for power64 right now.
806 #endif
807
808 #endif
809
810 /* -----------------------------------------------------------------------------
811    IA64 architecture
812
813    Again, in assembler - so we can fiddle with the register stack, and because
814    gcc doesn't handle asm-clobbered callee-saves correctly.
815
816    loc0  - loc15: preserved locals
817    loc16 - loc28: STG registers
818            loc29: saved ar.pfs
819            loc30: saved b0
820            loc31: saved gp (gcc 3.3 uses this slot)
821    -------------------------------------------------------------------------- */
822
823 #ifdef ia64_HOST_ARCH
824
825 /* the memory stack is rarely used, so 16K is excessive */
826 #undef RESERVED_C_STACK_BYTES
827 #define RESERVED_C_STACK_BYTES 1024
828
829 #if ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 3)
830 /* gcc 3.3+: leave an extra slot for gp saves */
831 #define LOCALS 32
832 #else
833 #define LOCALS 31
834 #endif
835
836 static void StgRunIsImplementedInAssembler(void)
837 {
838     __asm__ volatile(
839                 ".global StgRun\n"
840                 "StgRun:\n"
841                 "\talloc loc29 = ar.pfs, 0, %1, 8, 0\n" /* setup register frame */
842                 "\tld8 r18 = [r32],8\n"                 /* get procedure address */
843                 "\tadds sp = -%0, sp ;;\n"              /* setup stack */
844                 "\tld8 gp = [r32]\n"                    /* get procedure GP */
845                 "\tadds r16 = %0-(6*16), sp\n"
846                 "\tadds r17 = %0-(5*16), sp ;;\n"
847                 "\tstf.spill [r16] = f16,32\n"          /* spill callee-saved fp regs */
848                 "\tstf.spill [r17] = f17,32\n"
849                 "\tmov b6 = r18 ;;\n"                   /* set target address */
850                 "\tstf.spill [r16] = f18,32\n"
851                 "\tstf.spill [r17] = f19,32\n"
852                 "\tmov loc30 = b0 ;;\n"                 /* save return address */
853                 "\tstf.spill [r16] = f20,32\n"
854                 "\tstf.spill [r17] = f21,32\n"
855                 "\tbr.few b6 ;;\n"                      /* branch to function */
856                 ".global StgReturn\n"
857                 "StgReturn:\n"
858                 "\tmov r8 = loc16\n"            /* return value in r8 */
859                 "\tadds r16 = %0-(6*16), sp\n"
860                 "\tadds r17 = %0-(5*16), sp ;;\n"
861                 "\tldf.fill f16 = [r16],32\n"   /* start restoring fp regs */
862                 "\tldf.fill f17 = [r17],32\n"
863                 "\tmov ar.pfs = loc29 ;;\n"     /* restore register frame */
864                 "\tldf.fill f18 = [r16],32\n"
865                 "\tldf.fill f19 = [r17],32\n"
866                 "\tmov b0 = loc30 ;;\n"         /* restore return address */
867                 "\tldf.fill f20 = [r16],32\n"
868                 "\tldf.fill f21 = [r17],32\n"
869                 "\tadds sp = %0, sp\n"          /* restore stack */
870                 "\tbr.ret.sptk.many b0 ;;\n"    /* return */
871         : : "i"(RESERVED_C_STACK_BYTES + 6*16), "i"(LOCALS));
872 }
873
874 #endif
875
876 #endif /* !USE_MINIINTERPRETER */