/
dyncall_callvm_x64.c
208 lines (159 loc) · 5.24 KB
/
dyncall_callvm_x64.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
/*
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.
*/
/* MS Windows x64 calling convention, AMD64 SystemV ABI. */
#include "dyncall_callvm_x64.h"
#include "dyncall_alloc.h"
static DCCallVM* dc_callvm_new_x64(DCCallVM_vt* vt, DCsize size)
{
DCCallVM_x64* self = (DCCallVM_x64*)dcAllocMem(sizeof(DCCallVM_x64)+size);
dc_callvm_base_init(&self->mInterface, vt);
/* Since we store register parameters in DCCallVM_x64 directly, adjust the stack size. */
size -= sizeof(DCRegData_x64);
size = size < 0 ? 0 : size;
self->mRegCount.i = self->mRegCount.f = 0;
dcVecInit(&self->mVecHead, size);
return (DCCallVM*)self;
}
static void dc_callvm_free_x64(DCCallVM* in_self)
{
dcFreeMem(in_self);
}
static void dc_callvm_reset_x64(DCCallVM* in_self)
{
DCCallVM_x64* self = (DCCallVM_x64*)in_self;
dcVecReset(&self->mVecHead);
self->mRegCount.i = self->mRegCount.f = 0;
}
static void dc_callvm_mode_x64(DCCallVM* self, DCint mode)
{
switch(mode) {
case DC_CALL_C_DEFAULT:
case DC_CALL_C_ELLIPSIS:
break;
default:
self->mError = DC_ERROR_UNSUPPORTED_MODE;
break;
}
}
static void dc_callvm_argLongLong_x64(DCCallVM* in_self, DClonglong x)
{
/* A long long always has 64 bits on the supported x64 platforms (lp64 on unix and llp64 on windows). */
DCCallVM_x64* self = (DCCallVM_x64*)in_self;
if(self->mRegCount.i < numIntRegs)
self->mRegData.i[self->mRegCount.i++] = x;
else
dcVecAppend(&self->mVecHead, &x, sizeof(DClonglong));
}
static void dc_callvm_argBool_x64(DCCallVM* in_self, DCbool x)
{
dc_callvm_argLongLong_x64(in_self, (DClonglong)x);
}
static void dc_callvm_argChar_x64(DCCallVM* in_self, DCchar x)
{
dc_callvm_argLongLong_x64(in_self, x);
}
static void dc_callvm_argShort_x64(DCCallVM* in_self, DCshort x)
{
dc_callvm_argLongLong_x64(in_self, x);
}
static void dc_callvm_argInt_x64(DCCallVM* in_self, DCint x)
{
dc_callvm_argLongLong_x64(in_self, x);
}
static void dc_callvm_argLong_x64(DCCallVM* in_self, DClong x)
{
dc_callvm_argLongLong_x64(in_self, x);
}
static void dc_callvm_argFloat_x64(DCCallVM* in_self, DCfloat x)
{
DCCallVM_x64* self = (DCCallVM_x64*)in_self;
/* Although not promoted to doubles, floats are stored with 64bits in this API.*/
union {
DCdouble d;
DCfloat f;
} f;
f.f = x;
if(self->mRegCount.f < numFloatRegs)
*(DCfloat*)&self->mRegData.f[self->mRegCount.f++] = x;
else
dcVecAppend(&self->mVecHead, &f.f, sizeof(DCdouble));
}
static void dc_callvm_argDouble_x64(DCCallVM* in_self, DCdouble x)
{
DCCallVM_x64* self = (DCCallVM_x64*)in_self;
if(self->mRegCount.f < numFloatRegs)
self->mRegData.f[self->mRegCount.f++] = x;
else
dcVecAppend(&self->mVecHead, &x, sizeof(DCdouble));
}
static void dc_callvm_argPointer_x64(DCCallVM* in_self, DCpointer x)
{
DCCallVM_x64* self = (DCCallVM_x64*)in_self;
if(self->mRegCount.i < numIntRegs)
*(DCpointer*)&self->mRegData.i[self->mRegCount.i++] = x;
else
dcVecAppend(&self->mVecHead, &x, sizeof(DCpointer));
}
/* Call. */
void dc_callvm_call_x64(DCCallVM* in_self, DCpointer target)
{
DCCallVM_x64* self = (DCCallVM_x64*)in_self;
#if defined(DC_UNIX)
dcCall_x64_sysv(
#else
dcCall_x64_win64(
#endif
dcVecSize(&self->mVecHead), /* Size of stack data. */
dcVecData(&self->mVecHead), /* Pointer to stack arguments. */
self->mRegData.i, /* Pointer to register arguments (ints on SysV). */
#if defined(DC_UNIX)
self->mRegData.f, /* Pointer to floating point register arguments. */
#endif
target
);
}
DCCallVM_vt gVT_x64 =
{
&dc_callvm_free_x64
, &dc_callvm_reset_x64
, &dc_callvm_mode_x64
, &dc_callvm_argBool_x64
, &dc_callvm_argChar_x64
, &dc_callvm_argShort_x64
, &dc_callvm_argInt_x64
, &dc_callvm_argLong_x64
, &dc_callvm_argLongLong_x64
, &dc_callvm_argFloat_x64
, &dc_callvm_argDouble_x64
, &dc_callvm_argPointer_x64
, (DCvoidvmfunc*) &dc_callvm_call_x64
, (DCboolvmfunc*) &dc_callvm_call_x64
, (DCcharvmfunc*) &dc_callvm_call_x64
, (DCshortvmfunc*) &dc_callvm_call_x64
, (DCintvmfunc*) &dc_callvm_call_x64
, (DClongvmfunc*) &dc_callvm_call_x64
, (DClonglongvmfunc*) &dc_callvm_call_x64
, (DCfloatvmfunc*) &dc_callvm_call_x64
, (DCdoublevmfunc*) &dc_callvm_call_x64
, (DCpointervmfunc*) &dc_callvm_call_x64
};
DCCallVM* dcNewCallVM_x64(DCsize size)
{
return dc_callvm_new_x64(&gVT_x64, size);
}
DCCallVM* dcNewCallVM(DCsize size)
{
return dcNewCallVM_x64(size);
}