1#if defined(__ppc__)
2
3/* -----------------------------------------------------------------------
4   darwin_closure.S - Copyright (c) 2002, 2003, 2004, Free Software Foundation,
5   Inc. based on ppc_closure.S
6
7   PowerPC Assembly glue.
8
9   Permission is hereby granted, free of charge, to any person obtaining
10   a copy of this software and associated documentation files (the
11   ``Software''), to deal in the Software without restriction, including
12   without limitation the rights to use, copy, modify, merge, publish,
13   distribute, sublicense, and/or sell copies of the Software, and to
14   permit persons to whom the Software is furnished to do so, subject to
15   the following conditions:
16
17   The above copyright notice and this permission notice shall be included
18   in all copies or substantial portions of the Software.
19
20   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
21   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
24   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26   OTHER DEALINGS IN THE SOFTWARE.
27   ----------------------------------------------------------------------- */
28
29#define LIBFFI_ASM
30
31#include <ffi.h>
32#include <ppc-ffitarget.h>	// for FFI_TRAMPOLINE_SIZE
33#include <ppc-darwin.h>
34#include <architecture/ppc/mode_independent_asm.h>
35
36	.file	"ppc-darwin_closure.S"
37.text
38	.align	LOG2_GPR_BYTES
39	.globl	_ffi_closure_ASM
40
41.text
42	.align	LOG2_GPR_BYTES
43
44_ffi_closure_ASM:
45LFB1:
46	mflr	r0							/* extract return address  */
47	stg		r0,MODE_CHOICE(8,16)(r1)	/* save return address  */
48
49LCFI0:
50	/* 24/48 bytes (Linkage Area)
51	   32/64 bytes (outgoing parameter area, always reserved)
52	   104 bytes (13*8 from FPR)
53	   16/32 bytes (result)
54	   176/232 total bytes  */
55
56	/*	skip over caller save area and keep stack aligned to 16/32.	*/
57	stgu	r1,-SF_ROUND(MODE_CHOICE(176,248))(r1)
58
59LCFI1:
60	/* We want to build up an area for the parameters passed
61	   in registers. (both floating point and integer)  */
62
63	/*	176/256 bytes (callee stack frame aligned to 16/32)
64		24/48 bytes (caller linkage area)
65		200/304 (start of caller parameter area aligned to 4/8)
66	*/
67
68	/* Save GPRs 3 - 10 (aligned to 4/8)
69	   in the parents outgoing area.  */
70	stg		r3,MODE_CHOICE(200,304)(r1)
71	stg		r4,MODE_CHOICE(204,312)(r1)
72	stg		r5,MODE_CHOICE(208,320)(r1)
73	stg		r6,MODE_CHOICE(212,328)(r1)
74	stg		r7,MODE_CHOICE(216,336)(r1)
75	stg		r8,MODE_CHOICE(220,344)(r1)
76	stg		r9,MODE_CHOICE(224,352)(r1)
77	stg		r10,MODE_CHOICE(228,360)(r1)
78
79	/* Save FPRs 1 - 13. (aligned to 8)  */
80	stfd	f1,MODE_CHOICE(56,112)(r1)
81	stfd	f2,MODE_CHOICE(64,120)(r1)
82	stfd	f3,MODE_CHOICE(72,128)(r1)
83	stfd	f4,MODE_CHOICE(80,136)(r1)
84	stfd	f5,MODE_CHOICE(88,144)(r1)
85	stfd	f6,MODE_CHOICE(96,152)(r1)
86	stfd	f7,MODE_CHOICE(104,160)(r1)
87	stfd	f8,MODE_CHOICE(112,168)(r1)
88	stfd	f9,MODE_CHOICE(120,176)(r1)
89	stfd	f10,MODE_CHOICE(128,184)(r1)
90	stfd	f11,MODE_CHOICE(136,192)(r1)
91	stfd	f12,MODE_CHOICE(144,200)(r1)
92	stfd	f13,MODE_CHOICE(152,208)(r1)
93
94	/* Set up registers for the routine that actually does the work.
95	   Get the context pointer from the trampoline.  */
96	mr		r3,r11
97
98	/* Load the pointer to the result storage.  */
99	/* current stack frame size - ((4/8 * 4) + saved registers)	*/
100	addi	r4,r1,MODE_CHOICE(160,216)
101
102	/* Load the pointer to the saved gpr registers.  */
103	addi	r5,r1,MODE_CHOICE(200,304)
104
105	/* Load the pointer to the saved fpr registers.  */
106	addi	r6,r1,MODE_CHOICE(56,112)
107
108	/* Make the call.  */
109	bl		Lffi_closure_helper_DARWIN$stub
110
111	/* Now r3 contains the return type
112	   so use it to look up in a table
113	   so we know how to deal with each type.  */
114
115	/* Look the proper starting point in table
116	   by using return type as offset.  */
117	addi	r5,r1,MODE_CHOICE(160,216)	// Get pointer to results area.
118	bl		Lget_ret_type0_addr			// Get pointer to Lret_type0 into LR.
119	mflr	r4							// Move to r4.
120	slwi	r3,r3,4						// Now multiply return type by 16.
121	add		r3,r3,r4					// Add contents of table to table address.
122	mtctr	r3
123	bctr
124
125LFE1:
126/* Each of the ret_typeX code fragments has to be exactly 16 bytes long
127   (4 instructions). For cache effectiveness we align to a 16 byte boundary
128   first.  */
129	.align 4
130	nop
131	nop
132	nop
133
134Lget_ret_type0_addr:
135	blrl
136
137/* case FFI_TYPE_VOID  */
138Lret_type0:
139	b		Lfinish
140	nop
141	nop
142	nop
143
144/* case FFI_TYPE_INT  */
145Lret_type1:
146	lwz		r3,MODE_CHOICE(0,4)(r5)
147	b		Lfinish
148	nop
149	nop
150
151/* case FFI_TYPE_FLOAT  */
152Lret_type2:
153	lfs		f1,0(r5)
154	b		Lfinish
155	nop
156	nop
157
158/* case FFI_TYPE_DOUBLE  */
159Lret_type3:
160	lfd		f1,0(r5)
161	b		Lfinish
162	nop
163	nop
164
165/* case FFI_TYPE_LONGDOUBLE  */
166Lret_type4:
167	lfd		f1,0(r5)
168	lfd		f2,8(r5)
169	b		Lfinish
170	nop
171
172/* case FFI_TYPE_UINT8  */
173Lret_type5:
174	lbz		r3,MODE_CHOICE(3,7)(r5)
175	b		Lfinish
176	nop
177	nop
178
179/* case FFI_TYPE_SINT8  */
180Lret_type6:
181	lbz		r3,MODE_CHOICE(3,7)(r5)
182	extsb	r3,r3
183	b		Lfinish
184	nop
185
186/* case FFI_TYPE_UINT16  */
187Lret_type7:
188	lhz		r3,MODE_CHOICE(2,6)(r5)
189	b		Lfinish
190	nop
191	nop
192
193/* case FFI_TYPE_SINT16  */
194Lret_type8:
195	lha		r3,MODE_CHOICE(2,6)(r5)
196	b		Lfinish
197	nop
198	nop
199
200/* case FFI_TYPE_UINT32  */
201Lret_type9:		// same as Lret_type1
202	lwz		r3,MODE_CHOICE(0,4)(r5)
203	b		Lfinish
204	nop
205	nop
206
207/* case FFI_TYPE_SINT32  */
208Lret_type10:	// same as Lret_type1
209	lwz		r3,MODE_CHOICE(0,4)(r5)
210	b		Lfinish
211	nop
212	nop
213
214/* case FFI_TYPE_UINT64  */
215Lret_type11:
216	lwz		r3,0(r5)
217	lwz		r4,4(r5)
218	b		Lfinish
219	nop
220
221/* case FFI_TYPE_SINT64  */
222Lret_type12:	// same as Lret_type11
223	lwz		r3,0(r5)
224	lwz		r4,4(r5)
225	b		Lfinish
226	nop
227
228/* case FFI_TYPE_STRUCT  */
229Lret_type13:
230	b		MODE_CHOICE(Lfinish,Lret_struct)
231	nop
232	nop
233	nop
234
235/* End 16-byte aligned cases */
236/* case FFI_TYPE_POINTER  */
237// This case assumes that FFI_TYPE_POINTER == FFI_TYPE_LAST. If more types
238// are added in future, the following code will need to be updated and
239// padded to 16 bytes.
240Lret_type14:
241	lg		r3,0(r5)
242
243/* case done  */
244Lfinish:
245	addi	r1,r1,SF_ROUND(MODE_CHOICE(176,248))	// Restore stack pointer.
246	lg		r0,MODE_CHOICE(8,16)(r1)	/* Get return address.  */
247	mtlr	r0							/* Reset link register.  */
248	blr
249
250/* END(ffi_closure_ASM)  */
251
252.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
253EH_frame1:
254	.set	L$set$0,LECIE1-LSCIE1
255	.long	L$set$0		; Length of Common Information Entry
256LSCIE1:
257	.long	0x0			; CIE Identifier Tag
258	.byte	0x1			; CIE Version
259	.ascii	"zR\0"		; CIE Augmentation
260	.byte	0x1			; uleb128 0x1; CIE Code Alignment Factor
261	.byte	0x7c		; sleb128 -4; CIE Data Alignment Factor
262	.byte	0x41		; CIE RA Column
263	.byte	0x1			; uleb128 0x1; Augmentation size
264	.byte	0x90		; FDE Encoding (indirect pcrel)
265	.byte	0xc			; DW_CFA_def_cfa
266	.byte	0x1			; uleb128 0x1
267	.byte	0x0			; uleb128 0x0
268	.align	LOG2_GPR_BYTES
269LECIE1:
270.globl _ffi_closure_ASM.eh
271_ffi_closure_ASM.eh:
272LSFDE1:
273	.set	L$set$1,LEFDE1-LASFDE1
274	.long	L$set$1		; FDE Length
275
276LASFDE1:
277	.long	LASFDE1-EH_frame1		; FDE CIE offset
278	.g_long	LLFB1$non_lazy_ptr-.	; FDE initial location
279	.set	L$set$3,LFE1-LFB1
280	.g_long	L$set$3					; FDE address range
281	.byte   0x0						; uleb128 0x0; Augmentation size
282	.byte	0x4						; DW_CFA_advance_loc4
283	.set	L$set$3,LCFI1-LCFI0
284	.long	L$set$3
285	.byte	0xe						; DW_CFA_def_cfa_offset
286	.byte	176,1					; uleb128 176
287	.byte	0x4						; DW_CFA_advance_loc4
288	.set	L$set$4,LCFI0-LFB1
289	.long	L$set$4
290	.byte   0x11					; DW_CFA_offset_extended_sf
291	.byte	0x41					; uleb128 0x41
292	.byte   0x7e					; sleb128 -2
293	.align	LOG2_GPR_BYTES
294
295LEFDE1:
296.data
297	.align	LOG2_GPR_BYTES
298LDFCM0:
299.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
300	.align	LOG2_GPR_BYTES
301
302Lffi_closure_helper_DARWIN$stub:
303	.indirect_symbol _ffi_closure_helper_DARWIN
304	mflr	r0
305	bcl		20,31,LO$ffi_closure_helper_DARWIN
306
307LO$ffi_closure_helper_DARWIN:
308	mflr	r11
309	addis	r11,r11,ha16(L_ffi_closure_helper_DARWIN$lazy_ptr - LO$ffi_closure_helper_DARWIN)
310	mtlr	r0
311	lgu		r12,lo16(L_ffi_closure_helper_DARWIN$lazy_ptr - LO$ffi_closure_helper_DARWIN)(r11)
312	mtctr	r12
313	bctr
314
315.lazy_symbol_pointer
316L_ffi_closure_helper_DARWIN$lazy_ptr:
317	.indirect_symbol _ffi_closure_helper_DARWIN
318	.g_long dyld_stub_binding_helper
319
320.data
321	.align LOG2_GPR_BYTES
322LLFB1$non_lazy_ptr:
323	.g_long LFB1
324
325#endif // __ppc__
326