1/* ppc-sysv-asm.S -- PowerPC FFI trampoline for Mach-O			-*- asm -*-
2 *
3 * Author: Ian.Piumarta@INRIA.Fr
4 *
5 * Last edited:	2006-10-18 10:08:10 by piumarta on emilia.local
6 *
7 *   Copyright (C) 1996-2004 by Ian Piumarta and other authors/contributors
8 *                              listed elsewhere in this file.
9 *   All rights reserved.
10 *
11 *   This file is part of Unix Squeak.
12 *
13 *   Permission is hereby granted, free of charge, to any person obtaining a copy
14 *   of this software and associated documentation files (the "Software"), to deal
15 *   in the Software without restriction, including without limitation the rights
16 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 *   copies of the Software, and to permit persons to whom the Software is
18 *   furnished to do so, subject to the following conditions:
19 *
20 *   The above copyright notice and this permission notice shall be included in
21 *   all copies or substantial portions of the Software.
22 *
23 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 *   SOFTWARE.
30 */
31
32/* Mach-O PPC stack frames look like this (higher addresses first):
33 *
34 * 		| caller's lr		|
35 * 		| caller's cr		|
36 * caller's sp->| caller's caller's sp	|
37 *		| fpr save area		|
38 *		| gpr save area		|
39 *		|     [alignment pad]	|
40 *		| local variables	|
41 * sp + 24  ->	| param save area	|
42 * sp + 20  ->	| caller's toc		|
43 * sp + 16  ->	| reserved		|
44 * sp + 12  ->	| reserved		|
45 * sp +  8  ->	| (callee-save) lr	|
46 * sp +  4  ->	| (callee-save) cr	|
47 * sp +  0  ->	| caller's sp		|
48 */
49
50#define GPR_MAX   8
51#define FPR_MAX  13
52#define ARG_MAX 512
53#define FRAMESZ	 32
54
55#include "ppc-global.h"
56
57#define sp r1
58
59	.text
60	.globl	_ffiCallAddressOf
61
62_ffiCallAddressOf:
63	stwu	sp, -FRAMESZ(sp)		// push trampoline frame
64	mflr	r0
65	stw	r0, (FRAMESZ+8)(sp)
66        mfcr	r0
67        stw	r0, (FRAMESZ+4)(sp)		// saved ccr
68	mtlr	r3				// destination fn address
69	stw	r4, (FRAMESZ-4)(sp)		// globals
70	lwz	r5, stackIndex(r4)
71	slwi	r10, r5, 2			// param save area size
72	addi	r10, r10, 32+15			// round to quad word
73	rlwinm	r10, r10, 0,0,27
74	neg	r10, r10
75	stwux	sp, sp, r10			// push ffi caller frame
76	cmpwi	r5, 0				// have params?
77	beq+	2f
78	mtctr	r5				// words to move
79	la	r10, (stack-4)(r4)		// ffi param stack - 4
80	addi	r11, sp, 24-4			// param save area - 4
811:	lwzu	r0, 4(r10)			// copy param save area
82	stwu	r0, 4(r11)
83	bdnz	1b
842:	lwz	r5, fprCount(r4)
85	cmpwi	r5, 0
86	beq+	4f				// no fp args
87	la	r11, fprs(r4)
88	cmpwi	r5, 4
89	ble+	3f
90	lfd	f5,  32(r11)
91	lfd	f6,  40(r11)
92	lfd	f7,  48(r11)
93	lfd	f8,  56(r11)
94#	if (FPR_MAX > 8)
95	lfd	f9,  64(r11)
96	lfd	f10, 72(r11)
97	lfd	f11, 80(r11)
98	lfd	f12, 88(r11)
99	lfd	f13, 96(r11)
100#	endif
1013:	lfd	f1,   0(r11)
102	lfd	f2,   8(r11)
103	lfd	f3,  16(r11)
104	lfd	f4,  24(r11)
1054:	lwz	r5, gprCount(r4)
106	cmpwi	r5, 0
107	beq-	6f				// no int args
108	la	r11, gprs(r4)
109	cmpwi	r5, 4
110	ble+	5f
111	lwz	r7,  16(r11)
112	lwz	r8,  20(r11)
113	lwz	r9,  24(r11)
114	lwz	r10, 28(r11)
1155:	lwz	r3,   0(r11)
116	lwz	r4,   4(r11)
117	lwz	r5,   8(r11)
118	lwz	r6,  12(r11)
1196:	blrl					// callout
120	lwz	sp, 0(sp)			// pop ffi caller frame
121	lwz	r5, (FRAMESZ-4)(sp)		// globals
122	stw	r3, longReturnValue+0(r5)
123	stw	r4, longReturnValue+4(r5)
124	stfd	f1, floatReturnValue(r5)
125	lwz	r0, (FRAMESZ+8)(sp)
126 	mtlr	r0
127        lwz	r0, (FRAMESZ+4)(sp)		// saved ccr
128        mtcr	r0
129	addi	sp, sp, FRAMESZ			// pop trampoline frame
130	blr
131