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