hadrian: eliminate most of the remaining big rule enumerations
[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 #include "RtsUtils.h"
63 #if defined(DEBUG)
64 #include "Printer.h"
65 #endif
66
67 #if defined(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 #if defined(mingw32_HOST_OS)
94 /*
95 * Note [Windows Stack allocations]
96 *
97 * On windows the stack has to be allocated 4k at a time, otherwise
98 * we get a segfault. The C compiler knows how to do this (it calls
99 * _alloca()), so we make sure that we can allocate as much stack as
100 * we need. However since we are doing a local stack allocation and the value
101 * isn't valid outside the frame, compilers are free to optimize this allocation
102 * and the corresponding stack check away. So to prevent that we request that
103 * this function never be optimized (See #14669). */
104 STG_NO_OPTIMIZE StgWord8 *win32AllocStack(void)
105 {
106 StgWord8 stack[RESERVED_C_STACK_BYTES + 16 + 12];
107 return stack;
108 }
109 #endif
110
111 /* -----------------------------------------------------------------------------
112 x86 architecture
113 -------------------------------------------------------------------------- */
114
115 #if defined(i386_HOST_ARCH)
116
117 #if defined(darwin_HOST_OS) || defined(ios_HOST_OS)
118 #define STG_GLOBAL ".globl "
119 #define STG_HIDDEN ".private_extern "
120 #else
121 #define STG_GLOBAL ".global "
122 #define STG_HIDDEN ".hidden "
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 maintain 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 compatibility 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 * If you edit the sequence below be sure to update the unwinding information
161 * for stg_stop_thread in StgStartup.cmm.
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 // defined(i386_HOST_ARCH)
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 #if defined(x86_64_HOST_ARCH)
243
244 #define STG_GLOBAL ".globl "
245
246 #if defined(darwin_HOST_OS) || defined(ios_HOST_OS)
247 #define STG_HIDDEN ".private_extern "
248 #else
249 #define STG_HIDDEN ".hidden "
250 #endif
251
252 /*
253 Note [Unwinding foreign exports on x86-64]
254 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
255 For foreign exports, that is Haskell functions exported as C functions when
256 we unwind we have to unwind from Haskell code into C code. The current story
257 is as follows:
258
259 * The Haskell stack always has stg_stop_thread_info frame at the bottom
260 * We annotate stg_stop_thread_info to unwind the instruction pointer to a
261 label inside StgRun called StgRunJmp. It's the last instruction before the
262 code jumps into Haskell.
263 * StgRun - which is implemented in assembler is annotated with some manual
264 unwinding information. It unwinds all the registers that it has saved
265 on the stack. This is important as rsp and rbp are often required for
266 getting to the next frame and the rest of the saved registers are useful
267 when inspecting locals in gdb.
268
269
270 Example x86-64 stack for an FFI call
271 from C into a Haskell function:
272
273
274 HASKELL HEAP
275 "ADDRESS SPACE"
276
277 +--------------------+ <------ rbp
278 | |
279 | |
280 | |
281 | |
282 | Haskell |
283 | evaluation stack |
284 | |
285 | |
286 |--------------------|
287 |stg_catch_frame_info|
288 |--------------------|
289 | stg_forceIO_info |
290 |--------------------|
291 |stg_stop_thread_info| -------
292 +--------------------+ |
293 ... |
294 (other heap objects) |
295 ... |
296 |
297 |
298 |
299 C STACK "ADDRESS SPACE" |
300 v
301 +-----------------------------+ <------ rsp
302 | |
303 | RESERVED_C_STACK_BYTES ~16k |
304 | |
305 |-----------------------------|
306 | rbx ||
307 |-----------------------------| \
308 | rbp | |
309 |-----------------------------| \
310 | r12 | |
311 |-----------------------------| \
312 | r13 | | STG_RUN_STACK_FRAME_SIZE
313 |-----------------------------| /
314 | r14 | |
315 |-----------------------------| /
316 | r15 | |
317 |-----------------------------|/
318 | rip saved by call StgRun |
319 | in schedule() |
320 +-----------------------------+
321 ...
322 schedule() stack frame
323
324
325 Lower addresses on the top
326
327 One little snag in this approach is that the annotations accepted by the
328 assembler are surprisingly unexpressive. I had to resort to a .cfi_escape
329 and hand-assemble a DWARF expression. What made it worse was that big numbers
330 are LEB128 encoded, which makes them variable byte length, with length depending
331 on the magnitude.
332
333 Here's an example stack generated this way:
334
335 Thread 1 "m" hit Breakpoint 1, Fib_zdfstableZZC0ZZCmainZZCFibZZCfib1_info () at Fib.hs:9
336 9 fib a = return (a + 1)
337 #0 Fib_zdfstableZZC0ZZCmainZZCFibZZCfib1_info () at Fib.hs:9
338 #1 stg_catch_frame_info () at rts/Exception.cmm:372
339 #2 stg_forceIO_info () at rts/StgStartup.cmm:178
340 #3 stg_stop_thread_info () at rts/StgStartup.cmm:42
341 #4 0x00000000007048ab in StgRunIsImplementedInAssembler () at rts/StgCRun.c:255
342 #5 0x00000000006fcf42 in schedule (initialCapability=initialCapability@entry=0x8adac0 <MainCapability>, task=task@entry=0x8cf2a0) at rts/Schedule.c:451
343 #6 0x00000000006fe18e in scheduleWaitThread (tso=0x4200006388, ret=<optimized out>, pcap=0x7fffffffdac0) at rts/Schedule.c:2533
344 #7 0x000000000040a21e in hs_fib ()
345 #8 0x000000000040a083 in main (argc=1, argv=0x7fffffffdc48) at m.cpp:15
346
347 (This is from patched gdb. See Note [Info Offset].)
348
349 The previous approach was to encode the unwinding information for select
350 registers in stg_stop_thread_info with Cmm annotations. The unfortunate thing
351 about that approach was that it required introduction of an artificial MachSp
352 register that wasn't meaningful outside unwinding. I discovered that to get
353 stack unwinding working under -threaded runtime I also needed to unwind rbp
354 which would require adding MachRbp. If we wanted to see saved locals in gdb,
355 we'd have to add more. The core of the problem is that Cmm is architecture
356 independent, while unwinding isn't.
357
358 Note [Unwinding foreign imports]
359 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360 For unwinding foreign imports, that is C functions exposed as Haskell functions
361 no special handling is required. The C function unwinds according to the rip
362 saved on the stack by the call instruction. Then we perform regular Haskell
363 stack unwinding.
364 */
365
366
367 static void GNUC3_ATTRIBUTE(used)
368 StgRunIsImplementedInAssembler(void)
369 {
370 __asm__ volatile (
371 /*
372 * save callee-saves registers on behalf of the STG code.
373 */
374 STG_GLOBAL STG_RUN "\n"
375 #if !defined(mingw32_HOST_OS)
376 STG_HIDDEN STG_RUN "\n"
377 #endif
378 STG_RUN ":\n\t"
379 "subq %1, %%rsp\n\t"
380 "movq %%rsp, %%rax\n\t"
381 "subq %0, %%rsp\n\t"
382 "movq %%rbx,0(%%rax)\n\t"
383 "movq %%rbp,8(%%rax)\n\t"
384 "movq %%r12,16(%%rax)\n\t"
385 "movq %%r13,24(%%rax)\n\t"
386 "movq %%r14,32(%%rax)\n\t"
387 "movq %%r15,40(%%rax)\n\t"
388 #if defined(mingw32_HOST_OS)
389 /*
390 * Additional callee saved registers on Win64. This must match
391 * callClobberedRegisters in compiler/nativeGen/X86/Regs.hs as
392 * both represent the Win64 calling convention.
393 */
394 "movq %%rdi,48(%%rax)\n\t"
395 "movq %%rsi,56(%%rax)\n\t"
396 "movq %%xmm6, 64(%%rax)\n\t"
397 "movq %%xmm7, 72(%%rax)\n\t"
398 "movq %%xmm8, 80(%%rax)\n\t"
399 "movq %%xmm9, 88(%%rax)\n\t"
400 "movq %%xmm10, 96(%%rax)\n\t"
401 "movq %%xmm11,104(%%rax)\n\t"
402 "movq %%xmm12,112(%%rax)\n\t"
403 "movq %%xmm13,120(%%rax)\n\t"
404 "movq %%xmm14,128(%%rax)\n\t"
405 "movq %%xmm15,136(%%rax)\n\t"
406 #endif
407
408 /*
409 * Let the unwinder know where we saved the registers
410 * See Note [Unwinding foreign exports on x86-64].
411 */
412 ".cfi_def_cfa rsp, 0\n\t"
413 ".cfi_offset rbx, %c2\n\t"
414 ".cfi_offset rbp, %c3\n\t"
415 ".cfi_offset r12, %c4\n\t"
416 ".cfi_offset r13, %c5\n\t"
417 ".cfi_offset r14, %c6\n\t"
418 ".cfi_offset r15, %c7\n\t"
419 ".cfi_offset rip, %c8\n\t"
420 ".cfi_escape " // DW_CFA_val_expression is not expressible otherwise
421 "0x16, " // DW_CFA_val_expression
422 "0x07, " // register num 7 - rsp
423 "0x04, " // block length
424 "0x77, " // DW_OP_breg7 - signed LEB128 offset from rsp
425 #define RSP_DELTA (RESERVED_C_STACK_BYTES + STG_RUN_STACK_FRAME_SIZE + 8)
426 "%c9" // signed LEB128 encoded delta - byte 1
427 #if (RSP_DELTA >> 7) > 0
428 ", %c10" // signed LEB128 encoded delta - byte 2
429 #endif
430
431 #if (RSP_DELTA >> 14) > 0
432 ", %c11" // signed LEB128 encoded delta - byte 3
433 #endif
434
435 #if (RSP_DELTA >> 21) > 0
436 ", %c12" // signed LEB128 encoded delta - byte 4
437 #endif
438
439 #if (RSP_DELTA >> 28) > 0
440 #error "RSP_DELTA too big"
441 #endif
442 "\n\t"
443
444 /*
445 * Set BaseReg
446 */
447 #if defined(mingw32_HOST_OS)
448 "movq %%rdx,%%r13\n\t"
449 #else
450 "movq %%rsi,%%r13\n\t"
451 #endif
452 /*
453 * grab the function argument from the stack, and jump to it.
454 */
455 #if defined(mingw32_HOST_OS)
456 "movq %%rcx,%%rax\n\t"
457 #else
458 "movq %%rdi,%%rax\n\t"
459 #endif
460
461 STG_GLOBAL xstr(STG_RUN_JMP) "\n"
462 #if !defined(mingw32_HOST_OS)
463 STG_HIDDEN xstr(STG_RUN_JMP) "\n"
464 #endif
465 #if HAVE_SUBSECTIONS_VIA_SYMBOLS
466 // If we have deadstripping enabled and a label is detected as unused
467 // the code gets nop'd out.
468 ".no_dead_strip " xstr(STG_RUN_JMP) "\n"
469 #endif
470 xstr(STG_RUN_JMP) ":\n\t"
471 "jmp *%%rax\n\t"
472
473 ".globl " STG_RETURN "\n"
474 STG_RETURN ":\n\t"
475
476 "movq %%rbx, %%rax\n\t" /* Return value in R1 */
477
478 /*
479 * restore callee-saves registers. (Don't stomp on %%rax!)
480 */
481 "addq %0, %%rsp\n\t"
482 "movq 0(%%rsp),%%rbx\n\t" /* restore the registers saved above */
483 "movq 8(%%rsp),%%rbp\n\t"
484 "movq 16(%%rsp),%%r12\n\t"
485 "movq 24(%%rsp),%%r13\n\t"
486 "movq 32(%%rsp),%%r14\n\t"
487 "movq 40(%%rsp),%%r15\n\t"
488 #if defined(mingw32_HOST_OS)
489 "movq 48(%%rsp),%%rdi\n\t"
490 "movq 56(%%rsp),%%rsi\n\t"
491 "movq 64(%%rsp),%%xmm6\n\t"
492 "movq 72(%%rax),%%xmm7\n\t"
493 "movq 80(%%rax),%%xmm8\n\t"
494 "movq 88(%%rax),%%xmm9\n\t"
495 "movq 96(%%rax),%%xmm10\n\t"
496 "movq 104(%%rax),%%xmm11\n\t"
497 "movq 112(%%rax),%%xmm12\n\t"
498 "movq 120(%%rax),%%xmm13\n\t"
499 "movq 128(%%rax),%%xmm14\n\t"
500 "movq 136(%%rax),%%xmm15\n\t"
501 #endif
502 "addq %1, %%rsp\n\t"
503 "retq"
504
505 :
506 : "i"(RESERVED_C_STACK_BYTES),
507 "i"(STG_RUN_STACK_FRAME_SIZE /* stack frame size */),
508 "i"(RESERVED_C_STACK_BYTES /* rbx relative to cfa (rsp) */),
509 "i"(RESERVED_C_STACK_BYTES + 8 /* rbp relative to cfa (rsp) */),
510 "i"(RESERVED_C_STACK_BYTES + 16 /* r12 relative to cfa (rsp) */),
511 "i"(RESERVED_C_STACK_BYTES + 24 /* r13 relative to cfa (rsp) */),
512 "i"(RESERVED_C_STACK_BYTES + 32 /* r14 relative to cfa (rsp) */),
513 "i"(RESERVED_C_STACK_BYTES + 40 /* r15 relative to cfa (rsp) */),
514 "i"(RESERVED_C_STACK_BYTES + STG_RUN_STACK_FRAME_SIZE
515 /* rip relative to cfa */),
516 "i"((RSP_DELTA & 127) | (128 * ((RSP_DELTA >> 7) > 0)))
517 /* signed LEB128-encoded delta from rsp - byte 1 */
518 #if (RSP_DELTA >> 7) > 0
519 , "i"(((RSP_DELTA >> 7) & 127) | (128 * ((RSP_DELTA >> 14) > 0)))
520 /* signed LEB128-encoded delta from rsp - byte 2 */
521 #endif
522
523 #if (RSP_DELTA >> 14) > 0
524 , "i"(((RSP_DELTA >> 14) & 127) | (128 * ((RSP_DELTA >> 21) > 0)))
525 /* signed LEB128-encoded delta from rsp - byte 3 */
526 #endif
527
528 #if (RSP_DELTA >> 21) > 0
529 , "i"(((RSP_DELTA >> 21) & 127) | (128 * ((RSP_DELTA >> 28) > 0)))
530 /* signed LEB128-encoded delta from rsp - byte 4 */
531 #endif
532 #undef RSP_DELTA
533 );
534 /*
535 * See Note [Stack Alignment on X86]
536 */
537 }
538
539 #endif /* x86-64 */
540
541 /* -----------------------------------------------------------------------------
542 Sparc architecture
543
544 --
545 OLD COMMENT from GHC-3.02:
546
547 We want tailjumps to be calls, because `call xxx' is the only Sparc
548 branch that allows an arbitrary label as a target. (Gcc's ``goto
549 *target'' construct ends up loading the label into a register and
550 then jumping, at the cost of two extra instructions for the 32-bit
551 load.)
552
553 When entering the threaded world, we stash our return address in a
554 known location so that \tr{%i7} is available as an extra
555 callee-saves register. Of course, we have to restore this when
556 coming out of the threaded world.
557
558 I hate this god-forsaken architecture. Since the top of the
559 reserved stack space is used for globals and the bottom is reserved
560 for outgoing arguments, we have to stick our return address
561 somewhere in the middle. Currently, I'm allowing 100 extra
562 outgoing arguments beyond the first 6. --JSM
563
564 Updated info (GHC 4.06): we don't appear to use %i7 any more, so
565 I'm not sure whether we still need to save it. Incedentally, what
566 does the last paragraph above mean when it says "the top of the
567 stack is used for globals"? What globals? --SDM
568
569 Updated info (GHC 4.08.2): not saving %i7 any more (see below).
570 -------------------------------------------------------------------------- */
571
572 #if defined(sparc_HOST_ARCH)
573
574 StgRegTable *
575 StgRun(StgFunPtr f, StgRegTable *basereg) {
576
577 unsigned char space[RESERVED_C_STACK_BYTES];
578 #if 0
579 register void *i7 __asm__("%i7");
580 ((void **)(space))[100] = i7;
581 #endif
582 f();
583 __asm__ volatile (
584 ".align 4\n"
585 ".global " STG_RETURN "\n"
586 STG_RETURN ":"
587 : : "p" (space) : "l0","l1","l2","l3","l4","l5","l6","l7");
588 /* we tell the C compiler that l0-l7 are clobbered on return to
589 * StgReturn, otherwise it tries to use these to save eg. the
590 * address of space[100] across the call. The correct thing
591 * to do would be to save all the callee-saves regs, but we
592 * can't be bothered to do that.
593 *
594 * We also explicitly mark space as used since gcc eliminates it
595 * otherwise.
596 *
597 * The code that gcc generates for this little fragment is now
598 * terrible. We could do much better by coding it directly in
599 * assembler.
600 */
601 #if 0
602 /* updated 4.08.2: we don't save %i7 in the middle of the reserved
603 * space any more, since gcc tries to save its address across the
604 * call to f(), this gets clobbered in STG land and we end up
605 * dereferencing a bogus pointer in StgReturn.
606 */
607 __asm__ volatile ("ld %1,%0"
608 : "=r" (i7) : "m" (((void **)(space))[100]));
609 #endif
610 return (StgRegTable *)R1.i;
611 }
612
613 #endif
614
615 /* -----------------------------------------------------------------------------
616 PowerPC architecture
617
618 Everything is in assembler, so we don't have to deal with GCC...
619 -------------------------------------------------------------------------- */
620
621 #if defined(powerpc_HOST_ARCH)
622
623 #define STG_GLOBAL ".globl "
624
625 #if defined(darwin_HOST_OS)
626 #define STG_HIDDEN ".private_extern "
627 #else
628 #define STG_HIDDEN ".hidden "
629 #endif
630
631 #if defined(aix_HOST_OS)
632
633 // implementation is in StgCRunAsm.S
634
635 #elif defined(darwin_HOST_OS)
636 void StgRunIsImplementedInAssembler(void)
637 {
638 #if HAVE_SUBSECTIONS_VIA_SYMBOLS
639 // if the toolchain supports deadstripping, we have to
640 // prevent it here (it tends to get confused here).
641 __asm__ volatile (".no_dead_strip _StgRunIsImplementedInAssembler\n");
642 #endif
643 __asm__ volatile (
644 STG_GLOBAL STG_RUN "\n"
645 STG_HIDDEN STG_RUN "\n"
646 STG_RUN ":\n"
647 "\tmflr r0\n"
648 "\tbl saveFP # f14\n"
649 "\tstmw r13,-220(r1)\n"
650 "\tstwu r1,-%0(r1)\n"
651 "\tmr r27,r4\n" // BaseReg == r27
652 "\tmtctr r3\n"
653 "\tmr r12,r3\n"
654 "\tbctr\n"
655 ".globl _StgReturn\n"
656 "_StgReturn:\n"
657 "\tmr r3,r14\n"
658 "\tla r1,%0(r1)\n"
659 "\tlmw r13,-220(r1)\n"
660 "\tb restFP # f14\n"
661 : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
662 }
663 #else
664
665 // This version is for PowerPC Linux.
666
667 // Differences from the Darwin/Mac OS X version:
668 // *) Different Assembler Syntax
669 // *) Doesn't use Register Saving Helper Functions (although they exist somewhere)
670 // *) We may not access positive stack offsets
671 // (no "Red Zone" as in the Darwin ABI)
672 // *) The Link Register is saved to a different offset in the caller's stack frame
673 // (Linux: 4(r1), Darwin 8(r1))
674
675 static void GNUC3_ATTRIBUTE(used)
676 StgRunIsImplementedInAssembler(void)
677 {
678 __asm__ volatile (
679 "\t.globl StgRun\n"
680 "\t.hidden StgRun\n"
681 "\t.type StgRun,@function\n"
682 "StgRun:\n"
683 "\tmflr 0\n"
684 "\tstw 0,4(1)\n"
685 "\tmr 5,1\n"
686 "\tstwu 1,-%0(1)\n"
687 "\tstmw 13,-220(5)\n"
688 "\tstfd 14,-144(5)\n"
689 "\tstfd 15,-136(5)\n"
690 "\tstfd 16,-128(5)\n"
691 "\tstfd 17,-120(5)\n"
692 "\tstfd 18,-112(5)\n"
693 "\tstfd 19,-104(5)\n"
694 "\tstfd 20,-96(5)\n"
695 "\tstfd 21,-88(5)\n"
696 "\tstfd 22,-80(5)\n"
697 "\tstfd 23,-72(5)\n"
698 "\tstfd 24,-64(5)\n"
699 "\tstfd 25,-56(5)\n"
700 "\tstfd 26,-48(5)\n"
701 "\tstfd 27,-40(5)\n"
702 "\tstfd 28,-32(5)\n"
703 "\tstfd 29,-24(5)\n"
704 "\tstfd 30,-16(5)\n"
705 "\tstfd 31,-8(5)\n"
706 "\tmr 27,4\n" // BaseReg == r27
707 "\tmtctr 3\n"
708 "\tmr 12,3\n"
709 "\tbctr\n"
710 ".globl StgReturn\n"
711 "\t.type StgReturn,@function\n"
712 "StgReturn:\n"
713 "\tmr 3,14\n"
714 "\tla 5,%0(1)\n"
715 "\tlmw 13,-220(5)\n"
716 "\tlfd 14,-144(5)\n"
717 "\tlfd 15,-136(5)\n"
718 "\tlfd 16,-128(5)\n"
719 "\tlfd 17,-120(5)\n"
720 "\tlfd 18,-112(5)\n"
721 "\tlfd 19,-104(5)\n"
722 "\tlfd 20,-96(5)\n"
723 "\tlfd 21,-88(5)\n"
724 "\tlfd 22,-80(5)\n"
725 "\tlfd 23,-72(5)\n"
726 "\tlfd 24,-64(5)\n"
727 "\tlfd 25,-56(5)\n"
728 "\tlfd 26,-48(5)\n"
729 "\tlfd 27,-40(5)\n"
730 "\tlfd 28,-32(5)\n"
731 "\tlfd 29,-24(5)\n"
732 "\tlfd 30,-16(5)\n"
733 "\tlfd 31,-8(5)\n"
734 "\tmr 1,5\n"
735 "\tlwz 0,4(1)\n"
736 "\tmtlr 0\n"
737 "\tblr\n"
738 : : "i"(RESERVED_C_STACK_BYTES+224 /*stack frame size*/));
739 }
740 #endif
741
742 #endif
743
744 /* -----------------------------------------------------------------------------
745 PowerPC 64 architecture
746
747 Everything is in assembler, so we don't have to deal with GCC...
748 -------------------------------------------------------------------------- */
749
750 #if defined(powerpc64_HOST_ARCH)
751
752 #if defined(linux_HOST_OS)
753 static void GNUC3_ATTRIBUTE(used)
754 StgRunIsImplementedInAssembler(void)
755 {
756 // r0 volatile
757 // r1 stack pointer
758 // r2 toc - needs to be saved
759 // r3-r10 argument passing, volatile
760 // r11, r12 very volatile (not saved across cross-module calls)
761 // r13 thread local state (never modified, don't need to save)
762 // r14-r31 callee-save
763 __asm__ volatile (
764 ".section \".opd\",\"aw\"\n"
765 ".align 3\n"
766 ".globl StgRun\n"
767 ".hidden StgRun\n"
768 "StgRun:\n"
769 "\t.quad\t.StgRun,.TOC.@tocbase,0\n"
770 "\t.size StgRun,24\n"
771 ".globl StgReturn\n"
772 "StgReturn:\n"
773 "\t.quad\t.StgReturn,.TOC.@tocbase,0\n"
774 "\t.size StgReturn,24\n"
775 ".previous\n"
776 ".globl .StgRun\n"
777 ".type .StgRun,@function\n"
778 ".StgRun:\n"
779 "\tmflr 0\n"
780 "\tmr 5, 1\n"
781 "\tstd 0, 16(1)\n"
782 "\tstdu 1, -%0(1)\n"
783 "\tstd 2, -296(5)\n"
784 "\tstd 14, -288(5)\n"
785 "\tstd 15, -280(5)\n"
786 "\tstd 16, -272(5)\n"
787 "\tstd 17, -264(5)\n"
788 "\tstd 18, -256(5)\n"
789 "\tstd 19, -248(5)\n"
790 "\tstd 20, -240(5)\n"
791 "\tstd 21, -232(5)\n"
792 "\tstd 22, -224(5)\n"
793 "\tstd 23, -216(5)\n"
794 "\tstd 24, -208(5)\n"
795 "\tstd 25, -200(5)\n"
796 "\tstd 26, -192(5)\n"
797 "\tstd 27, -184(5)\n"
798 "\tstd 28, -176(5)\n"
799 "\tstd 29, -168(5)\n"
800 "\tstd 30, -160(5)\n"
801 "\tstd 31, -152(5)\n"
802 "\tstfd 14, -144(5)\n"
803 "\tstfd 15, -136(5)\n"
804 "\tstfd 16, -128(5)\n"
805 "\tstfd 17, -120(5)\n"
806 "\tstfd 18, -112(5)\n"
807 "\tstfd 19, -104(5)\n"
808 "\tstfd 20, -96(5)\n"
809 "\tstfd 21, -88(5)\n"
810 "\tstfd 22, -80(5)\n"
811 "\tstfd 23, -72(5)\n"
812 "\tstfd 24, -64(5)\n"
813 "\tstfd 25, -56(5)\n"
814 "\tstfd 26, -48(5)\n"
815 "\tstfd 27, -40(5)\n"
816 "\tstfd 28, -32(5)\n"
817 "\tstfd 29, -24(5)\n"
818 "\tstfd 30, -16(5)\n"
819 "\tstfd 31, -8(5)\n"
820 "\tmr 27, 4\n" // BaseReg == r27
821 "\tld 2, 8(3)\n"
822 "\tld 3, 0(3)\n"
823 "\tmtctr 3\n"
824 "\tbctr\n"
825 ".globl .StgReturn\n"
826 ".type .StgReturn,@function\n"
827 ".StgReturn:\n"
828 "\tmr 3,14\n"
829 "\tla 5, %0(1)\n" // load address == addi r5, r1, %0
830 "\tld 2, -296(5)\n"
831 "\tld 14, -288(5)\n"
832 "\tld 15, -280(5)\n"
833 "\tld 16, -272(5)\n"
834 "\tld 17, -264(5)\n"
835 "\tld 18, -256(5)\n"
836 "\tld 19, -248(5)\n"
837 "\tld 20, -240(5)\n"
838 "\tld 21, -232(5)\n"
839 "\tld 22, -224(5)\n"
840 "\tld 23, -216(5)\n"
841 "\tld 24, -208(5)\n"
842 "\tld 25, -200(5)\n"
843 "\tld 26, -192(5)\n"
844 "\tld 27, -184(5)\n"
845 "\tld 28, -176(5)\n"
846 "\tld 29, -168(5)\n"
847 "\tld 30, -160(5)\n"
848 "\tld 31, -152(5)\n"
849 "\tlfd 14, -144(5)\n"
850 "\tlfd 15, -136(5)\n"
851 "\tlfd 16, -128(5)\n"
852 "\tlfd 17, -120(5)\n"
853 "\tlfd 18, -112(5)\n"
854 "\tlfd 19, -104(5)\n"
855 "\tlfd 20, -96(5)\n"
856 "\tlfd 21, -88(5)\n"
857 "\tlfd 22, -80(5)\n"
858 "\tlfd 23, -72(5)\n"
859 "\tlfd 24, -64(5)\n"
860 "\tlfd 25, -56(5)\n"
861 "\tlfd 26, -48(5)\n"
862 "\tlfd 27, -40(5)\n"
863 "\tlfd 28, -32(5)\n"
864 "\tlfd 29, -24(5)\n"
865 "\tlfd 30, -16(5)\n"
866 "\tlfd 31, -8(5)\n"
867 "\tmr 1, 5\n"
868 "\tld 0, 16(1)\n"
869 "\tmtlr 0\n"
870 "\tblr\n"
871 : : "i"(RESERVED_C_STACK_BYTES+304 /*stack frame size*/));
872 }
873
874 #else // linux_HOST_OS
875 #error Only Linux support for power64 right now.
876 #endif
877
878 #endif
879
880 #if defined(powerpc64le_HOST_ARCH)
881 /* -----------------------------------------------------------------------------
882 PowerPC 64 little endian architecture
883
884 Really everything is in assembler, so we don't have to deal with GCC...
885 -------------------------------------------------------------------------- */
886 #endif
887
888 /* -----------------------------------------------------------------------------
889 ARM architecture
890 -------------------------------------------------------------------------- */
891
892 #if defined(arm_HOST_ARCH)
893
894 #if defined(__thumb__)
895 #define THUMB_FUNC ".thumb\n\t.thumb_func\n\t"
896 #else
897 #define THUMB_FUNC
898 #endif
899
900 StgRegTable *
901 StgRun(StgFunPtr f, StgRegTable *basereg) {
902 StgRegTable * r;
903 __asm__ volatile (
904 /*
905 * save callee-saves registers on behalf of the STG code.
906 */
907 "stmfd sp!, {r4-r11, ip, lr}\n\t"
908 #if !defined(arm_HOST_ARCH_PRE_ARMv6)
909 "vstmdb sp!, {d8-d11}\n\t"
910 #endif
911 /*
912 * allocate some space for Stg machine's temporary storage.
913 * Note: RESERVED_C_STACK_BYTES has to be a round number here or
914 * the assembler can't assemble it.
915 */
916 "sub sp, sp, %3\n\t"
917 /*
918 * Set BaseReg
919 */
920 "mov r4, %2\n\t"
921 /*
922 * Jump to function argument.
923 */
924 "bx %1\n\t"
925
926 ".globl " STG_RETURN "\n\t"
927 THUMB_FUNC
928 #if !defined(ios_HOST_OS)
929 ".type " STG_RETURN ", %%function\n"
930 #endif
931 STG_RETURN ":\n\t"
932 /*
933 * Free the space we allocated
934 */
935 "add sp, sp, %3\n\t"
936 /*
937 * Return the new register table, taking it from Stg's R1 (ARM's R7).
938 */
939 "mov %0, r7\n\t"
940 /*
941 * restore callee-saves registers.
942 */
943 #if !defined(arm_HOST_ARCH_PRE_ARMv6)
944 "vldmia sp!, {d8-d11}\n\t"
945 #endif
946 "ldmfd sp!, {r4-r11, ip, lr}\n\t"
947 : "=r" (r)
948 : "r" (f), "r" (basereg), "i" (RESERVED_C_STACK_BYTES)
949 #if !defined(__thumb__)
950 /* In ARM mode, r11/fp is frame-pointer and so we cannot mark
951 it as clobbered. If we do so, GCC complains with error. */
952 : "%r4", "%r5", "%r6", "%r7", "%r8", "%r9", "%r10", "%ip", "%lr"
953 #else
954 /* In Thumb mode r7 is frame-pointer and so we cannot mark it
955 as clobbered. On the other hand we mark as clobbered also
956 those regs not used in Thumb mode. Hard to judge if this is
957 needed, but certainly Haskell code is using them for
958 placing GHC's virtual registers there. See
959 includes/stg/MachRegs.h Please note that Haskell code is
960 compiled by GHC/LLVM into ARM code (not Thumb!), at least
961 as of February 2012 */
962 : "%r4", "%r5", "%r6", "%r8", "%r9", "%r10", "%11", "%ip", "%lr"
963 #endif
964 );
965 return r;
966 }
967 #endif
968
969 #if defined(aarch64_HOST_ARCH)
970
971 StgRegTable *
972 StgRun(StgFunPtr f, StgRegTable *basereg) {
973 StgRegTable * r;
974 __asm__ volatile (
975 /*
976 * Save callee-saves registers on behalf of the STG code.
977 * Floating point registers only need the bottom 64 bits preserved.
978 * We need to use the names x16, x17, x29 and x30 instead of ip0
979 * ip1, fp and lp because one of either clang or gcc doesn't understand
980 * the later names.
981 */
982 "stp x29, x30, [sp, #-16]!\n\t"
983 "mov x29, sp\n\t"
984 "stp x16, x17, [sp, #-16]!\n\t"
985 "stp x19, x20, [sp, #-16]!\n\t"
986 "stp x21, x22, [sp, #-16]!\n\t"
987 "stp x23, x24, [sp, #-16]!\n\t"
988 "stp x25, x26, [sp, #-16]!\n\t"
989 "stp x27, x28, [sp, #-16]!\n\t"
990 "stp d8, d9, [sp, #-16]!\n\t"
991 "stp d10, d11, [sp, #-16]!\n\t"
992 "stp d12, d13, [sp, #-16]!\n\t"
993 "stp d14, d15, [sp, #-16]!\n\t"
994
995 /*
996 * allocate some space for Stg machine's temporary storage.
997 * Note: RESERVED_C_STACK_BYTES has to be a round number here or
998 * the assembler can't assemble it.
999 */
1000 "sub sp, sp, %3\n\t"
1001 /*
1002 * Set BaseReg
1003 */
1004 "mov x19, %2\n\t"
1005 /*
1006 * Jump to function argument.
1007 */
1008 "br %1\n\t"
1009
1010 ".globl " STG_RETURN "\n\t"
1011 #if !defined(ios_HOST_OS)
1012 ".type " STG_RETURN ", %%function\n"
1013 #endif
1014 STG_RETURN ":\n\t"
1015 /*
1016 * Free the space we allocated
1017 */
1018 "add sp, sp, %3\n\t"
1019 /*
1020 * Return the new register table, taking it from Stg's R1 (ARM64's R22).
1021 */
1022 "mov %0, x22\n\t"
1023 /*
1024 * restore callee-saves registers.
1025 */
1026
1027 "ldp d14, d15, [sp], #16\n\t"
1028 "ldp d12, d13, [sp], #16\n\t"
1029 "ldp d10, d11, [sp], #16\n\t"
1030 "ldp d8, d9, [sp], #16\n\t"
1031 "ldp x27, x28, [sp], #16\n\t"
1032 "ldp x25, x26, [sp], #16\n\t"
1033 "ldp x23, x24, [sp], #16\n\t"
1034 "ldp x21, x22, [sp], #16\n\t"
1035 "ldp x19, x20, [sp], #16\n\t"
1036 "ldp x16, x17, [sp], #16\n\t"
1037 "ldp x29, x30, [sp], #16\n\t"
1038
1039 : "=r" (r)
1040 : "r" (f), "r" (basereg), "i" (RESERVED_C_STACK_BYTES)
1041 : "%x19", "%x20", "%x21", "%x22", "%x23", "%x24", "%x25", "%x26", "%x27", "%x28",
1042 "%x16", "%x17", "%x30"
1043 );
1044 return r;
1045 }
1046
1047 #endif
1048
1049 #endif /* !USE_MINIINTERPRETER */