[project @ 2002-02-06 15:48:56 by sewardj]
[ghc-hetmet.git] / ghc / rts / StgCRun.c
1 /* -----------------------------------------------------------------------------
2  * $Id: StgCRun.c,v 1.28 2002/02/06 15:48:56 sewardj 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 #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
71 #ifdef DEBUG
72 #include "RtsFlags.h"
73 #include "RtsUtils.h"
74 #include "Printer.h"
75 #endif
76
77 #ifdef USE_MINIINTERPRETER
78
79 /* -----------------------------------------------------------------------------
80    any architecture (using miniinterpreter)
81    -------------------------------------------------------------------------- */
82
83 extern StgThreadReturnCode StgRun(StgFunPtr f, StgRegTable *basereg)
84 {
85    while (f) {
86       IF_DEBUG(evaluator,
87                fprintf(stderr,"Jumping to ");
88                printPtr((P_)f);
89                fprintf(stderr,"\n");
90               );
91       f = (StgFunPtr) (f)();
92    }
93    return (StgThreadReturnCode)R1.i;
94 }
95
96 EXTFUN(StgReturn)
97 {
98    return 0;
99 }
100
101 #else /* !USE_MINIINTERPRETER */
102
103 #ifdef LEADING_UNDERSCORE
104 #define STG_RETURN "_StgReturn"
105 #else
106 #define STG_RETURN "StgReturn"
107 #endif
108
109 /* -----------------------------------------------------------------------------
110    x86 architecture
111    -------------------------------------------------------------------------- */
112
113 #ifdef i386_TARGET_ARCH
114
115 StgThreadReturnCode
116 StgRun(StgFunPtr f, StgRegTable *basereg) {
117
118     static volatile void* stg_esp_saved_before_stack_align;
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 %5, %%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 %4,%%ebx\n\t"
137         /*
138          * grab the function argument from the stack
139          */
140         "movl %3,%%eax\n\t"
141         /* 
142          * Get %%esp 8-aligned, first saving the current value.  This
143          * moves it down another zero or four bytes, depending on
144          * whether it was already aligned or not.  This is a bit
145          * subtle -- we must not have a swizzled %esp at any place
146          * where we might refer to one of the %digit parameters to
147          * this piece of assembly code, since gcc may generate
148          * %esp-relative stack offsets which will then be wrong.
149          */
150         "movl %%esp, %6\n\t"
151         "subl $4, %%esp\n\t"
152         "andl $0xFFFFFFF8, %%esp\n\t"
153         /* 
154          * And finally, jump to the function argument. 
155          */
156         "jmp *%%eax\n\t"
157
158         ".global " STG_RETURN "\n"
159         STG_RETURN ":\n\t"
160         /* 
161          * Restore %%esp to pre-alignment state. 
162          */
163         "movl %2, %%esp\n"
164         /* 
165          * Move return value from R1 to %%eax 
166          */
167         "movl %%esi, %%eax\n\t"
168         /*
169          * restore callee-saves registers.  (Don't stomp on %%eax!)
170          */
171         "movl %%esp, %%edx\n\t"
172         "addl %5, %%edx\n\t"
173         "movl 0(%%edx),%%ebx\n\t"       /* restore the registers saved above */
174         "movl 4(%%edx),%%esi\n\t"
175         "movl 8(%%edx),%%edi\n\t"
176         "movl 12(%%edx),%%ebp\n\t"
177
178         : "=&a" (r), "=m" (space),
179           "=m" (stg_esp_saved_before_stack_align)
180         : "m" (f), "m" (basereg), "i" (RESERVED_C_STACK_BYTES),
181           "m" (stg_esp_saved_before_stack_align)
182
183       : "edx" /* stomps on %edx */
184     );
185
186     return r;
187 }
188
189 #endif
190
191 /* -----------------------------------------------------------------------------
192    Sparc architecture
193
194    --
195    OLD COMMENT from GHC-3.02:
196
197    We want tailjumps to be calls, because `call xxx' is the only Sparc
198    branch that allows an arbitrary label as a target.  (Gcc's ``goto
199    *target'' construct ends up loading the label into a register and
200    then jumping, at the cost of two extra instructions for the 32-bit
201    load.)
202
203    When entering the threaded world, we stash our return address in a
204    known location so that \tr{%i7} is available as an extra
205    callee-saves register.  Of course, we have to restore this when
206    coming out of the threaded world.
207
208    I hate this god-forsaken architecture.  Since the top of the
209    reserved stack space is used for globals and the bottom is reserved
210    for outgoing arguments, we have to stick our return address
211    somewhere in the middle.  Currently, I'm allowing 100 extra
212    outgoing arguments beyond the first 6.  --JSM
213
214    Updated info (GHC 4.06): we don't appear to use %i7 any more, so
215    I'm not sure whether we still need to save it.  Incedentally, what
216    does the last paragraph above mean when it says "the top of the
217    stack is used for globals"?  What globals?  --SDM
218
219    Updated info (GHC 4.08.2): not saving %i7 any more (see below).
220    -------------------------------------------------------------------------- */
221
222 #ifdef sparc_TARGET_ARCH
223
224 StgThreadReturnCode
225 StgRun(StgFunPtr f, StgRegTable *basereg) {
226
227     unsigned char space[RESERVED_C_STACK_BYTES];
228 #if 0
229     register void *i7 __asm__("%i7");
230     ((void **)(space))[100] = i7;
231 #endif
232     f();
233     __asm__ volatile (
234             ".align 4\n"
235             ".global " STG_RETURN "\n"
236             STG_RETURN ":"
237             : : : "l0","l1","l2","l3","l4","l5","l6","l7");
238     /* we tell the C compiler that l0-l7 are clobbered on return to
239      * StgReturn, otherwise it tries to use these to save eg. the
240      * address of space[100] across the call.  The correct thing
241      * to do would be to save all the callee-saves regs, but we
242      * can't be bothered to do that.
243      *
244      * The code that gcc generates for this little fragment is now
245      * terrible.  We could do much better by coding it directly in
246      * assembler.
247      */
248 #if 0
249     /* updated 4.08.2: we don't save %i7 in the middle of the reserved
250      * space any more, since gcc tries to save its address across the
251      * call to f(), this gets clobbered in STG land and we end up
252      * dereferencing a bogus pointer in StgReturn.
253      */
254     __asm__ volatile ("ld %1,%0"
255                       : "=r" (i7) : "m" (((void **)(space))[100]));
256 #endif
257     return (StgThreadReturnCode)R1.i;
258 }
259
260 #endif
261
262 /* -----------------------------------------------------------------------------
263    alpha architecture
264
265    "The stack pointer (SP) must at all times denote an address that has octaword
266     alignment. (This restriction has the side effect that the in-memory portion
267     of the argument list, if any, will start on an octaword boundary.) Note that
268     the stack grows toward lower addresses. During a procedure invocation, SP
269     can never be set to a value that is higher than the value of SP at entry to
270     that procedure invocation.
271
272    "The contents of the stack, located above the portion of the argument list
273     (if any) that is passed in memory, belong to the calling procedure. Because
274     they are part of the calling procedure, they should not be read or written
275     by the called procedure, except as specified by indirect arguments or
276     language-controlled up-level references.
277
278    "The SP value might be used by the hardware when raising exceptions and
279     asynchronous interrupts. It must be assumed that the contents of the stack
280     below the current SP value and within the stack for the current thread are
281     continually and unpredictably modified, as specified in the _Alpha
282     Architecture Reference Manual_, and as a result of asynchronous software
283     actions."
284
285    -- Compaq Computer Corporation, Houston. Tru64 UNIX Calling Standard for
286       Alpha Systems, 5.1 edition, August 2000, section 3.2.1.  http://www.
287       tru64unix.compaq.com/docs/base_doc/DOCUMENTATION/V51_PDF/ARH9MBTE.PDF
288    -------------------------------------------------------------------------- */
289
290 #ifdef alpha_TARGET_ARCH
291
292 StgThreadReturnCode
293 StgRun(StgFunPtr f, StgRegTable *basereg)
294 {
295     register long   real_ra __asm__("$26"); volatile long   save_ra;
296
297     register long   real_s0 __asm__("$9" ); volatile long   save_s0;
298     register long   real_s1 __asm__("$10"); volatile long   save_s1;
299     register long   real_s2 __asm__("$11"); volatile long   save_s2;
300     register long   real_s3 __asm__("$12"); volatile long   save_s3;
301     register long   real_s4 __asm__("$13"); volatile long   save_s4;
302     register long   real_s5 __asm__("$14"); volatile long   save_s5;
303 #ifdef alpha_EXTRA_CAREFUL
304     register long   real_s6 __asm__("$15"); volatile long   save_s6;
305 #endif
306
307     register double real_f2 __asm__("$f2"); volatile double save_f2;
308     register double real_f3 __asm__("$f3"); volatile double save_f3;
309     register double real_f4 __asm__("$f4"); volatile double save_f4;
310     register double real_f5 __asm__("$f5"); volatile double save_f5;
311     register double real_f6 __asm__("$f6"); volatile double save_f6;
312     register double real_f7 __asm__("$f7"); volatile double save_f7;
313 #ifdef alpha_EXTRA_CAREFUL
314     register double real_f8 __asm__("$f8"); volatile double save_f8;
315     register double real_f9 __asm__("$f9"); volatile double save_f9;
316 #endif
317
318     register StgFunPtr real_pv __asm__("$27");
319
320     StgThreadReturnCode ret;
321
322     save_ra = real_ra;
323
324     save_s0 = real_s0;
325     save_s1 = real_s1;
326     save_s2 = real_s2;
327     save_s3 = real_s3;
328     save_s4 = real_s4;
329     save_s5 = real_s5;
330 #ifdef alpha_EXTRA_CAREFUL
331     save_s6 = real_s6;
332 #endif
333
334     save_f2 = real_f2;
335     save_f3 = real_f3;
336     save_f4 = real_f4;
337     save_f5 = real_f5;
338     save_f6 = real_f6;
339     save_f7 = real_f7;
340 #ifdef alpha_EXTRA_CAREFUL
341     save_f8 = real_f8;
342     save_f9 = real_f9;
343 #endif
344
345     real_pv = f;
346
347     __asm__ volatile(   "lda $30,-%0($30)"      "\n"
348                 "\t"    "jmp ($27)"             "\n"
349                 "\t"    ".align 3"              "\n"
350                 ".globl " STG_RETURN            "\n"
351                 STG_RETURN ":"                  "\n"
352                 "\t"    "lda $30,%0($30)"       "\n"
353                 : : "K" (RESERVED_C_STACK_BYTES));
354
355     ret = real_s5;
356
357     real_s0 = save_s0;
358     real_s1 = save_s1;
359     real_s2 = save_s2;
360     real_s3 = save_s3;
361     real_s4 = save_s4;
362     real_s5 = save_s5;
363 #ifdef alpha_EXTRA_CAREFUL
364     real_s6 = save_s6;
365 #endif
366
367     real_f2 = save_f2;
368     real_f3 = save_f3;
369     real_f4 = save_f4;
370     real_f5 = save_f5;
371     real_f6 = save_f6;
372     real_f7 = save_f7;
373 #ifdef alpha_EXTRA_CAREFUL
374     real_f8 = save_f8;
375     real_f9 = save_f9;
376 #endif
377
378     real_ra = save_ra;
379
380     return ret;
381 }
382
383 #endif /* alpha_TARGET_ARCH */
384
385 /* -----------------------------------------------------------------------------
386    HP-PA architecture
387    -------------------------------------------------------------------------- */
388
389 #ifdef hppa1_1_TARGET_ARCH
390
391 StgThreadReturnCode
392 StgRun(StgFunPtr f, StgRegTable *basereg)
393 {
394     StgChar space[RESERVED_C_STACK_BYTES+16*sizeof(long)+10*sizeof(double)];
395     StgThreadReturnCode ret;
396
397     __asm__ volatile ("ldo %0(%%r30),%%r19\n"
398                       "\tstw %%r3, 0(0,%%r19)\n"
399                       "\tstw %%r4, 4(0,%%r19)\n"
400                       "\tstw %%r5, 8(0,%%r19)\n"
401                       "\tstw %%r6,12(0,%%r19)\n"
402                       "\tstw %%r7,16(0,%%r19)\n"
403                       "\tstw %%r8,20(0,%%r19)\n"
404                       "\tstw %%r9,24(0,%%r19)\n"
405                       "\tstw %%r10,28(0,%%r19)\n"
406                       "\tstw %%r11,32(0,%%r19)\n"
407                       "\tstw %%r12,36(0,%%r19)\n"
408                       "\tstw %%r13,40(0,%%r19)\n"
409                       "\tstw %%r14,44(0,%%r19)\n"
410                       "\tstw %%r15,48(0,%%r19)\n"
411                       "\tstw %%r16,52(0,%%r19)\n"
412                       "\tstw %%r17,56(0,%%r19)\n"
413                       "\tstw %%r18,60(0,%%r19)\n"
414                       "\tldo 80(%%r19),%%r19\n"
415                       "\tfstds %%fr12,-16(0,%%r19)\n"
416                       "\tfstds %%fr13, -8(0,%%r19)\n"
417                       "\tfstds %%fr14,  0(0,%%r19)\n"
418                       "\tfstds %%fr15,  8(0,%%r19)\n"
419                       "\tldo 32(%%r19),%%r19\n"
420                       "\tfstds %%fr16,-16(0,%%r19)\n"
421                       "\tfstds %%fr17, -8(0,%%r19)\n"
422                       "\tfstds %%fr18,  0(0,%%r19)\n"
423                       "\tfstds %%fr19,  8(0,%%r19)\n"
424                       "\tldo 32(%%r19),%%r19\n"
425                       "\tfstds %%fr20,-16(0,%%r19)\n"
426                       "\tfstds %%fr21, -8(0,%%r19)\n" : :
427                       "n" (-(116 * sizeof(long) + 10 * sizeof(double))) : "%r19"
428                       );
429
430     f();
431
432     __asm__ volatile (".align 4\n"
433                       "\t.EXPORT " STG_RETURN ",CODE\n"
434                       "\t.EXPORT " STG_RETURN ",ENTRY,PRIV_LEV=3\n"
435                       STG_RETURN "\n"
436                       /* "\tldo %0(%%r3),%%r19\n" */
437                       "\tldo %1(%%r30),%%r19\n"
438                       "\tcopy %%r11, %0\n"  /* save R1 */
439                       "\tldw  0(0,%%r19),%%r3\n"
440                       "\tldw  4(0,%%r19),%%r4\n"
441                       "\tldw  8(0,%%r19),%%r5\n"
442                       "\tldw 12(0,%%r19),%%r6\n"
443                       "\tldw 16(0,%%r19),%%r7\n"
444                       "\tldw 20(0,%%r19),%%r8\n"
445                       "\tldw 24(0,%%r19),%%r9\n"
446                       "\tldw 28(0,%%r19),%%r10\n"
447                       "\tldw 32(0,%%r19),%%r11\n"
448                       "\tldw 36(0,%%r19),%%r12\n"
449                       "\tldw 40(0,%%r19),%%r13\n"
450                       "\tldw 44(0,%%r19),%%r14\n"
451                       "\tldw 48(0,%%r19),%%r15\n"
452                       "\tldw 52(0,%%r19),%%r16\n"
453                       "\tldw 56(0,%%r19),%%r17\n"
454                       "\tldw 60(0,%%r19),%%r18\n"
455                       "\tldo 80(%%r19),%%r19\n"
456                       "\tfldds -16(0,%%r19),%%fr12\n"
457                       "\tfldds  -8(0,%%r19),%%fr13\n"
458                       "\tfldds   0(0,%%r19),%%fr14\n"
459                       "\tfldds   8(0,%%r19),%%fr15\n"
460                       "\tldo 32(%%r19),%%r19\n"
461                       "\tfldds -16(0,%%r19),%%fr16\n"
462                       "\tfldds  -8(0,%%r19),%%fr17\n"
463                       "\tfldds   0(0,%%r19),%%fr18\n"
464                       "\tfldds   8(0,%%r19),%%fr19\n"
465                       "\tldo 32(%%r19),%%r19\n"
466                       "\tfldds -16(0,%%r19),%%fr20\n"
467                       "\tfldds  -8(0,%%r19),%%fr21\n"
468                          : "=r" (ret)
469                          : "n" (-(116 * sizeof(long) + 10 * sizeof(double)))
470                          : "%r19"
471                       );
472
473     return ret;
474 }
475
476 #endif /* hppa1_1_TARGET_ARCH */
477
478 #endif /* !USE_MINIINTERPRETER */