1214152Sed/* ===----- trampoline_setup.c - Implement __trampoline_setup -------------===
2214152Sed *
3214152Sed *                     The LLVM Compiler Infrastructure
4214152Sed *
5222656Sed * This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed * Source Licenses. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed */
10214152Sed
11214152Sed#include "int_lib.h"
12214152Sed
13214152Sedextern void __clear_cache(void* start, void* end);
14214152Sed
15214152Sed/*
16214152Sed * The ppc compiler generates calls to __trampoline_setup() when creating
17214152Sed * trampoline functions on the stack for use with nested functions.
18214152Sed * This function creates a custom 40-byte trampoline function on the stack
19214152Sed * which loads r11 with a pointer to the outer function's locals
20214152Sed * and then jumps to the target nested function.
21214152Sed */
22214152Sed
23215125Sed#if __ppc__ && !defined(__powerpc64__)
24214152Sedvoid __trampoline_setup(uint32_t* trampOnStack, int trampSizeAllocated,
25214152Sed                                const void* realFunc, void* localsPtr)
26214152Sed{
27214152Sed    /* should never happen, but if compiler did not allocate */
28214152Sed    /* enough space on stack for the trampoline, abort */
29214152Sed    if ( trampSizeAllocated < 40 )
30214152Sed        compilerrt_abort();
31214152Sed
32214152Sed    /* create trampoline */
33214152Sed    trampOnStack[0] = 0x7c0802a6;    /* mflr r0 */
34214152Sed    trampOnStack[1] = 0x4800000d;    /* bl Lbase */
35214152Sed    trampOnStack[2] = (uint32_t)realFunc;
36214152Sed    trampOnStack[3] = (uint32_t)localsPtr;
37214152Sed    trampOnStack[4] = 0x7d6802a6;    /* Lbase: mflr r11 */
38214152Sed    trampOnStack[5] = 0x818b0000;    /* lwz    r12,0(r11) */
39214152Sed    trampOnStack[6] = 0x7c0803a6;    /* mtlr r0 */
40214152Sed    trampOnStack[7] = 0x7d8903a6;    /* mtctr r12 */
41214152Sed    trampOnStack[8] = 0x816b0004;    /* lwz    r11,4(r11) */
42214152Sed    trampOnStack[9] = 0x4e800420;    /* bctr */
43214152Sed
44214152Sed    /* clear instruction cache */
45214152Sed    __clear_cache(trampOnStack, &trampOnStack[10]);
46214152Sed}
47229413Sed#endif /* __ppc__ && !defined(__powerpc64__) */
48