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