/
dyncall_callvm_ppc32.c
388 lines (322 loc) · 10.7 KB
/
dyncall_callvm_ppc32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
Copyright (c) 2007-2009 Daniel Adler <dadler@uni-goettingen.de>,
Tassilo Philipp <tphilipp@potion-studios.com>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
dyncall callvm for ppc32 architectures
SUPPORTED CALLING CONVENTIONS
ppc32/osx
ppc32/linux (sysv abi)
REVISION
2007/12/11 initial support for Darwin ABI
2009/01/09 added System V ABI support
*/
#include "dyncall_callvm_ppc32.h"
#include "dyncall_call_ppc32.h"
#include "dyncall_alloc.h"
#include "dyncall_macros.h"
#include "dyncall_types.h"
/* Support for Mac OS X (Darwin) and Systen V ABI for Power PC 32-bit */
#if defined(DC__OS_Darwin)
#define DC__ABI_Darwin
#elif defined(DC__OS_Linux) || defined(DC__OS_FreeBSD) || defined(DC__OS_OpenBSD) || defined(DC__OS_NetBSD) || defined(DC__OS_DragonFlyBSD) || defined(DC__OS_SunOS)
#define DC__ABI_SysV
#else
#error Unsupported OS for ppc32 architecture.
#endif
static void dc_callvm_free_ppc32(DCCallVM* in_self)
{
dcFreeMem(in_self);
}
static void dc_callvm_reset_ppc32(DCCallVM* in_self)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
dcVecReset(&self->mVecHead);
self->mIntRegs = 0;
self->mFloatRegs = 0;
}
/* OS X/Darwin: fillup integer register file AND push on stack (for ellipsis) */
static void dc_callvm_argInt_ppc32_darwin(DCCallVM* in_self, DCint i)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
/* fillup integer register file */
if (self->mIntRegs < 8)
self->mRegData.mIntData[self->mIntRegs++] = i;
/* AND push onto stack */
dcVecAppend(&self->mVecHead,&i,sizeof(DCint));
}
static void dc_callvm_argInt_ppc32_sysv(DCCallVM* in_self, DCint i)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
/* fillup integer register file */
if (self->mIntRegs < 8)
self->mRegData.mIntData[self->mIntRegs++] = i;
/* OR push onto stack */
else
dcVecAppend(&self->mVecHead,&i,sizeof(DCint));
}
/** floating-point **/
/* double*/
static void dc_callvm_argDouble_ppc32_darwin(DCCallVM* in_self, DCdouble d)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
if (self->mFloatRegs < 13) {
self->mRegData.mFloatData[self->mFloatRegs++] = d;
/* skip two integer register file entries */
if (self->mIntRegs < 8)
self->mRegData.mIntData[self->mIntRegs++] = ( (DCint*) &d )[0];
if (self->mIntRegs < 8)
self->mRegData.mIntData[self->mIntRegs++] = ( (DCint*) &d )[1];
}
/* push on stack */
dcVecAppend(&self->mVecHead, &d, sizeof(DCdouble));
}
#if 0
static void dc_callvm_argDouble_ppc32_sysv(DCCallVM* in_self, DCdouble d)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
if (self->mFloatRegs < 8) {
self->mRegData.mFloatData[self->mFloatRegs++] = d;
/* skip two integer register file entries */
if (self->mIntRegs < 8)
self->mRegData.mIntData[self->mIntRegs++] = ( (DCint*) &d )[0];
if (self->mIntRegs < 8)
self->mRegData.mIntData[self->mIntRegs++] = ( (DCint*) &d )[1];
}
/* push on stack */
dcVecAppend(&self->mVecHead, &d, sizeof(DCdouble));
}
#endif
static void dc_callvm_argDouble_ppc32_sysv(DCCallVM* in_self, DCdouble d)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
if (self->mFloatRegs < 8)
self->mRegData.mFloatData[self->mFloatRegs++] = d;
else /* OR push data on stack */
{
/* align stack to 8 byte boundary */
dcVecResize(&self->mVecHead , ( dcVecSize(&self->mVecHead) + 7UL ) & -8UL );
/* AND push data */
dcVecAppend(&self->mVecHead,(DCpointer) &d,sizeof(DCdouble));
}
}
/* Floating-point */
/* darwin:
* - skip one integer register file entry (write in - for ellipsis calls)
* sysv:
* -
*/
static void dc_callvm_argFloat_ppc32_darwin(DCCallVM* in_self, DCfloat f)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
if (self->mFloatRegs < 13) {
self->mRegData.mFloatData[self->mFloatRegs++] = (DCdouble) (f);
}
/* AND skip one integer register file entry (write in - for ellipsis calls) */
if (self->mIntRegs < 8)
self->mRegData.mIntData[self->mIntRegs++] = *( (DCint*) &f );
/* AND push on stack */
dcVecAppend(&self->mVecHead, &f, sizeof(DCfloat));
}
static void dc_callvm_argFloat_ppc32_sysv(DCCallVM* in_self, DCfloat f)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
/* Put as float register (casted to double) */
if (self->mFloatRegs < 8)
self->mRegData.mFloatData[self->mFloatRegs++] = (DCdouble) (f);
else /* OR put float on stack */
dcVecAppend(&self->mVecHead, &f, sizeof(DCfloat));
}
/* long long integer */
static void dc_callvm_argLongLong_ppc32_darwin(DCCallVM* in_self, DClonglong L)
{
DCint* p = (DCint*) &L;
dcArgInt(in_self, p[0] );
dcArgInt(in_self, p[1] );
// dc_callvm_argInt_ppc32( in_self, p[0] );
// dc_callvm_argInt_ppc32( in_self, p[1] );
}
static void dc_callvm_argLongLong_ppc32_sysv(DCCallVM* in_self, DClonglong L)
{
DCint* p = (DCint*) &L;
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
/* fillup integer register file */
if (self->mIntRegs < 7)
{
/* next free integer register is even (r0, r2, r3) ? */
/* if not, skip one integer */
if (self->mIntRegs & 1) self->mIntRegs++;
self->mRegData.mIntData[self->mIntRegs++] = p[0];
self->mRegData.mIntData[self->mIntRegs++] = p[1];
}
/* OR push onto stack */
else
{
/* in case, mIntRegs == 7, set it to 8 */
self->mIntRegs = 8;
/* align stack to 8 byte boundary */
dcVecResize(&self->mVecHead , ( dcVecSize(&self->mVecHead) + 7 ) & (-8UL) );
/* push data */
dcVecAppend(&self->mVecHead,&L,sizeof(DClonglong));
}
}
static void dc_callvm_argBool_ppc32(DCCallVM* in_self, DCbool x)
{
/* promote to integer */
dcArgInt(in_self, (x == 0) ? DC_FALSE : DC_TRUE );
// dc_callvm_argInt_ppc32( in_self, (x == 0) ? DC_FALSE : DC_TRUE );
}
static void dc_callvm_argChar_ppc32(DCCallVM* in_self, DCchar ch)
{
/* promote to integer */
dcArgInt(in_self, (DCint) ch );
// dc_callvm_argInt_ppc32(in_self, (DCint) ch);
}
static void dc_callvm_argShort_ppc32(DCCallVM* in_self, DCshort s)
{
/* promote to integer */
dcArgInt(in_self, (DCint) s );
// dc_callvm_argInt_ppc32(in_self, (DCint) s);
}
static void dc_callvm_argLong_ppc32(DCCallVM* in_self, DClong l)
{
/* promote to integer */
dcArgInt(in_self, (DCint) l );
// dc_callvm_argInt_ppc32(in_self, (DCint) l);
}
static void dc_callvm_argPointer_ppc32(DCCallVM* in_self, DCpointer p)
{
/* promote to integer */
dcArgInt(in_self, *(DCint*) &p );
// dc_callvm_argInt_ppc32(in_self, * (DCint*) &p);
}
#ifndef max
#define max(a,b) (a>=b)?a:b
#endif
#if 0
/* Call. */
void dc_callvm_call_ppc32(DCCallVM* in_self, DCpointer target)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)in_self;
( void (*) (DCpointer, DCpointer, DCint, DCpointer) self->mpCallFunc ) (
/*
dcCall_ppc32(
*/
target
,&self->mRegData
,DC_MAX( dcVecSize(&self->mVecHead) , 8*4 )
,dcVecData(&self->mVecHead)
);
}
#endif
void dc_callvm_call_ppc32_darwin(DCCallVM* in_self, DCpointer target)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*) in_self;
dcCall_ppc32_darwin(
target,
&self->mRegData,
max( dcVecSize(&self->mVecHead), 8*4 ) ,
dcVecData(&self->mVecHead)
);
}
void dc_callvm_call_ppc32_sysv(DCCallVM* in_self, DCpointer target)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*) in_self;
dcCall_ppc32_sysv( target, &self->mRegData, dcVecSize(&self->mVecHead) , dcVecData(&self->mVecHead));
}
void dc_callvm_mode_ppc32(DCCallVM* in_self, DCint mode);
DCCallVM_vt gVT_ppc32_darwin =
{
&dc_callvm_free_ppc32
, &dc_callvm_reset_ppc32
, &dc_callvm_mode_ppc32
, &dc_callvm_argBool_ppc32
, &dc_callvm_argChar_ppc32
, &dc_callvm_argShort_ppc32
, &dc_callvm_argInt_ppc32_darwin
, &dc_callvm_argLong_ppc32
, &dc_callvm_argLongLong_ppc32_darwin
, &dc_callvm_argFloat_ppc32_darwin
, &dc_callvm_argDouble_ppc32_darwin
, &dc_callvm_argPointer_ppc32
, (DCvoidvmfunc*) &dc_callvm_call_ppc32_darwin
, (DCboolvmfunc*) &dc_callvm_call_ppc32_darwin
, (DCcharvmfunc*) &dc_callvm_call_ppc32_darwin
, (DCshortvmfunc*) &dc_callvm_call_ppc32_darwin
, (DCintvmfunc*) &dc_callvm_call_ppc32_darwin
, (DClongvmfunc*) &dc_callvm_call_ppc32_darwin
, (DClonglongvmfunc*) &dc_callvm_call_ppc32_darwin
, (DCfloatvmfunc*) &dc_callvm_call_ppc32_darwin
, (DCdoublevmfunc*) &dc_callvm_call_ppc32_darwin
, (DCpointervmfunc*) &dc_callvm_call_ppc32_darwin
};
DCCallVM_vt gVT_ppc32_sysv =
{
&dc_callvm_free_ppc32
, &dc_callvm_reset_ppc32
, &dc_callvm_mode_ppc32
, &dc_callvm_argBool_ppc32
, &dc_callvm_argChar_ppc32
, &dc_callvm_argShort_ppc32
, &dc_callvm_argInt_ppc32_sysv
, &dc_callvm_argLong_ppc32
, &dc_callvm_argLongLong_ppc32_sysv
, &dc_callvm_argFloat_ppc32_sysv
, &dc_callvm_argDouble_ppc32_sysv
, &dc_callvm_argPointer_ppc32
, (DCvoidvmfunc*) &dc_callvm_call_ppc32_sysv
, (DCboolvmfunc*) &dc_callvm_call_ppc32_sysv
, (DCcharvmfunc*) &dc_callvm_call_ppc32_sysv
, (DCshortvmfunc*) &dc_callvm_call_ppc32_sysv
, (DCintvmfunc*) &dc_callvm_call_ppc32_sysv
, (DClongvmfunc*) &dc_callvm_call_ppc32_sysv
, (DClonglongvmfunc*) &dc_callvm_call_ppc32_sysv
, (DCfloatvmfunc*) &dc_callvm_call_ppc32_sysv
, (DCdoublevmfunc*) &dc_callvm_call_ppc32_sysv
, (DCpointervmfunc*) &dc_callvm_call_ppc32_sysv
};
void dc_callvm_mode_ppc32(DCCallVM* in_self, DCint mode)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*) in_self;
DCCallVM_vt* vt;
switch(mode) {
case DC_CALL_C_PPC32_OSX:
#if defined(DC__ABI_Darwin)
case DC_CALL_C_DEFAULT:
case DC_CALL_C_ELLIPSIS:
#endif
vt = &gVT_ppc32_darwin;
break;
case DC_CALL_C_PPC32_SYSV:
#if defined(DC__ABI_SysV)
case DC_CALL_C_DEFAULT:
case DC_CALL_C_ELLIPSIS:
#endif
vt = &gVT_ppc32_sysv;
break;
default:
self->mInterface.mError = DC_ERROR_UNSUPPORTED_MODE;
return;
}
dc_callvm_base_init(&self->mInterface, vt);
dcReset(in_self);
}
DCCallVM* dcNewCallVM(DCsize size)
{
DCCallVM_ppc32* self = (DCCallVM_ppc32*)dcAllocMem(sizeof(DCCallVM_ppc32)+size);
dcVecInit(&self->mVecHead, size);
self->mIntRegs = 0;
self->mFloatRegs = 0;
dc_callvm_mode_ppc32( (DCCallVM*) self, DC_CALL_C_DEFAULT );
return (DCCallVM*)self;
}