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