Merge branch 'master' of ssh://darcs.haskell.org/srv/darcs/ghc
[ghc.git] / rts / StgCRun.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2011
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), passing the
16 * return-value "rv" in R1, to return to the caller of StgRun returning "rv" in
17 * the whatever way C returns a value.
18 *
19 * NOTE: StgRun/StgReturn do *NOT* load or store Hp or any other registers
20 * (other than saving the C callee-saves registers). Instead, the called
21 * function "f" must do that in STG land.
22 *
23 * We also initially make sure that there are @RESERVED_C_STACK_BYTES@ on the
24 * C-stack. This is done to reserve some space for the allocation of
25 * temporaries in STG code.
26 *
27 * -------------------------------------------------------------------------- */
28
29 #include "PosixSource.h"
30 #include "ghcconfig.h"
31
32 #if defined(sparc_HOST_ARCH) || defined(USE_MINIINTERPRETER)
33 /* include Stg.h first because we want real machine regs in here: we
34 * have to get the value of R1 back from Stg land to C land intact.
35 */
36
37 /* We include windows.h very early, as on Win64 the CONTEXT type has
38 fields "R8", "R9" and "R10", which goes bad if we've already
39 #define'd those names for our own purposes (in stg/Regs.h) */
40 #if defined(HAVE_WINDOWS_H)
41 #include <windows.h>
42 #endif
43
44 #define IN_STGCRUN 1
45 #include "Stg.h"
46 #include "Rts.h"
47 #else
48 /* The other architectures do not require the actual register macro definitions
49 * here because they use hand written assembly to implement the StgRun
50 * function. Including Stg.h first will define the R1 values using GCC specific
51 * techniques, which we don't want for LLVM based C compilers. Since we don't
52 * actually need the real machine register definitions here, we include the
53 * headers in the opposite order to allow LLVM-based C compilers to work.
54 */
55 #include "Rts.h"
56 #include "Stg.h"
57 #endif
58
59 #include "StgRun.h"
60 #include "Capability.h"
61
62 #ifdef DEBUG
63 #include "RtsUtils.h"
64 #include "Printer.h"
65 #endif
66
67 #ifdef USE_MINIINTERPRETER
68
69 /* -----------------------------------------------------------------------------
70 any architecture (using miniinterpreter)
71 -------------------------------------------------------------------------- */
72
73 StgRegTable * StgRun(StgFunPtr f, StgRegTable *basereg STG_UNUSED)
74 {
75 while (f) {
76 IF_DEBUG(interpreter,
77 debugBelch("Jumping to ");
78 printPtr((P_)f); fflush(stdout);
79 debugBelch("\n");
80 );
81 f = (StgFunPtr) (f)();
82 }
83 return (StgRegTable *)R1.p;
84 }
85
86 StgFunPtr StgReturn(void)
87 {
88 return 0;
89 }
90
91 #else /* !USE_MINIINTERPRETER */
92
93 #ifdef LEADING_UNDERSCORE
94 #define STG_RUN "_StgRun"
95 #define STG_RETURN "_StgReturn"
96 #else
97 #define STG_RUN "StgRun"
98 #define STG_RETURN "StgReturn"
99 #endif
100
101 #if defined(mingw32_HOST_OS)
102 // On windows the stack has to be allocated 4k at a time, otherwise
103 // we get a segfault. The C compiler knows how to do this (it calls
104 // _alloca()), so we make sure that we can allocate as much stack as
105 // we need:
106 StgWord8 *win32AllocStack(void)
107 {
108 StgWord8 stack[RESERVED_C_STACK_BYTES + 16 + 12];
109 return stack;
110 }
111 #endif
112
113 /* -----------------------------------------------------------------------------
114 x86 architecture
115 -------------------------------------------------------------------------- */
116
117 #ifdef i386_HOST_ARCH
118
119 #ifdef darwin_HOST_OS
120 #define STG_GLOBAL ".globl "
121 #else
122 #define STG_GLOBAL ".global "
123 #endif
124
125 /*
126 * Note [Stack Alignment on X86]
127 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128 *
129 * On X86 (both 32bit and 64bit) we keep the stack aligned on function calls at
130 * a 16-byte boundary. This is done because on a number of architectures the
131 * ABI requires this (x64, Mac OSX 32bit/64bit) as well as interfacing with
132 * other libraries through the FFI.
133 *
134 * As part of this arrangment we must maintain the stack at a 16-byte boundary
135 * - word_size-bytes (so 16n - 4 for i386 and 16n - 8 for x64) on entry to a
136 * procedure since both GCC and LLVM expect this. This is because the stack
137 * should have been 16-byte boundary aligned and then a call made which pushes
138 * a return address onto the stack (so word_size more space used). In STG code
139 * we only jump to other STG procedures, so we maintain the 16n - word_size
140 * alignment for these jumps.
141 *
142 * This gives us binary compatability with LLVM and GCC as well as dealing
143 * with the FFI. Previously we just maintianed a 16n byte alignment for
144 * procedure entry and calls, which led to bugs (see #4211 and #5250).
145 *
146 * To change this convention you need to change the code here, and in
147 * compiler/nativeGen/X86/CodeGen.hs::GenCCall, and maybe the adjustor
148 * code for thunks in rts/AdjustorAsm.s, rts/Adjustor.c.
149 *
150 * A quick way to see if this is wrong is to compile this code:
151 *
152 * main = System.Exit.exitWith ExitSuccess
153 *
154 * And run it with +RTS -sstderr. The stats code in the RTS, in
155 * particular statsPrintf(), relies on the stack alignment because
156 * it saves the %xmm regs on the stack, so it'll fall over if the
157 * stack isn't aligned, and calling exitWith from Haskell invokes
158 * shutdownHaskellAndExit using a C call.
159 *
160 */
161
162 static void GNUC3_ATTRIBUTE(used)
163 StgRunIsImplementedInAssembler(void)
164 {
165 __asm__ volatile (
166 STG_GLOBAL STG_RUN "\n"
167 STG_RUN ":\n\t"
168
169 /*
170 * move %esp down to reserve an area for temporary storage
171 * during the execution of STG code.
172 *
173 * The stack pointer has to be aligned to a multiple of 16
174 * bytes from here - this is a requirement of the C ABI, so
175 * that C code can assign SSE2 registers directly to/from
176 * stack locations.
177 */
178 "subl %0, %%esp\n\t"
179
180 /*
181 * save callee-saves registers on behalf of the STG code.
182 */
183 "movl %%esp, %%eax\n\t"
184 "addl %0-16, %%eax\n\t"
185 "movl %%ebx,0(%%eax)\n\t"
186 "movl %%esi,4(%%eax)\n\t"
187 "movl %%edi,8(%%eax)\n\t"
188 "movl %%ebp,12(%%eax)\n\t"
189 /*
190 * Set BaseReg
191 */
192 "movl 24(%%eax),%%ebx\n\t"
193 /*
194 * grab the function argument from the stack
195 */
196 "movl 20(%%eax),%%eax\n\t"
197 /*
198 * jump to it
199 */
200 "jmp *%%eax\n\t"
201
202 STG_GLOBAL STG_RETURN "\n"
203 STG_RETURN ":\n\t"
204
205 "movl %%esi, %%eax\n\t" /* Return value in R1 */
206
207 /*
208 * restore callee-saves registers. (Don't stomp on %%eax!)
209 */
210 "movl %%esp, %%edx\n\t"
211 "addl %0-16, %%edx\n\t"
212 "movl 0(%%edx),%%ebx\n\t" /* restore the registers saved above */
213 "movl 4(%%edx),%%esi\n\t"
214 "movl 8(%%edx),%%edi\n\t"
215 "movl 12(%%edx),%%ebp\n\t"
216
217 "addl %0, %%esp\n\t"
218 "ret"
219
220 : : "i" (RESERVED_C_STACK_BYTES + 16)
221 // + 16 to make room for the 4 registers we have to save
222 // See Note [Stack Alignment on X86]
223 );
224 }
225
226 #endif
227
228 /* ----------------------------------------------------------------------------
229 x86-64 is almost the same as plain x86.
230
231 I've done it using entirely inline assembler, because I couldn't
232 get gcc to generate the correct subtraction from %rsp by using
233 the local array variable trick. It didn't seem to reserve
234 enough space. Oh well, it's not much harder this way.
235 ------------------------------------------------------------------------- */
236
237 #ifdef x86_64_HOST_ARCH
238
239 extern StgRegTable * StgRun(StgFunPtr f, StgRegTable *basereg);
240
241 static void GNUC3_ATTRIBUTE(used)
242 StgRunIsImplementedInAssembler(void)
243 {
244 __asm__ volatile (
245 /*
246 * save callee-saves registers on behalf of the STG code.
247 */
248 ".globl " STG_RUN "\n"
249 STG_RUN ":\n\t"
250 "subq %1, %%rsp\n\t"
251 "movq %%rsp, %%rax\n\t"
252 "subq %0, %%rsp\n\t"
253 "movq %%rbx,0(%%rax)\n\t"
254 "movq %%rbp,8(%%rax)\n\t"
255 "movq %%r12,16(%%rax)\n\t"
256 "movq %%r13,24(%%rax)\n\t"
257 "movq %%r14,32(%%rax)\n\t"
258 "movq %%r15,40(%%rax)\n\t"
259 #if defined(mingw32_HOST_OS)
260 "movq %%rdi,48(%%rax)\n\t"
261 "movq %%rsi,56(%%rax)\n\t"
262 "movq %%xmm6,64(%%rax)\n\t"
263 #endif
264 /*
265 * Set BaseReg
266 */
267 #if defined(mingw32_HOST_OS)
268 "movq %%rdx,%%r13\n\t"
269 #else
270 "movq %%rsi,%%r13\n\t"
271 #endif
272 /*
273 * grab the function argument from the stack, and jump to it.
274 */
275 #if defined(mingw32_HOST_OS)
276 "movq %%rcx,%%rax\n\t"
277 #else
278 "movq %%rdi,%%rax\n\t"
279 #endif
280 "jmp *%%rax\n\t"
281
282 ".globl " STG_RETURN "\n"
283 STG_RETURN ":\n\t"
284
285 "movq %%rbx, %%rax\n\t" /* Return value in R1 */
286
287 /*
288 * restore callee-saves registers. (Don't stomp on %%rax!)
289 */
290 "addq %0, %%rsp\n\t"
291 "movq %%rsp, %%rdx\n\t"
292 "addq %1, %%rsp\n\t"
293 "movq 0(%%rdx),%%rbx\n\t" /* restore the registers saved above */
294 "movq 8(%%rdx),%%rbp\n\t"
295 "movq 16(%%rdx),%%r12\n\t"
296 "movq 24(%%rdx),%%r13\n\t"
297 "movq 32(%%rdx),%%r14\n\t"
298 "movq 40(%%rdx),%%r15\n\t"
299 #if defined(mingw32_HOST_OS)
300 "movq 48(%%rdx),%%rdi\n\t"
301 "movq 56(%%rdx),%%rsi\n\t"
302 "movq 64(%%rdx),%%xmm6\n\t"
303 #endif
304 "retq"
305
306 :
307 : "i"(RESERVED_C_STACK_BYTES),
308 #if defined(mingw32_HOST_OS)
309 "i"(80 /*stack frame size; 8 too large to make the alignment right*/)
310 #else
311 "i"(48 /*stack frame size*/)
312 #endif
313 );
314 /*
315 * See Note [Stack Alignment on X86]
316 */
317 }
318
319 #endif /* x86-64 */
320
321 /* -----------------------------------------------------------------------------
322 Sparc architecture
323
324 --
325 OLD COMMENT from GHC-3.02:
326
327 We want tailjumps to be calls, because `call xxx' is the only Sparc
328 branch that allows an arbitrary label as a target. (Gcc's ``goto
329 *target'' construct ends up loading the label into a register and
330 then jumping, at the cost of two extra instructions for the 32-bit
331 load.)
332
333 When entering the threaded world, we stash our return address in a
334 known location so that \tr{%i7} is available as an extra
335 callee-saves register. Of course, we have to restore this when
336 coming out of the threaded world.
337
338 I hate this god-forsaken architecture. Since the top of the
339 reserved stack space is used for globals and the bottom is reserved
340 for outgoing arguments, we have to stick our return address
341 somewhere in the middle. Currently, I'm allowing 100 extra
342 outgoing arguments beyond the first 6. --JSM
343
344 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
345 I'm not sure whether we still need to save it. Incedentally, what
346 does the last paragraph above mean when it says "the top of the
347 stack is used for globals"? What globals? --SDM
348
349 Updated info (GHC 4.08.2): not saving %i7 any more (see below).
350 -------------------------------------------------------------------------- */
351
352 #ifdef sparc_HOST_ARCH
353
354 StgRegTable *
355 StgRun(StgFunPtr f, StgRegTable *basereg) {
356
357 unsigned char space[RESERVED_C_STACK_BYTES];
358 #if 0
359 register void *i7 __asm__("%i7");
360 ((void **)(space))[100] = i7;
361 #endif
362 f();
363 __asm__ volatile (
364 ".align 4\n"
365 ".global " STG_RETURN "\n"
366 STG_RETURN ":"
367 : : "p" (space) : "l0","l1","l2","l3","l4","l5","l6","l7");
368 /* we tell the C compiler that l0-l7 are clobbered on return to
369 * StgReturn, otherwise it tries to use these to save eg. the
370 * address of space[100] across the call. The correct thing
371 * to do would be to save all the callee-saves regs, but we
372 * can't be bothered to do that.
373 *
374 * We also explicitly mark space as used since gcc eliminates it
375 * otherwise.
376 *
377 * The code that gcc generates for this little fragment is now
378 * terrible. We could do much better by coding it directly in
379 * assembler.
380 */
381 #if 0
382 /* updated 4.08.2: we don't save %i7 in the middle of the reserved
383 * space any more, since gcc tries to save its address across the
384 * call to f(), this gets clobbered in STG land and we end up
385 * dereferencing a bogus pointer in StgReturn.
386 */
387 __asm__ volatile ("ld %1,%0"
388 : "=r" (i7) : "m" (((void **)(space))[100]));
389 #endif
390 return (StgRegTable *)R1.i;
391 }
392
393 #endif
394
395 /* -----------------------------------------------------------------------------
396 PowerPC architecture
397
398 Everything is in assembler, so we don't have to deal with GCC...
399 -------------------------------------------------------------------------- */
400
401 #ifdef powerpc_HOST_ARCH
402
403 extern StgRegTable * StgRun(StgFunPtr f, StgRegTable *basereg);
404
405 #ifdef darwin_HOST_OS
406 void StgRunIsImplementedInAssembler(void)
407 {
408 #if HAVE_SUBSECTIONS_VIA_SYMBOLS
409 // if the toolchain supports deadstripping, we have to
410 // prevent it here (it tends to get confused here).
411 __asm__ volatile (".no_dead_strip _StgRunIsImplementedInAssembler");
412 #endif
413 __asm__ volatile (
414 "\n.globl _StgRun\n"
415 "_StgRun:\n"
416 "\tmflr r0\n"
417 "\tbl saveFP # f14\n"
418 "\tstmw r13,-220(r1)\n"
419 "\tstwu r1,-%0(r1)\n"
420 "\tmr r27,r4\n" // BaseReg == r27
421 "\tmtctr r3\n"
422 "\tmr r12,r3\n"
423 "\tbctr\n"
424 ".globl _StgReturn\n"
425 "_StgReturn:\n"
426 "\tmr r3,r14\n"
427 "\tla r1,%0(r1)\n"
428 "\tlmw r13,-220(r1)\n"
429 "\tb restFP # f14\n"
430 : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
431 }
432 #else
433
434 // This version is for PowerPC Linux.
435
436 // Differences from the Darwin/Mac OS X version:
437 // *) Different Assembler Syntax
438 // *) Doesn't use Register Saving Helper Functions (although they exist somewhere)
439 // *) We may not access positive stack offsets
440 // (no "Red Zone" as in the Darwin ABI)
441 // *) The Link Register is saved to a different offset in the caller's stack frame
442 // (Linux: 4(r1), Darwin 8(r1))
443
444 static void GNUC3_ATTRIBUTE(used)
445 StgRunIsImplementedInAssembler(void)
446 {
447 __asm__ volatile (
448 "\t.globl StgRun\n"
449 "\t.type StgRun,@function\n"
450 "StgRun:\n"
451 "\tmflr 0\n"
452 "\tstw 0,4(1)\n"
453 "\tmr 5,1\n"
454 "\tstwu 1,-%0(1)\n"
455 "\tstmw 13,-220(5)\n"
456 "\tstfd 14,-144(5)\n"
457 "\tstfd 15,-136(5)\n"
458 "\tstfd 16,-128(5)\n"
459 "\tstfd 17,-120(5)\n"
460 "\tstfd 18,-112(5)\n"
461 "\tstfd 19,-104(5)\n"
462 "\tstfd 20,-96(5)\n"
463 "\tstfd 21,-88(5)\n"
464 "\tstfd 22,-80(5)\n"
465 "\tstfd 23,-72(5)\n"
466 "\tstfd 24,-64(5)\n"
467 "\tstfd 25,-56(5)\n"
468 "\tstfd 26,-48(5)\n"
469 "\tstfd 27,-40(5)\n"
470 "\tstfd 28,-32(5)\n"
471 "\tstfd 29,-24(5)\n"
472 "\tstfd 30,-16(5)\n"
473 "\tstfd 31,-8(5)\n"
474 "\tmr 27,4\n" // BaseReg == r27
475 "\tmtctr 3\n"
476 "\tmr 12,3\n"
477 "\tbctr\n"
478 ".globl StgReturn\n"
479 "\t.type StgReturn,@function\n"
480 "StgReturn:\n"
481 "\tmr 3,14\n"
482 "\tla 5,%0(1)\n"
483 "\tlmw 13,-220(5)\n"
484 "\tlfd 14,-144(5)\n"
485 "\tlfd 15,-136(5)\n"
486 "\tlfd 16,-128(5)\n"
487 "\tlfd 17,-120(5)\n"
488 "\tlfd 18,-112(5)\n"
489 "\tlfd 19,-104(5)\n"
490 "\tlfd 20,-96(5)\n"
491 "\tlfd 21,-88(5)\n"
492 "\tlfd 22,-80(5)\n"
493 "\tlfd 23,-72(5)\n"
494 "\tlfd 24,-64(5)\n"
495 "\tlfd 25,-56(5)\n"
496 "\tlfd 26,-48(5)\n"
497 "\tlfd 27,-40(5)\n"
498 "\tlfd 28,-32(5)\n"
499 "\tlfd 29,-24(5)\n"
500 "\tlfd 30,-16(5)\n"
501 "\tlfd 31,-8(5)\n"
502 "\tmr 1,5\n"
503 "\tlwz 0,4(1)\n"
504 "\tmtlr 0\n"
505 "\tblr\n"
506 : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
507 }
508 #endif
509
510 #endif
511
512 /* -----------------------------------------------------------------------------
513 PowerPC 64 architecture
514
515 Everything is in assembler, so we don't have to deal with GCC...
516 -------------------------------------------------------------------------- */
517
518 #ifdef powerpc64_HOST_ARCH
519
520 #ifdef linux_HOST_OS
521 extern StgRegTable * StgRun(StgFunPtr f, StgRegTable *basereg);
522
523 static void GNUC3_ATTRIBUTE(used)
524 StgRunIsImplementedInAssembler(void)
525 {
526 // r0 volatile
527 // r1 stack pointer
528 // r2 toc - needs to be saved
529 // r3-r10 argument passing, volatile
530 // r11, r12 very volatile (not saved across cross-module calls)
531 // r13 thread local state (never modified, don't need to save)
532 // r14-r31 callee-save
533 __asm__ volatile (
534 ".section \".opd\",\"aw\"\n"
535 ".align 3\n"
536 ".globl StgRun\n"
537 "StgRun:\n"
538 "\t.quad\t.StgRun,.TOC.@tocbase,0\n"
539 "\t.size StgRun,24\n"
540 ".globl StgReturn\n"
541 "StgReturn:\n"
542 "\t.quad\t.StgReturn,.TOC.@tocbase,0\n"
543 "\t.size StgReturn,24\n"
544 ".previous\n"
545 ".globl .StgRun\n"
546 ".type .StgRun,@function\n"
547 ".StgRun:\n"
548 "\tmflr 0\n"
549 "\tmr 5, 1\n"
550 "\tstd 0, 16(1)\n"
551 "\tstdu 1, -%0(1)\n"
552 "\tstd 2, -296(5)\n"
553 "\tstd 14, -288(5)\n"
554 "\tstd 15, -280(5)\n"
555 "\tstd 16, -272(5)\n"
556 "\tstd 17, -264(5)\n"
557 "\tstd 18, -256(5)\n"
558 "\tstd 19, -248(5)\n"
559 "\tstd 20, -240(5)\n"
560 "\tstd 21, -232(5)\n"
561 "\tstd 22, -224(5)\n"
562 "\tstd 23, -216(5)\n"
563 "\tstd 24, -208(5)\n"
564 "\tstd 25, -200(5)\n"
565 "\tstd 26, -192(5)\n"
566 "\tstd 27, -184(5)\n"
567 "\tstd 28, -176(5)\n"
568 "\tstd 29, -168(5)\n"
569 "\tstd 30, -160(5)\n"
570 "\tstd 31, -152(5)\n"
571 "\tstfd 14, -144(5)\n"
572 "\tstfd 15, -136(5)\n"
573 "\tstfd 16, -128(5)\n"
574 "\tstfd 17, -120(5)\n"
575 "\tstfd 18, -112(5)\n"
576 "\tstfd 19, -104(5)\n"
577 "\tstfd 20, -96(5)\n"
578 "\tstfd 21, -88(5)\n"
579 "\tstfd 22, -80(5)\n"
580 "\tstfd 23, -72(5)\n"
581 "\tstfd 24, -64(5)\n"
582 "\tstfd 25, -56(5)\n"
583 "\tstfd 26, -48(5)\n"
584 "\tstfd 27, -40(5)\n"
585 "\tstfd 28, -32(5)\n"
586 "\tstfd 29, -24(5)\n"
587 "\tstfd 30, -16(5)\n"
588 "\tstfd 31, -8(5)\n"
589 "\tmr 27, 4\n" // BaseReg == r27
590 "\tld 2, 8(3)\n"
591 "\tld 3, 0(3)\n"
592 "\tmtctr 3\n"
593 "\tbctr\n"
594 ".globl .StgReturn\n"
595 ".type .StgReturn,@function\n"
596 ".StgReturn:\n"
597 "\tmr 3,14\n"
598 "\tla 5, %0(1)\n" // load address == addi r5, r1, %0
599 "\tld 2, -296(5)\n"
600 "\tld 14, -288(5)\n"
601 "\tld 15, -280(5)\n"
602 "\tld 16, -272(5)\n"
603 "\tld 17, -264(5)\n"
604 "\tld 18, -256(5)\n"
605 "\tld 19, -248(5)\n"
606 "\tld 20, -240(5)\n"
607 "\tld 21, -232(5)\n"
608 "\tld 22, -224(5)\n"
609 "\tld 23, -216(5)\n"
610 "\tld 24, -208(5)\n"
611 "\tld 25, -200(5)\n"
612 "\tld 26, -192(5)\n"
613 "\tld 27, -184(5)\n"
614 "\tld 28, -176(5)\n"
615 "\tld 29, -168(5)\n"
616 "\tld 30, -160(5)\n"
617 "\tld 31, -152(5)\n"
618 "\tlfd 14, -144(5)\n"
619 "\tlfd 15, -136(5)\n"
620 "\tlfd 16, -128(5)\n"
621 "\tlfd 17, -120(5)\n"
622 "\tlfd 18, -112(5)\n"
623 "\tlfd 19, -104(5)\n"
624 "\tlfd 20, -96(5)\n"
625 "\tlfd 21, -88(5)\n"
626 "\tlfd 22, -80(5)\n"
627 "\tlfd 23, -72(5)\n"
628 "\tlfd 24, -64(5)\n"
629 "\tlfd 25, -56(5)\n"
630 "\tlfd 26, -48(5)\n"
631 "\tlfd 27, -40(5)\n"
632 "\tlfd 28, -32(5)\n"
633 "\tlfd 29, -24(5)\n"
634 "\tlfd 30, -16(5)\n"
635 "\tlfd 31, -8(5)\n"
636 "\tmr 1, 5\n"
637 "\tld 0, 16(1)\n"
638 "\tmtlr 0\n"
639 "\tblr\n"
640 : : "i"(RESERVED_C_STACK_BYTES+304 /*stack frame size*/));
641 }
642
643 #else // linux_HOST_OS
644 #error Only linux support for power64 right now.
645 #endif
646
647 #endif
648
649 /* -----------------------------------------------------------------------------
650 ARM architecture
651 -------------------------------------------------------------------------- */
652
653 #ifdef arm_HOST_ARCH
654
655 #if defined(__thumb__)
656 #define THUMB_FUNC ".thumb\n\t.thumb_func\n\t"
657 #else
658 #define THUMB_FUNC
659 #endif
660
661 StgRegTable *
662 StgRun(StgFunPtr f, StgRegTable *basereg) {
663 StgRegTable * r;
664 __asm__ volatile (
665 /*
666 * save callee-saves registers on behalf of the STG code.
667 */
668 "stmfd sp!, {r4-r10, fp, ip, lr}\n\t"
669 #if !defined(arm_HOST_ARCH_PRE_ARMv6)
670 "vstmdb sp!, {d8-d11}\n\t"
671 #endif
672 /*
673 * allocate some space for Stg machine's temporary storage.
674 * Note: RESERVER_C_STACK_BYTES has to be a round number here or
675 * the assembler can't assemble it.
676 */
677 "sub sp, sp, %3\n\t"
678 /*
679 * Set BaseReg
680 */
681 "mov r4, %2\n\t"
682 /*
683 * Jump to function argument.
684 */
685 "bx %1\n\t"
686
687 ".global " STG_RETURN "\n\t"
688 THUMB_FUNC
689 ".type " STG_RETURN ", %%function\n"
690 STG_RETURN ":\n\t"
691 /*
692 * Free the space we allocated
693 */
694 "add sp, sp, %3\n\t"
695 /*
696 * Return the new register table, taking it from Stg's R1 (ARM's R7).
697 */
698 "mov %0, r7\n\t"
699 /*
700 * restore callee-saves registers.
701 */
702 #if !defined(arm_HOST_ARCH_PRE_ARMv6)
703 "vldmia sp!, {d8-d11}\n\t"
704 #endif
705 "ldmfd sp!, {r4-r10, fp, ip, lr}\n\t"
706 : "=r" (r)
707 : "r" (f), "r" (basereg), "i" (RESERVED_C_STACK_BYTES)
708 #if !defined(__thumb__)
709 /* In ARM mode, r11/fp is frame-pointer and so we cannot mark
710 it as clobbered. If we do so, GCC complains with error. */
711 : "%r4", "%r5", "%r6", "%r7", "%r8", "%r9", "%r10", "%ip", "%lr"
712 #else
713 /* In Thumb mode r7 is frame-pointer and so we cannot mark it
714 as clobbered. On the other hand we mark as clobbered also
715 those regs not used in Thumb mode. Hard to judge if this is
716 needed, but certainly Haskell code is using them for
717 placing GHC's virtual registers there. See
718 includes/stg/MachRegs.h Please note that Haskell code is
719 compiled by GHC/LLVM into ARM code (not Thumb!), at least
720 as of February 2012 */
721 : "%r4", "%r5", "%r6", "%r8", "%r9", "%r10", "%fp", "%ip", "%lr"
722 #endif
723 );
724 return r;
725 }
726 #endif
727
728 #endif /* !USE_MINIINTERPRETER */