15f9fd26a8f632415249d7c37612077bbd20f44a
[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 maitain 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 %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 #if defined(mingw32_HOST_OS)
263 "movq %%rdx,%%r13\n\t"
264 #else
265 "movq %%rsi,%%r13\n\t"
266 #endif
267 /*
268 * grab the function argument from the stack, and jump to it.
269 */
270 #if defined(mingw32_HOST_OS)
271 "movq %%rcx,%%rax\n\t"
272 #else
273 "movq %%rdi,%%rax\n\t"
274 #endif
275 "jmp *%%rax\n\t"
276
277 ".globl " STG_RETURN "\n"
278 STG_RETURN ":\n\t"
279
280 "movq %%rbx, %%rax\n\t" /* Return value in R1 */
281
282 /*
283 * restore callee-saves registers. (Don't stomp on %%rax!)
284 */
285 "movq %%rsp, %%rdx\n\t"
286 "addq %0-48, %%rdx\n\t"
287 "movq 0(%%rdx),%%rbx\n\t" /* restore the registers saved above */
288 "movq 8(%%rdx),%%rbp\n\t"
289 "movq 16(%%rdx),%%r12\n\t"
290 "movq 24(%%rdx),%%r13\n\t"
291 "movq 32(%%rdx),%%r14\n\t"
292 "movq 40(%%rdx),%%r15\n\t"
293 "addq %0, %%rsp\n\t"
294 "retq"
295
296 : : "i"(RESERVED_C_STACK_BYTES + 48 /*stack frame size*/));
297 /*
298 * See Note [Stack Alignment on X86]
299 */
300 }
301
302 #endif /* x86-64 */
303
304 /* -----------------------------------------------------------------------------
305 Sparc architecture
306
307 --
308 OLD COMMENT from GHC-3.02:
309
310 We want tailjumps to be calls, because `call xxx' is the only Sparc
311 branch that allows an arbitrary label as a target. (Gcc's ``goto
312 *target'' construct ends up loading the label into a register and
313 then jumping, at the cost of two extra instructions for the 32-bit
314 load.)
315
316 When entering the threaded world, we stash our return address in a
317 known location so that \tr{%i7} is available as an extra
318 callee-saves register. Of course, we have to restore this when
319 coming out of the threaded world.
320
321 I hate this god-forsaken architecture. Since the top of the
322 reserved stack space is used for globals and the bottom is reserved
323 for outgoing arguments, we have to stick our return address
324 somewhere in the middle. Currently, I'm allowing 100 extra
325 outgoing arguments beyond the first 6. --JSM
326
327 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
328 I'm not sure whether we still need to save it. Incedentally, what
329 does the last paragraph above mean when it says "the top of the
330 stack is used for globals"? What globals? --SDM
331
332 Updated info (GHC 4.08.2): not saving %i7 any more (see below).
333 -------------------------------------------------------------------------- */
334
335 #ifdef sparc_HOST_ARCH
336
337 StgRegTable *
338 StgRun(StgFunPtr f, StgRegTable *basereg) {
339
340 unsigned char space[RESERVED_C_STACK_BYTES];
341 #if 0
342 register void *i7 __asm__("%i7");
343 ((void **)(space))[100] = i7;
344 #endif
345 f();
346 __asm__ volatile (
347 ".align 4\n"
348 ".global " STG_RETURN "\n"
349 STG_RETURN ":"
350 : : "p" (space) : "l0","l1","l2","l3","l4","l5","l6","l7");
351 /* we tell the C compiler that l0-l7 are clobbered on return to
352 * StgReturn, otherwise it tries to use these to save eg. the
353 * address of space[100] across the call. The correct thing
354 * to do would be to save all the callee-saves regs, but we
355 * can't be bothered to do that.
356 *
357 * We also explicitly mark space as used since gcc eliminates it
358 * otherwise.
359 *
360 * The code that gcc generates for this little fragment is now
361 * terrible. We could do much better by coding it directly in
362 * assembler.
363 */
364 #if 0
365 /* updated 4.08.2: we don't save %i7 in the middle of the reserved
366 * space any more, since gcc tries to save its address across the
367 * call to f(), this gets clobbered in STG land and we end up
368 * dereferencing a bogus pointer in StgReturn.
369 */
370 __asm__ volatile ("ld %1,%0"
371 : "=r" (i7) : "m" (((void **)(space))[100]));
372 #endif
373 return (StgRegTable *)R1.i;
374 }
375
376 #endif
377
378 /* -----------------------------------------------------------------------------
379 PowerPC architecture
380
381 Everything is in assembler, so we don't have to deal with GCC...
382 -------------------------------------------------------------------------- */
383
384 #ifdef powerpc_HOST_ARCH
385
386 extern StgRegTable * StgRun(StgFunPtr f, StgRegTable *basereg);
387
388 #ifdef darwin_HOST_OS
389 void StgRunIsImplementedInAssembler(void)
390 {
391 #if HAVE_SUBSECTIONS_VIA_SYMBOLS
392 // if the toolchain supports deadstripping, we have to
393 // prevent it here (it tends to get confused here).
394 __asm__ volatile (".no_dead_strip _StgRunIsImplementedInAssembler");
395 #endif
396 __asm__ volatile (
397 "\n.globl _StgRun\n"
398 "_StgRun:\n"
399 "\tmflr r0\n"
400 "\tbl saveFP # f14\n"
401 "\tstmw r13,-220(r1)\n"
402 "\tstwu r1,-%0(r1)\n"
403 "\tmr r27,r4\n" // BaseReg == r27
404 "\tmtctr r3\n"
405 "\tmr r12,r3\n"
406 "\tbctr\n"
407 ".globl _StgReturn\n"
408 "_StgReturn:\n"
409 "\tmr r3,r14\n"
410 "\tla r1,%0(r1)\n"
411 "\tlmw r13,-220(r1)\n"
412 "\tb restFP # f14\n"
413 : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
414 }
415 #else
416
417 // This version is for PowerPC Linux.
418
419 // Differences from the Darwin/Mac OS X version:
420 // *) Different Assembler Syntax
421 // *) Doesn't use Register Saving Helper Functions (although they exist somewhere)
422 // *) We may not access positive stack offsets
423 // (no "Red Zone" as in the Darwin ABI)
424 // *) The Link Register is saved to a different offset in the caller's stack frame
425 // (Linux: 4(r1), Darwin 8(r1))
426
427 static void GNUC3_ATTRIBUTE(used)
428 StgRunIsImplementedInAssembler(void)
429 {
430 __asm__ volatile (
431 "\t.globl StgRun\n"
432 "\t.type StgRun,@function\n"
433 "StgRun:\n"
434 "\tmflr 0\n"
435 "\tstw 0,4(1)\n"
436 "\tmr 5,1\n"
437 "\tstwu 1,-%0(1)\n"
438 "\tstmw 13,-220(5)\n"
439 "\tstfd 14,-144(5)\n"
440 "\tstfd 15,-136(5)\n"
441 "\tstfd 16,-128(5)\n"
442 "\tstfd 17,-120(5)\n"
443 "\tstfd 18,-112(5)\n"
444 "\tstfd 19,-104(5)\n"
445 "\tstfd 20,-96(5)\n"
446 "\tstfd 21,-88(5)\n"
447 "\tstfd 22,-80(5)\n"
448 "\tstfd 23,-72(5)\n"
449 "\tstfd 24,-64(5)\n"
450 "\tstfd 25,-56(5)\n"
451 "\tstfd 26,-48(5)\n"
452 "\tstfd 27,-40(5)\n"
453 "\tstfd 28,-32(5)\n"
454 "\tstfd 29,-24(5)\n"
455 "\tstfd 30,-16(5)\n"
456 "\tstfd 31,-8(5)\n"
457 "\tmr 27,4\n" // BaseReg == r27
458 "\tmtctr 3\n"
459 "\tmr 12,3\n"
460 "\tbctr\n"
461 ".globl StgReturn\n"
462 "\t.type StgReturn,@function\n"
463 "StgReturn:\n"
464 "\tmr 3,14\n"
465 "\tla 5,%0(1)\n"
466 "\tlmw 13,-220(5)\n"
467 "\tlfd 14,-144(5)\n"
468 "\tlfd 15,-136(5)\n"
469 "\tlfd 16,-128(5)\n"
470 "\tlfd 17,-120(5)\n"
471 "\tlfd 18,-112(5)\n"
472 "\tlfd 19,-104(5)\n"
473 "\tlfd 20,-96(5)\n"
474 "\tlfd 21,-88(5)\n"
475 "\tlfd 22,-80(5)\n"
476 "\tlfd 23,-72(5)\n"
477 "\tlfd 24,-64(5)\n"
478 "\tlfd 25,-56(5)\n"
479 "\tlfd 26,-48(5)\n"
480 "\tlfd 27,-40(5)\n"
481 "\tlfd 28,-32(5)\n"
482 "\tlfd 29,-24(5)\n"
483 "\tlfd 30,-16(5)\n"
484 "\tlfd 31,-8(5)\n"
485 "\tmr 1,5\n"
486 "\tlwz 0,4(1)\n"
487 "\tmtlr 0\n"
488 "\tblr\n"
489 : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
490 }
491 #endif
492
493 #endif
494
495 /* -----------------------------------------------------------------------------
496 PowerPC 64 architecture
497
498 Everything is in assembler, so we don't have to deal with GCC...
499 -------------------------------------------------------------------------- */
500
501 #ifdef powerpc64_HOST_ARCH
502
503 #ifdef linux_HOST_OS
504 extern StgRegTable * StgRun(StgFunPtr f, StgRegTable *basereg);
505
506 static void GNUC3_ATTRIBUTE(used)
507 StgRunIsImplementedInAssembler(void)
508 {
509 // r0 volatile
510 // r1 stack pointer
511 // r2 toc - needs to be saved
512 // r3-r10 argument passing, volatile
513 // r11, r12 very volatile (not saved across cross-module calls)
514 // r13 thread local state (never modified, don't need to save)
515 // r14-r31 callee-save
516 __asm__ volatile (
517 ".section \".opd\",\"aw\"\n"
518 ".align 3\n"
519 ".globl StgRun\n"
520 "StgRun:\n"
521 "\t.quad\t.StgRun,.TOC.@tocbase,0\n"
522 "\t.size StgRun,24\n"
523 ".globl StgReturn\n"
524 "StgReturn:\n"
525 "\t.quad\t.StgReturn,.TOC.@tocbase,0\n"
526 "\t.size StgReturn,24\n"
527 ".previous\n"
528 ".globl .StgRun\n"
529 ".type .StgRun,@function\n"
530 ".StgRun:\n"
531 "\tmflr 0\n"
532 "\tmr 5, 1\n"
533 "\tstd 0, 16(1)\n"
534 "\tstdu 1, -%0(1)\n"
535 "\tstd 2, -296(5)\n"
536 "\tstd 14, -288(5)\n"
537 "\tstd 15, -280(5)\n"
538 "\tstd 16, -272(5)\n"
539 "\tstd 17, -264(5)\n"
540 "\tstd 18, -256(5)\n"
541 "\tstd 19, -248(5)\n"
542 "\tstd 20, -240(5)\n"
543 "\tstd 21, -232(5)\n"
544 "\tstd 22, -224(5)\n"
545 "\tstd 23, -216(5)\n"
546 "\tstd 24, -208(5)\n"
547 "\tstd 25, -200(5)\n"
548 "\tstd 26, -192(5)\n"
549 "\tstd 27, -184(5)\n"
550 "\tstd 28, -176(5)\n"
551 "\tstd 29, -168(5)\n"
552 "\tstd 30, -160(5)\n"
553 "\tstd 31, -152(5)\n"
554 "\tstfd 14, -144(5)\n"
555 "\tstfd 15, -136(5)\n"
556 "\tstfd 16, -128(5)\n"
557 "\tstfd 17, -120(5)\n"
558 "\tstfd 18, -112(5)\n"
559 "\tstfd 19, -104(5)\n"
560 "\tstfd 20, -96(5)\n"
561 "\tstfd 21, -88(5)\n"
562 "\tstfd 22, -80(5)\n"
563 "\tstfd 23, -72(5)\n"
564 "\tstfd 24, -64(5)\n"
565 "\tstfd 25, -56(5)\n"
566 "\tstfd 26, -48(5)\n"
567 "\tstfd 27, -40(5)\n"
568 "\tstfd 28, -32(5)\n"
569 "\tstfd 29, -24(5)\n"
570 "\tstfd 30, -16(5)\n"
571 "\tstfd 31, -8(5)\n"
572 "\tmr 27, 4\n" // BaseReg == r27
573 "\tld 2, 8(3)\n"
574 "\tld 3, 0(3)\n"
575 "\tmtctr 3\n"
576 "\tbctr\n"
577 ".globl .StgReturn\n"
578 ".type .StgReturn,@function\n"
579 ".StgReturn:\n"
580 "\tmr 3,14\n"
581 "\tla 5, %0(1)\n" // load address == addi r5, r1, %0
582 "\tld 2, -296(5)\n"
583 "\tld 14, -288(5)\n"
584 "\tld 15, -280(5)\n"
585 "\tld 16, -272(5)\n"
586 "\tld 17, -264(5)\n"
587 "\tld 18, -256(5)\n"
588 "\tld 19, -248(5)\n"
589 "\tld 20, -240(5)\n"
590 "\tld 21, -232(5)\n"
591 "\tld 22, -224(5)\n"
592 "\tld 23, -216(5)\n"
593 "\tld 24, -208(5)\n"
594 "\tld 25, -200(5)\n"
595 "\tld 26, -192(5)\n"
596 "\tld 27, -184(5)\n"
597 "\tld 28, -176(5)\n"
598 "\tld 29, -168(5)\n"
599 "\tld 30, -160(5)\n"
600 "\tld 31, -152(5)\n"
601 "\tlfd 14, -144(5)\n"
602 "\tlfd 15, -136(5)\n"
603 "\tlfd 16, -128(5)\n"
604 "\tlfd 17, -120(5)\n"
605 "\tlfd 18, -112(5)\n"
606 "\tlfd 19, -104(5)\n"
607 "\tlfd 20, -96(5)\n"
608 "\tlfd 21, -88(5)\n"
609 "\tlfd 22, -80(5)\n"
610 "\tlfd 23, -72(5)\n"
611 "\tlfd 24, -64(5)\n"
612 "\tlfd 25, -56(5)\n"
613 "\tlfd 26, -48(5)\n"
614 "\tlfd 27, -40(5)\n"
615 "\tlfd 28, -32(5)\n"
616 "\tlfd 29, -24(5)\n"
617 "\tlfd 30, -16(5)\n"
618 "\tlfd 31, -8(5)\n"
619 "\tmr 1, 5\n"
620 "\tld 0, 16(1)\n"
621 "\tmtlr 0\n"
622 "\tblr\n"
623 : : "i"(RESERVED_C_STACK_BYTES+304 /*stack frame size*/));
624 }
625
626 #else // linux_HOST_OS
627 #error Only linux support for power64 right now.
628 #endif
629
630 #endif
631
632 /* -----------------------------------------------------------------------------
633 ARM architecture
634 -------------------------------------------------------------------------- */
635
636 #ifdef arm_HOST_ARCH
637
638 #if defined(__thumb__)
639 #define THUMB_FUNC ".thumb\n\t.thumb_func\n\t"
640 #else
641 #define THUMB_FUNC
642 #endif
643
644 StgRegTable *
645 StgRun(StgFunPtr f, StgRegTable *basereg) {
646 StgRegTable * r;
647 __asm__ volatile (
648 /*
649 * save callee-saves registers on behalf of the STG code.
650 */
651 "stmfd sp!, {r4-r10, fp, ip, lr}\n\t"
652 #if !defined(arm_HOST_ARCH_PRE_ARMv6)
653 "vstmdb sp!, {d8-d11}\n\t"
654 #endif
655 /*
656 * allocate some space for Stg machine's temporary storage.
657 * Note: RESERVER_C_STACK_BYTES has to be a round number here or
658 * the assembler can't assemble it.
659 */
660 "sub sp, sp, %3\n\t"
661 /*
662 * Set BaseReg
663 */
664 "mov r4, %2\n\t"
665 /*
666 * Jump to function argument.
667 */
668 "bx %1\n\t"
669
670 ".global " STG_RETURN "\n\t"
671 THUMB_FUNC
672 ".type " STG_RETURN ", %%function\n"
673 STG_RETURN ":\n\t"
674 /*
675 * Free the space we allocated
676 */
677 "add sp, sp, %3\n\t"
678 /*
679 * Return the new register table, taking it from Stg's R1 (ARM's R7).
680 */
681 "mov %0, r7\n\t"
682 /*
683 * restore callee-saves registers.
684 */
685 #if !defined(arm_HOST_ARCH_PRE_ARMv6)
686 "vldmia sp!, {d8-d11}\n\t"
687 #endif
688 "ldmfd sp!, {r4-r10, fp, ip, lr}\n\t"
689 : "=r" (r)
690 : "r" (f), "r" (basereg), "i" (RESERVED_C_STACK_BYTES)
691 #if !defined(__thumb__)
692 /* In ARM mode, r11/fp is frame-pointer and so we cannot mark
693 it as clobbered. If we do so, GCC complains with error. */
694 : "%r4", "%r5", "%r6", "%r7", "%r8", "%r9", "%r10", "%ip", "%lr"
695 #else
696 /* In Thumb mode r7 is frame-pointer and so we cannot mark it
697 as clobbered. On the other hand we mark as clobbered also
698 those regs not used in Thumb mode. Hard to judge if this is
699 needed, but certainly Haskell code is using them for
700 placing GHC's virtual registers there. See
701 includes/stg/MachRegs.h Please note that Haskell code is
702 compiled by GHC/LLVM into ARM code (not Thumb!), at least
703 as of February 2012 */
704 : "%r4", "%r5", "%r6", "%r8", "%r9", "%r10", "%fp", "%ip", "%lr"
705 #endif
706 );
707 return r;
708 }
709 #endif
710
711 #endif /* !USE_MINIINTERPRETER */