Debian Codegraph @

rpython/translator/c/src/stacklet/switch_ppc64_gcc.h

raw symbols SCIP
1 #if !(defined(__LITTLE_ENDIAN__) ^ defined(__BIG_ENDIAN__))
2 # error "cannot determine if it is ppc64 or ppc64le"
3 #endif
4
5 #ifdef __BIG_ENDIAN__
6 # define TOC_AREA "40"
7 #else
8 # define TOC_AREA "24"
9 #endif
10
11
12 /* This depends on these attributes so that gcc generates a function
13 with no code before the asm, and only "blr" after. */
14 static __attribute__((noinline, optimize("O2")))
15 void *slp_switch(void *(*save_state)(void*, void*),
16 void *(*restore_state)(void*, void*),
17 void *extra)
18 {
19 void *result;
20 __asm__ volatile (
21 /* By Vaibhav Sood & Armin Rigo, with some copying from
22 the Stackless version by Kristjan Valur Jonsson */
23
24 /* Save all 18 volatile GP registers, 18 volatile FP regs, and 12
25 volatile vector regs. We need a stack frame of 144 bytes for FPR,
26 144 bytes for GPR, 192 bytes for VR plus 48 bytes for the standard
27 stackframe = 528 bytes (a multiple of 16). */
28
29 "mflr 0\n" /* Save LR into 16(r1) */
30 "std 0, 16(1)\n"
31
32 "std 14,-288(1)\n" /* the GPR save area is between -288(r1) */
33 "std 15,-280(1)\n" /* included and -144(r1) excluded */
34 "std 16,-272(1)\n"
35 "std 17,-264(1)\n"
36 "std 18,-256(1)\n"
37 "std 19,-248(1)\n"
38 "std 20,-240(1)\n"
39 "std 21,-232(1)\n"
40 "std 22,-224(1)\n"
41 "std 23,-216(1)\n"
42 "std 24,-208(1)\n"
43 "std 25,-200(1)\n"
44 "std 26,-192(1)\n"
45 "std 27,-184(1)\n"
46 "std 28,-176(1)\n"
47 "std 29,-168(1)\n"
48 "std 30,-160(1)\n"
49 "std 31,-152(1)\n"
50
51 "stfd 14,-144(1)\n" /* the FPR save area is between -144(r1) */
52 "stfd 15,-136(1)\n" /* included and 0(r1) excluded */
53 "stfd 16,-128(1)\n"
54 "stfd 17,-120(1)\n"
55 "stfd 18,-112(1)\n"
56 "stfd 19,-104(1)\n"
57 "stfd 20,-96(1)\n"
58 "stfd 21,-88(1)\n"
59 "stfd 22,-80(1)\n"
60 "stfd 23,-72(1)\n"
61 "stfd 24,-64(1)\n"
62 "stfd 25,-56(1)\n"
63 "stfd 26,-48(1)\n"
64 "stfd 27,-40(1)\n"
65 "stfd 28,-32(1)\n"
66 "stfd 29,-24(1)\n"
67 "stfd 30,-16(1)\n"
68 "stfd 31,-8(1)\n"
69
70 "li 12,-480\n" /* the VR save area is between -480(r1) */
71 "stvx 20,12,1\n" /* included and -288(r1) excluded */
72 "li 12,-464\n"
73 "stvx 21,12,1\n"
74 "li 12,-448\n"
75 "stvx 22,12,1\n"
76 "li 12,-432\n"
77 "stvx 23,12,1\n"
78 "li 12,-416\n"
79 "stvx 24,12,1\n"
80 "li 12,-400\n"
81 "stvx 25,12,1\n"
82 "li 12,-384\n"
83 "stvx 26,12,1\n"
84 "li 12,-368\n"
85 "stvx 27,12,1\n"
86 "li 12,-352\n"
87 "stvx 28,12,1\n"
88 "li 12,-336\n"
89 "stvx 29,12,1\n"
90 "li 12,-320\n"
91 "stvx 30,12,1\n"
92 "li 12,-304\n"
93 "stvx 31,12,1\n"
94
95 "stdu 1,-528(1)\n" /* Create stack frame */
96
97 "std 2, "TOC_AREA"(1)\n" /* Save TOC in the "TOC save area"*/
98 "mfcr 12\n" /* Save CR in the "CR save area" */
99 "std 12, 8(1)\n"
100
101 "mr 14, %[restore_state]\n" /* save 'restore_state' for later */
102 "mr 15, %[extra]\n" /* save 'extra' for later */
103 "mr 12, %[save_state]\n" /* move 'save_state' into r12 for branching */
104 "mr 3, 1\n" /* arg 1: current (old) stack pointer */
105 "mr 4, 15\n" /* arg 2: extra */
106
107 "stdu 1, -48(1)\n" /* create temp stack space (see below) */
108 #ifdef __BIG_ENDIAN__
109 "ld 0, 0(12)\n"
110 "ld 11, 16(12)\n"
111 "mtctr 0\n"
112 "ld 2, 8(12)\n"
113 #else
114 "mtctr 12\n" /* r12 is fixed by this ABI */
115 #endif
116 "bctrl\n" /* call save_state() */
117 "addi 1, 1, 48\n" /* destroy temp stack space */
118
119 "cmpdi 3, 0\n" /* skip the rest if the return value is null */
120 "bt eq, zero\n"
121
122 "mr 1, 3\n" /* change the stack pointer */
123 /* From now on, the stack pointer is modified, but the content of the
124 stack is not restored yet. It contains only garbage here. */
125
126 "mr 4, 15\n" /* arg 2: extra */
127 /* arg 1: current (new) stack pointer
128 is already in r3 */
129
130 "stdu 1, -48(1)\n" /* create temp stack space for callee to use */
131 /* ^^^ we have to be careful. The function call will store the link
132 register in the current frame (as the ABI) dictates. But it will
133 then trample it with the restore! We fix this by creating a fake
134 stack frame */
135
136 #ifdef __BIG_ENDIAN__
137 "ld 0, 0(14)\n" /* 'restore_state' is in r14 */
138 "ld 11, 16(14)\n"
139 "mtctr 0\n"
140 "ld 2, 8(14)\n"
141 #endif
142 #ifdef __LITTLE_ENDIAN__
143 "mr 12, 14\n" /* copy 'restore_state' */
144 "mtctr 12\n" /* r12 is fixed by this ABI */
145 #endif
146
147 "bctrl\n" /* call restore_state() */
148 "addi 1, 1, 48\n" /* destroy temp stack space */
149
150 /* The stack's content is now restored. */
151
152 "zero:\n"
153
154 /* Epilogue */
155
156 "ld 2, "TOC_AREA"(1)\n" /* restore the TOC */
157 "ld 12,8(1)\n" /* restore the condition register */
158 "mtcrf 0xff, 12\n"
159
160 "addi 1,1,528\n" /* restore stack pointer */
161
162 "li 12,-480\n" /* restore vector registers */
163 "lvx 20,12,1\n"
164 "li 12,-464\n"
165 "lvx 21,12,1\n"
166 "li 12,-448\n"
167 "lvx 22,12,1\n"
168 "li 12,-432\n"
169 "lvx 23,12,1\n"
170 "li 12,-416\n"
171 "lvx 24,12,1\n"
172 "li 12,-400\n"
173 "lvx 25,12,1\n"
174 "li 12,-384\n"
175 "lvx 26,12,1\n"
176 "li 12,-368\n"
177 "lvx 27,12,1\n"
178 "li 12,-352\n"
179 "lvx 28,12,1\n"
180 "li 12,-336\n"
181 "lvx 29,12,1\n"
182 "li 12,-320\n"
183 "lvx 30,12,1\n"
184 "li 12,-304\n"
185 "lvx 31,12,1\n"
186
187 "ld 14,-288(1)\n" /* restore general purporse registers */
188 "ld 15,-280(1)\n"
189 "ld 16,-272(1)\n"
190 "ld 17,-264(1)\n"
191 "ld 18,-256(1)\n"
192 "ld 19,-248(1)\n"
193 "ld 20,-240(1)\n"
194 "ld 21,-232(1)\n"
195 "ld 22,-224(1)\n"
196 "ld 23,-216(1)\n"
197 "ld 24,-208(1)\n"
198 "ld 25,-200(1)\n"
199 "ld 26,-192(1)\n"
200 "ld 27,-184(1)\n"
201 "ld 28,-176(1)\n"
202 "ld 29,-168(1)\n"
203 "ld 30,-160(1)\n"
204 "ld 31,-152(1)\n"
205
206 "lfd 14,-144(1)\n" /* restore floating point registers */
207 "lfd 15,-136(1)\n"
208 "lfd 16,-128(1)\n"
209 "lfd 17,-120(1)\n"
210 "lfd 18,-112(1)\n"
211 "lfd 19,-104(1)\n"
212 "lfd 20,-96(1)\n"
213 "lfd 21,-88(1)\n"
214 "lfd 22,-80(1)\n"
215 "lfd 23,-72(1)\n"
216 "lfd 24,-64(1)\n"
217 "lfd 25,-56(1)\n"
218 "lfd 26,-48(1)\n"
219 "lfd 27,-40(1)\n"
220 "lfd 28,-32(1)\n"
221 "ld 0, 16(1)\n"
222 "lfd 29,-24(1)\n"
223 "mtlr 0\n"
224 "lfd 30,-16(1)\n"
225 "lfd 31,-8(1)\n"
226
227 : "=r"(result) /* output variable: expected to be r3 */
228 : [restore_state]"r"(restore_state), /* input variables */
229 [save_state]"r"(save_state),
230 [extra]"r"(extra)
231 );
232 return result;
233 }