Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 1 | /* ===----- trampoline_setup.c - Implement __trampoline_setup -------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
| 5 | * This file is distributed under the University of Illinois Open Source |
| 6 | * License. See LICENSE.TXT for details. |
| 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 10 | |
| 11 | #include <stdint.h> |
| 12 | #include <stdlib.h> |
| 13 | |
Nick Kledzik | d0d9913 | 2010-04-15 20:37:56 +0000 | [diff] [blame^] | 14 | #include "int_lib.h" |
| 15 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 16 | extern void __clear_cache(void* start, void* end); |
| 17 | |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 18 | /* |
| 19 | * The ppc compiler generates calls to __trampoline_setup() when creating |
| 20 | * trampoline functions on the stack for use with nested functions. |
| 21 | * This function creates a custom 40-byte trampoline function on the stack |
| 22 | * which loads r11 with a pointer to the outer function's locals |
| 23 | * and then jumps to the target nested function. |
| 24 | */ |
| 25 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 26 | #if __ppc__ |
| 27 | void __trampoline_setup(uint32_t* trampOnStack, int trampSizeAllocated, |
| 28 | const void* realFunc, void* localsPtr) |
| 29 | { |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 30 | /* should never happen, but if compiler did not allocate */ |
| 31 | /* enough space on stack for the trampoline, abort */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 32 | if ( trampSizeAllocated < 40 ) |
Daniel Dunbar | 48f46ac | 2010-03-31 17:00:45 +0000 | [diff] [blame] | 33 | compilerrt_abort(); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 34 | |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 35 | /* create trampoline */ |
| 36 | trampOnStack[0] = 0x7c0802a6; /* mflr r0 */ |
| 37 | trampOnStack[1] = 0x4800000d; /* bl Lbase */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 38 | trampOnStack[2] = (uint32_t)realFunc; |
| 39 | trampOnStack[3] = (uint32_t)localsPtr; |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 40 | trampOnStack[4] = 0x7d6802a6; /* Lbase: mflr r11 */ |
| 41 | trampOnStack[5] = 0x818b0000; /* lwz r12,0(r11) */ |
| 42 | trampOnStack[6] = 0x7c0803a6; /* mtlr r0 */ |
| 43 | trampOnStack[7] = 0x7d8903a6; /* mtctr r12 */ |
| 44 | trampOnStack[8] = 0x816b0004; /* lwz r11,4(r11) */ |
| 45 | trampOnStack[9] = 0x4e800420; /* bctr */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 46 | |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 47 | /* clear instruction cache */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 48 | __clear_cache(trampOnStack, &trampOnStack[10]); |
| 49 | } |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 50 | #endif /* __ppc__ */ |