Support more sphinx-build versions in configure script
[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 #if defined(darwin_HOST_OS) || defined(ios_HOST_OS)
120 #define STG_GLOBAL ".globl "
121 #define STG_HIDDEN ".private_extern "
122 #else
123 #define STG_GLOBAL ".global "
124 #define STG_HIDDEN ".hidden "
125 #endif
126
127 /*
128 * Note [Stack Alignment on X86]
129 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130 *
131 * On X86 (both 32bit and 64bit) we keep the stack aligned on function calls at
132 * a 16-byte boundary. This is done because on a number of architectures the
133 * ABI requires this (x64, Mac OSX 32bit/64bit) as well as interfacing with
134 * other libraries through the FFI.
135 *
136 * As part of this arrangment we must maintain the stack at a 16-byte boundary
137 * - word_size-bytes (so 16n - 4 for i386 and 16n - 8 for x64) on entry to a
138 * procedure since both GCC and LLVM expect this. This is because the stack
139 * should have been 16-byte boundary aligned and then a call made which pushes
140 * a return address onto the stack (so word_size more space used). In STG code
141 * we only jump to other STG procedures, so we maintain the 16n - word_size
142 * alignment for these jumps.
143 *
144 * This gives us binary compatability with LLVM and GCC as well as dealing
145 * with the FFI. Previously we just maintianed a 16n byte alignment for
146 * procedure entry and calls, which led to bugs (see #4211 and #5250).
147 *
148 * To change this convention you need to change the code here, and in
149 * compiler/nativeGen/X86/CodeGen.hs::GenCCall, and maybe the adjustor
150 * code for thunks in rts/AdjustorAsm.s, rts/Adjustor.c.
151 *
152 * A quick way to see if this is wrong is to compile this code:
153 *
154 * main = System.Exit.exitWith ExitSuccess
155 *
156 * And run it with +RTS -sstderr. The stats code in the RTS, in
157 * particular statsPrintf(), relies on the stack alignment because
158 * it saves the %xmm regs on the stack, so it'll fall over if the
159 * stack isn't aligned, and calling exitWith from Haskell invokes
160 * shutdownHaskellAndExit using a C call.
161 *
162 */
163
164 static void GNUC3_ATTRIBUTE(used)
165 StgRunIsImplementedInAssembler(void)
166 {
167 __asm__ volatile (
168 STG_GLOBAL STG_RUN "\n"
169 #if !defined(mingw32_HOST_OS)
170 STG_HIDDEN STG_RUN "\n"
171 #endif
172 STG_RUN ":\n\t"
173
174 /*
175 * move %esp down to reserve an area for temporary storage
176 * during the execution of STG code.
177 *
178 * The stack pointer has to be aligned to a multiple of 16
179 * bytes from here - this is a requirement of the C ABI, so
180 * that C code can assign SSE2 registers directly to/from
181 * stack locations.
182 */
183 "subl %0, %%esp\n\t"
184
185 /*
186 * save callee-saves registers on behalf of the STG code.
187 */
188 "movl %%esp, %%eax\n\t"
189 "addl %0-16, %%eax\n\t"
190 "movl %%ebx,0(%%eax)\n\t"
191 "movl %%esi,4(%%eax)\n\t"
192 "movl %%edi,8(%%eax)\n\t"
193 "movl %%ebp,12(%%eax)\n\t"
194 /*
195 * Set BaseReg
196 */
197 "movl 24(%%eax),%%ebx\n\t"
198 /*
199 * grab the function argument from the stack
200 */
201 "movl 20(%%eax),%%eax\n\t"
202 /*
203 * jump to it
204 */
205 "jmp *%%eax\n\t"
206
207 STG_GLOBAL STG_RETURN "\n"
208 STG_RETURN ":\n\t"
209
210 "movl %%esi, %%eax\n\t" /* Return value in R1 */
211
212 /*
213 * restore callee-saves registers. (Don't stomp on %%eax!)
214 */
215 "movl %%esp, %%edx\n\t"
216 "addl %0-16, %%edx\n\t"
217 "movl 0(%%edx),%%ebx\n\t" /* restore the registers saved above */
218 "movl 4(%%edx),%%esi\n\t"
219 "movl 8(%%edx),%%edi\n\t"
220 "movl 12(%%edx),%%ebp\n\t"
221
222 "addl %0, %%esp\n\t"
223 "ret"
224
225 : : "i" (RESERVED_C_STACK_BYTES + 16)
226 // + 16 to make room for the 4 registers we have to save
227 // See Note [Stack Alignment on X86]
228 );
229 }
230
231 #endif
232
233 /* ----------------------------------------------------------------------------
234 x86-64 is almost the same as plain x86.
235
236 I've done it using entirely inline assembler, because I couldn't
237 get gcc to generate the correct subtraction from %rsp by using
238 the local array variable trick. It didn't seem to reserve
239 enough space. Oh well, it's not much harder this way.
240 ------------------------------------------------------------------------- */
241
242 #ifdef x86_64_HOST_ARCH
243
244 #define STG_GLOBAL ".globl "
245
246 #ifdef darwin_HOST_OS
247 #define STG_HIDDEN ".private_extern "
248 #else
249 #define STG_HIDDEN ".hidden "
250 #endif
251
252 static void GNUC3_ATTRIBUTE(used)
253 StgRunIsImplementedInAssembler(void)
254 {
255 __asm__ volatile (
256 /*
257 * save callee-saves registers on behalf of the STG code.
258 */
259 STG_GLOBAL STG_RUN "\n"
260 #if !defined(mingw32_HOST_OS)
261 STG_HIDDEN STG_RUN "\n"
262 #endif
263 STG_RUN ":\n\t"
264 "subq %1, %%rsp\n\t"
265 "movq %%rsp, %%rax\n\t"
266 "subq %0, %%rsp\n\t"
267 "movq %%rbx,0(%%rax)\n\t"
268 "movq %%rbp,8(%%rax)\n\t"
269 "movq %%r12,16(%%rax)\n\t"
270 "movq %%r13,24(%%rax)\n\t"
271 "movq %%r14,32(%%rax)\n\t"
272 "movq %%r15,40(%%rax)\n\t"
273 #if defined(mingw32_HOST_OS)
274 "movq %%rdi,48(%%rax)\n\t"
275 "movq %%rsi,56(%%rax)\n\t"
276 "movq %%xmm6,64(%%rax)\n\t"
277 #endif
278 /*
279 * Set BaseReg
280 */
281 #if defined(mingw32_HOST_OS)
282 "movq %%rdx,%%r13\n\t"
283 #else
284 "movq %%rsi,%%r13\n\t"
285 #endif
286 /*
287 * grab the function argument from the stack, and jump to it.
288 */
289 #if defined(mingw32_HOST_OS)
290 "movq %%rcx,%%rax\n\t"
291 #else
292 "movq %%rdi,%%rax\n\t"
293 #endif
294 "jmp *%%rax\n\t"
295
296 ".globl " STG_RETURN "\n"
297 STG_RETURN ":\n\t"
298
299 "movq %%rbx, %%rax\n\t" /* Return value in R1 */
300
301 /*
302 * restore callee-saves registers. (Don't stomp on %%rax!)
303 */
304 "addq %0, %%rsp\n\t"
305 "movq 0(%%rsp),%%rbx\n\t" /* restore the registers saved above */
306 "movq 8(%%rsp),%%rbp\n\t"
307 "movq 16(%%rsp),%%r12\n\t"
308 "movq 24(%%rsp),%%r13\n\t"
309 "movq 32(%%rsp),%%r14\n\t"
310 "movq 40(%%rsp),%%r15\n\t"
311 #if defined(mingw32_HOST_OS)
312 "movq 48(%%rsp),%%rdi\n\t"
313 "movq 56(%%rsp),%%rsi\n\t"
314 "movq 64(%%rsp),%%xmm6\n\t"
315 #endif
316 "addq %1, %%rsp\n\t"
317 "retq"
318
319 :
320 : "i"(RESERVED_C_STACK_BYTES),
321 #if defined(mingw32_HOST_OS)
322 "i"(80 /*stack frame size; 8 too large to make the alignment right*/)
323 #else
324 "i"(48 /*stack frame size*/)
325 #endif
326 );
327 /*
328 * See Note [Stack Alignment on X86]
329 */
330 }
331
332 #endif /* x86-64 */
333
334 /* -----------------------------------------------------------------------------
335 Sparc architecture
336
337 --
338 OLD COMMENT from GHC-3.02:
339
340 We want tailjumps to be calls, because `call xxx' is the only Sparc
341 branch that allows an arbitrary label as a target. (Gcc's ``goto
342 *target'' construct ends up loading the label into a register and
343 then jumping, at the cost of two extra instructions for the 32-bit
344 load.)
345
346 When entering the threaded world, we stash our return address in a
347 known location so that \tr{%i7} is available as an extra
348 callee-saves register. Of course, we have to restore this when
349 coming out of the threaded world.
350
351 I hate this god-forsaken architecture. Since the top of the
352 reserved stack space is used for globals and the bottom is reserved
353 for outgoing arguments, we have to stick our return address
354 somewhere in the middle. Currently, I'm allowing 100 extra
355 outgoing arguments beyond the first 6. --JSM
356
357 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
358 I'm not sure whether we still need to save it. Incedentally, what
359 does the last paragraph above mean when it says "the top of the
360 stack is used for globals"? What globals? --SDM
361
362 Updated info (GHC 4.08.2): not saving %i7 any more (see below).
363 -------------------------------------------------------------------------- */
364
365 #ifdef sparc_HOST_ARCH
366
367 StgRegTable *
368 StgRun(StgFunPtr f, StgRegTable *basereg) {
369
370 unsigned char space[RESERVED_C_STACK_BYTES];
371 #if 0
372 register void *i7 __asm__("%i7");
373 ((void **)(space))[100] = i7;
374 #endif
375 f();
376 __asm__ volatile (
377 ".align 4\n"
378 ".global " STG_RETURN "\n"
379 STG_RETURN ":"
380 : : "p" (space) : "l0","l1","l2","l3","l4","l5","l6","l7");
381 /* we tell the C compiler that l0-l7 are clobbered on return to
382 * StgReturn, otherwise it tries to use these to save eg. the
383 * address of space[100] across the call. The correct thing
384 * to do would be to save all the callee-saves regs, but we
385 * can't be bothered to do that.
386 *
387 * We also explicitly mark space as used since gcc eliminates it
388 * otherwise.
389 *
390 * The code that gcc generates for this little fragment is now
391 * terrible. We could do much better by coding it directly in
392 * assembler.
393 */
394 #if 0
395 /* updated 4.08.2: we don't save %i7 in the middle of the reserved
396 * space any more, since gcc tries to save its address across the
397 * call to f(), this gets clobbered in STG land and we end up
398 * dereferencing a bogus pointer in StgReturn.
399 */
400 __asm__ volatile ("ld %1,%0"
401 : "=r" (i7) : "m" (((void **)(space))[100]));
402 #endif
403 return (StgRegTable *)R1.i;
404 }
405
406 #endif
407
408 /* -----------------------------------------------------------------------------
409 PowerPC architecture
410
411 Everything is in assembler, so we don't have to deal with GCC...
412 -------------------------------------------------------------------------- */
413
414 #ifdef powerpc_HOST_ARCH
415
416 #define STG_GLOBAL ".globl "
417
418 #ifdef darwin_HOST_OS
419 #define STG_HIDDEN ".private_extern "
420 #else
421 #define STG_HIDDEN ".hidden "
422 #endif
423
424 #ifdef darwin_HOST_OS
425 void StgRunIsImplementedInAssembler(void)
426 {
427 #if HAVE_SUBSECTIONS_VIA_SYMBOLS
428 // if the toolchain supports deadstripping, we have to
429 // prevent it here (it tends to get confused here).
430 __asm__ volatile (".no_dead_strip _StgRunIsImplementedInAssembler\n");
431 #endif
432 __asm__ volatile (
433 STG_GLOBAL STG_RUN "\n"
434 STG_HIDDEN STG_RUN "\n"
435 STG_RUN ":\n"
436 "\tmflr r0\n"
437 "\tbl saveFP # f14\n"
438 "\tstmw r13,-220(r1)\n"
439 "\tstwu r1,-%0(r1)\n"
440 "\tmr r27,r4\n" // BaseReg == r27
441 "\tmtctr r3\n"
442 "\tmr r12,r3\n"
443 "\tbctr\n"
444 ".globl _StgReturn\n"
445 "_StgReturn:\n"
446 "\tmr r3,r14\n"
447 "\tla r1,%0(r1)\n"
448 "\tlmw r13,-220(r1)\n"
449 "\tb restFP # f14\n"
450 : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
451 }
452 #else
453
454 // This version is for PowerPC Linux.
455
456 // Differences from the Darwin/Mac OS X version:
457 // *) Different Assembler Syntax
458 // *) Doesn't use Register Saving Helper Functions (although they exist somewhere)
459 // *) We may not access positive stack offsets
460 // (no "Red Zone" as in the Darwin ABI)
461 // *) The Link Register is saved to a different offset in the caller's stack frame
462 // (Linux: 4(r1), Darwin 8(r1))
463
464 static void GNUC3_ATTRIBUTE(used)
465 StgRunIsImplementedInAssembler(void)
466 {
467 __asm__ volatile (
468 "\t.globl StgRun\n"
469 "\t.hidden StgRun\n"
470 "\t.type StgRun,@function\n"
471 "StgRun:\n"
472 "\tmflr 0\n"
473 "\tstw 0,4(1)\n"
474 "\tmr 5,1\n"
475 "\tstwu 1,-%0(1)\n"
476 "\tstmw 13,-220(5)\n"
477 "\tstfd 14,-144(5)\n"
478 "\tstfd 15,-136(5)\n"
479 "\tstfd 16,-128(5)\n"
480 "\tstfd 17,-120(5)\n"
481 "\tstfd 18,-112(5)\n"
482 "\tstfd 19,-104(5)\n"
483 "\tstfd 20,-96(5)\n"
484 "\tstfd 21,-88(5)\n"
485 "\tstfd 22,-80(5)\n"
486 "\tstfd 23,-72(5)\n"
487 "\tstfd 24,-64(5)\n"
488 "\tstfd 25,-56(5)\n"
489 "\tstfd 26,-48(5)\n"
490 "\tstfd 27,-40(5)\n"
491 "\tstfd 28,-32(5)\n"
492 "\tstfd 29,-24(5)\n"
493 "\tstfd 30,-16(5)\n"
494 "\tstfd 31,-8(5)\n"
495 "\tmr 27,4\n" // BaseReg == r27
496 "\tmtctr 3\n"
497 "\tmr 12,3\n"
498 "\tbctr\n"
499 ".globl StgReturn\n"
500 "\t.type StgReturn,@function\n"
501 "StgReturn:\n"
502 "\tmr 3,14\n"
503 "\tla 5,%0(1)\n"
504 "\tlmw 13,-220(5)\n"
505 "\tlfd 14,-144(5)\n"
506 "\tlfd 15,-136(5)\n"
507 "\tlfd 16,-128(5)\n"
508 "\tlfd 17,-120(5)\n"
509 "\tlfd 18,-112(5)\n"
510 "\tlfd 19,-104(5)\n"
511 "\tlfd 20,-96(5)\n"
512 "\tlfd 21,-88(5)\n"
513 "\tlfd 22,-80(5)\n"
514 "\tlfd 23,-72(5)\n"
515 "\tlfd 24,-64(5)\n"
516 "\tlfd 25,-56(5)\n"
517 "\tlfd 26,-48(5)\n"
518 "\tlfd 27,-40(5)\n"
519 "\tlfd 28,-32(5)\n"
520 "\tlfd 29,-24(5)\n"
521 "\tlfd 30,-16(5)\n"
522 "\tlfd 31,-8(5)\n"
523 "\tmr 1,5\n"
524 "\tlwz 0,4(1)\n"
525 "\tmtlr 0\n"
526 "\tblr\n"
527 : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
528 }
529 #endif
530
531 #endif
532
533 /* -----------------------------------------------------------------------------
534 PowerPC 64 architecture
535
536 Everything is in assembler, so we don't have to deal with GCC...
537 -------------------------------------------------------------------------- */
538
539 #ifdef powerpc64_HOST_ARCH
540
541 #ifdef linux_HOST_OS
542 static void GNUC3_ATTRIBUTE(used)
543 StgRunIsImplementedInAssembler(void)
544 {
545 // r0 volatile
546 // r1 stack pointer
547 // r2 toc - needs to be saved
548 // r3-r10 argument passing, volatile
549 // r11, r12 very volatile (not saved across cross-module calls)
550 // r13 thread local state (never modified, don't need to save)
551 // r14-r31 callee-save
552 __asm__ volatile (
553 ".section \".opd\",\"aw\"\n"
554 ".align 3\n"
555 ".globl StgRun\n"
556 ".hidden StgRun\n"
557 "StgRun:\n"
558 "\t.quad\t.StgRun,.TOC.@tocbase,0\n"
559 "\t.size StgRun,24\n"
560 ".globl StgReturn\n"
561 "StgReturn:\n"
562 "\t.quad\t.StgReturn,.TOC.@tocbase,0\n"
563 "\t.size StgReturn,24\n"
564 ".previous\n"
565 ".globl .StgRun\n"
566 ".type .StgRun,@function\n"
567 ".StgRun:\n"
568 "\tmflr 0\n"
569 "\tmr 5, 1\n"
570 "\tstd 0, 16(1)\n"
571 "\tstdu 1, -%0(1)\n"
572 "\tstd 2, -296(5)\n"
573 "\tstd 14, -288(5)\n"
574 "\tstd 15, -280(5)\n"
575 "\tstd 16, -272(5)\n"
576 "\tstd 17, -264(5)\n"
577 "\tstd 18, -256(5)\n"
578 "\tstd 19, -248(5)\n"
579 "\tstd 20, -240(5)\n"
580 "\tstd 21, -232(5)\n"
581 "\tstd 22, -224(5)\n"
582 "\tstd 23, -216(5)\n"
583 "\tstd 24, -208(5)\n"
584 "\tstd 25, -200(5)\n"
585 "\tstd 26, -192(5)\n"
586 "\tstd 27, -184(5)\n"
587 "\tstd 28, -176(5)\n"
588 "\tstd 29, -168(5)\n"
589 "\tstd 30, -160(5)\n"
590 "\tstd 31, -152(5)\n"
591 "\tstfd 14, -144(5)\n"
592 "\tstfd 15, -136(5)\n"
593 "\tstfd 16, -128(5)\n"
594 "\tstfd 17, -120(5)\n"
595 "\tstfd 18, -112(5)\n"
596 "\tstfd 19, -104(5)\n"
597 "\tstfd 20, -96(5)\n"
598 "\tstfd 21, -88(5)\n"
599 "\tstfd 22, -80(5)\n"
600 "\tstfd 23, -72(5)\n"
601 "\tstfd 24, -64(5)\n"
602 "\tstfd 25, -56(5)\n"
603 "\tstfd 26, -48(5)\n"
604 "\tstfd 27, -40(5)\n"
605 "\tstfd 28, -32(5)\n"
606 "\tstfd 29, -24(5)\n"
607 "\tstfd 30, -16(5)\n"
608 "\tstfd 31, -8(5)\n"
609 "\tmr 27, 4\n" // BaseReg == r27
610 "\tld 2, 8(3)\n"
611 "\tld 3, 0(3)\n"
612 "\tmtctr 3\n"
613 "\tbctr\n"
614 ".globl .StgReturn\n"
615 ".type .StgReturn,@function\n"
616 ".StgReturn:\n"
617 "\tmr 3,14\n"
618 "\tla 5, %0(1)\n" // load address == addi r5, r1, %0
619 "\tld 2, -296(5)\n"
620 "\tld 14, -288(5)\n"
621 "\tld 15, -280(5)\n"
622 "\tld 16, -272(5)\n"
623 "\tld 17, -264(5)\n"
624 "\tld 18, -256(5)\n"
625 "\tld 19, -248(5)\n"
626 "\tld 20, -240(5)\n"
627 "\tld 21, -232(5)\n"
628 "\tld 22, -224(5)\n"
629 "\tld 23, -216(5)\n"
630 "\tld 24, -208(5)\n"
631 "\tld 25, -200(5)\n"
632 "\tld 26, -192(5)\n"
633 "\tld 27, -184(5)\n"
634 "\tld 28, -176(5)\n"
635 "\tld 29, -168(5)\n"
636 "\tld 30, -160(5)\n"
637 "\tld 31, -152(5)\n"
638 "\tlfd 14, -144(5)\n"
639 "\tlfd 15, -136(5)\n"
640 "\tlfd 16, -128(5)\n"
641 "\tlfd 17, -120(5)\n"
642 "\tlfd 18, -112(5)\n"
643 "\tlfd 19, -104(5)\n"
644 "\tlfd 20, -96(5)\n"
645 "\tlfd 21, -88(5)\n"
646 "\tlfd 22, -80(5)\n"
647 "\tlfd 23, -72(5)\n"
648 "\tlfd 24, -64(5)\n"
649 "\tlfd 25, -56(5)\n"
650 "\tlfd 26, -48(5)\n"
651 "\tlfd 27, -40(5)\n"
652 "\tlfd 28, -32(5)\n"
653 "\tlfd 29, -24(5)\n"
654 "\tlfd 30, -16(5)\n"
655 "\tlfd 31, -8(5)\n"
656 "\tmr 1, 5\n"
657 "\tld 0, 16(1)\n"
658 "\tmtlr 0\n"
659 "\tblr\n"
660 : : "i"(RESERVED_C_STACK_BYTES+304 /*stack frame size*/));
661 }
662
663 #else // linux_HOST_OS
664 #error Only Linux support for power64 right now.
665 #endif
666
667 #endif
668
669 #ifdef powerpc64le_HOST_ARCH
670 /* -----------------------------------------------------------------------------
671 PowerPC 64 little endian architecture
672
673 Really everything is in assembler, so we don't have to deal with GCC...
674 -------------------------------------------------------------------------- */
675 #endif
676
677 /* -----------------------------------------------------------------------------
678 ARM architecture
679 -------------------------------------------------------------------------- */
680
681 #ifdef arm_HOST_ARCH
682
683 #if defined(__thumb__)
684 #define THUMB_FUNC ".thumb\n\t.thumb_func\n\t"
685 #else
686 #define THUMB_FUNC
687 #endif
688
689 StgRegTable *
690 StgRun(StgFunPtr f, StgRegTable *basereg) {
691 StgRegTable * r;
692 __asm__ volatile (
693 /*
694 * save callee-saves registers on behalf of the STG code.
695 */
696 "stmfd sp!, {r4-r11, ip, lr}\n\t"
697 #if !defined(arm_HOST_ARCH_PRE_ARMv6)
698 "vstmdb sp!, {d8-d11}\n\t"
699 #endif
700 /*
701 * allocate some space for Stg machine's temporary storage.
702 * Note: RESERVED_C_STACK_BYTES has to be a round number here or
703 * the assembler can't assemble it.
704 */
705 "sub sp, sp, %3\n\t"
706 /*
707 * Set BaseReg
708 */
709 "mov r4, %2\n\t"
710 /*
711 * Jump to function argument.
712 */
713 "bx %1\n\t"
714
715 ".globl " STG_RETURN "\n\t"
716 THUMB_FUNC
717 #if !defined(ios_HOST_OS)
718 ".type " STG_RETURN ", %%function\n"
719 #endif
720 STG_RETURN ":\n\t"
721 /*
722 * Free the space we allocated
723 */
724 "add sp, sp, %3\n\t"
725 /*
726 * Return the new register table, taking it from Stg's R1 (ARM's R7).
727 */
728 "mov %0, r7\n\t"
729 /*
730 * restore callee-saves registers.
731 */
732 #if !defined(arm_HOST_ARCH_PRE_ARMv6)
733 "vldmia sp!, {d8-d11}\n\t"
734 #endif
735 "ldmfd sp!, {r4-r11, ip, lr}\n\t"
736 : "=r" (r)
737 : "r" (f), "r" (basereg), "i" (RESERVED_C_STACK_BYTES)
738 #if !defined(__thumb__)
739 /* In ARM mode, r11/fp is frame-pointer and so we cannot mark
740 it as clobbered. If we do so, GCC complains with error. */
741 : "%r4", "%r5", "%r6", "%r7", "%r8", "%r9", "%r10", "%ip", "%lr"
742 #else
743 /* In Thumb mode r7 is frame-pointer and so we cannot mark it
744 as clobbered. On the other hand we mark as clobbered also
745 those regs not used in Thumb mode. Hard to judge if this is
746 needed, but certainly Haskell code is using them for
747 placing GHC's virtual registers there. See
748 includes/stg/MachRegs.h Please note that Haskell code is
749 compiled by GHC/LLVM into ARM code (not Thumb!), at least
750 as of February 2012 */
751 : "%r4", "%r5", "%r6", "%r8", "%r9", "%r10", "%11", "%ip", "%lr"
752 #endif
753 );
754 return r;
755 }
756 #endif
757
758 #ifdef aarch64_HOST_ARCH
759
760 StgRegTable *
761 StgRun(StgFunPtr f, StgRegTable *basereg) {
762 StgRegTable * r;
763 __asm__ volatile (
764 /*
765 * Save callee-saves registers on behalf of the STG code.
766 * Floating point registers only need the bottom 64 bits preserved.
767 * We need to use the the names x16, x17, x29 and x30 instead of ip0
768 * ip1, fp and lp because one of either clang or gcc doesn't understand
769 * the later names.
770 */
771 "stp x29, x30, [sp, #-16]!\n\t"
772 "mov x29, sp\n\t"
773 "stp x16, x17, [sp, #-16]!\n\t"
774 "stp x19, x20, [sp, #-16]!\n\t"
775 "stp x21, x22, [sp, #-16]!\n\t"
776 "stp x23, x24, [sp, #-16]!\n\t"
777 "stp x25, x26, [sp, #-16]!\n\t"
778 "stp x27, x28, [sp, #-16]!\n\t"
779 "stp d8, d9, [sp, #-16]!\n\t"
780 "stp d10, d11, [sp, #-16]!\n\t"
781 "stp d12, d13, [sp, #-16]!\n\t"
782 "stp d14, d15, [sp, #-16]!\n\t"
783
784 /*
785 * allocate some space for Stg machine's temporary storage.
786 * Note: RESERVED_C_STACK_BYTES has to be a round number here or
787 * the assembler can't assemble it.
788 */
789 "sub sp, sp, %3\n\t"
790 /*
791 * Set BaseReg
792 */
793 "mov x19, %2\n\t"
794 /*
795 * Jump to function argument.
796 */
797 "br %1\n\t"
798
799 ".globl " STG_RETURN "\n\t"
800 #if !defined(ios_HOST_OS)
801 ".type " STG_RETURN ", %%function\n"
802 #endif
803 STG_RETURN ":\n\t"
804 /*
805 * Free the space we allocated
806 */
807 "add sp, sp, %3\n\t"
808 /*
809 * Return the new register table, taking it from Stg's R1 (ARM64's R22).
810 */
811 "mov %0, x22\n\t"
812 /*
813 * restore callee-saves registers.
814 */
815
816 "ldp d14, d15, [sp], #16\n\t"
817 "ldp d12, d13, [sp], #16\n\t"
818 "ldp d10, d11, [sp], #16\n\t"
819 "ldp d8, d9, [sp], #16\n\t"
820 "ldp x27, x28, [sp], #16\n\t"
821 "ldp x25, x26, [sp], #16\n\t"
822 "ldp x23, x24, [sp], #16\n\t"
823 "ldp x21, x22, [sp], #16\n\t"
824 "ldp x19, x20, [sp], #16\n\t"
825 "ldp x16, x17, [sp], #16\n\t"
826 "ldp x29, x30, [sp], #16\n\t"
827
828 : "=r" (r)
829 : "r" (f), "r" (basereg), "i" (RESERVED_C_STACK_BYTES)
830 : "%x19", "%x20", "%x21", "%x22", "%x23", "%x24", "%x25", "%x26", "%x27", "%x28",
831 "%x16", "%x17", "%x30"
832 );
833 return r;
834 }
835
836 #endif
837
838 #endif /* !USE_MINIINTERPRETER */