0
|
1 /*
|
|
2
|
|
3 Package: dyncall
|
|
4 Library: dyncall
|
|
5 File: dyncall/dyncall_callvm_x64.c
|
|
6 Description:
|
|
7 License:
|
|
8
|
339
|
9 Copyright (c) 2007-2020 Daniel Adler <dadler@uni-goettingen.de>,
|
0
|
10 Tassilo Philipp <tphilipp@potion-studios.com>
|
|
11
|
|
12 Permission to use, copy, modify, and distribute this software for any
|
|
13 purpose with or without fee is hereby granted, provided that the above
|
|
14 copyright notice and this permission notice appear in all copies.
|
|
15
|
|
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
23
|
|
24 */
|
|
25
|
|
26
|
|
27
|
|
28
|
|
29 /* MS Windows x64 calling convention, AMD64 SystemV ABI. */
|
|
30
|
|
31
|
|
32 #include "dyncall_callvm_x64.h"
|
|
33 #include "dyncall_alloc.h"
|
|
34 #include "dyncall_struct.h"
|
|
35
|
|
36
|
|
37 static void dc_callvm_free_x64(DCCallVM* in_self)
|
|
38 {
|
|
39 dcFreeMem(in_self);
|
|
40 }
|
|
41
|
|
42
|
|
43 static void dc_callvm_reset_x64(DCCallVM* in_self)
|
|
44 {
|
|
45 DCCallVM_x64* self = (DCCallVM_x64*)in_self;
|
|
46 dcVecReset(&self->mVecHead);
|
84
|
47 self->mRegCount.i = self->mRegCount.f = 0;
|
0
|
48 }
|
|
49
|
|
50
|
|
51 static void dc_callvm_argLongLong_x64(DCCallVM* in_self, DClonglong x)
|
|
52 {
|
|
53 /* A long long always has 64 bits on the supported x64 platforms (lp64 on unix and llp64 on windows). */
|
|
54 DCCallVM_x64* self = (DCCallVM_x64*)in_self;
|
|
55 if(self->mRegCount.i < numIntRegs)
|
|
56 self->mRegData.i[self->mRegCount.i++] = x;
|
|
57 else
|
|
58 dcVecAppend(&self->mVecHead, &x, sizeof(DClonglong));
|
|
59 }
|
|
60
|
|
61
|
|
62 static void dc_callvm_argBool_x64(DCCallVM* in_self, DCbool x)
|
|
63 {
|
|
64 dc_callvm_argLongLong_x64(in_self, (DClonglong)x);
|
|
65 }
|
|
66
|
|
67
|
|
68 static void dc_callvm_argChar_x64(DCCallVM* in_self, DCchar x)
|
|
69 {
|
|
70 dc_callvm_argLongLong_x64(in_self, x);
|
|
71 }
|
|
72
|
|
73
|
|
74 static void dc_callvm_argShort_x64(DCCallVM* in_self, DCshort x)
|
|
75 {
|
|
76 dc_callvm_argLongLong_x64(in_self, x);
|
|
77 }
|
|
78
|
|
79
|
|
80 static void dc_callvm_argInt_x64(DCCallVM* in_self, DCint x)
|
|
81 {
|
|
82 dc_callvm_argLongLong_x64(in_self, x);
|
|
83 }
|
|
84
|
|
85
|
|
86 static void dc_callvm_argLong_x64(DCCallVM* in_self, DClong x)
|
|
87 {
|
|
88 dc_callvm_argLongLong_x64(in_self, x);
|
|
89 }
|
|
90
|
|
91
|
|
92 static void dc_callvm_argFloat_x64(DCCallVM* in_self, DCfloat x)
|
|
93 {
|
|
94 DCCallVM_x64* self = (DCCallVM_x64*)in_self;
|
|
95
|
|
96 /* Although not promoted to doubles, floats are stored with 64bits in this API.*/
|
|
97 union {
|
|
98 DCdouble d;
|
|
99 DCfloat f;
|
|
100 } f;
|
|
101 f.f = x;
|
|
102
|
|
103 if(self->mRegCount.f < numFloatRegs)
|
|
104 *(DCfloat*)&self->mRegData.f[self->mRegCount.f++] = x;
|
|
105 else
|
|
106 dcVecAppend(&self->mVecHead, &f.f, sizeof(DCdouble));
|
|
107 }
|
|
108
|
|
109
|
|
110 static void dc_callvm_argDouble_x64(DCCallVM* in_self, DCdouble x)
|
|
111 {
|
|
112 DCCallVM_x64* self = (DCCallVM_x64*)in_self;
|
|
113 if(self->mRegCount.f < numFloatRegs)
|
|
114 self->mRegData.f[self->mRegCount.f++] = x;
|
|
115 else
|
|
116 dcVecAppend(&self->mVecHead, &x, sizeof(DCdouble));
|
|
117 }
|
|
118
|
|
119
|
|
120 static void dc_callvm_argPointer_x64(DCCallVM* in_self, DCpointer x)
|
|
121 {
|
|
122 DCCallVM_x64* self = (DCCallVM_x64*)in_self;
|
|
123 if(self->mRegCount.i < numIntRegs)
|
|
124 *(DCpointer*)&self->mRegData.i[self->mRegCount.i++] = x;
|
|
125 else
|
|
126 dcVecAppend(&self->mVecHead, &x, sizeof(DCpointer));
|
|
127 }
|
|
128
|
|
129 static void dc_callvm_argStruct_x64(DCCallVM* in_self, DCstruct* s, DCpointer x)
|
|
130 {
|
|
131 DCCallVM_x64* self = (DCCallVM_x64*)in_self;
|
|
132 dcVecAppend(&self->mVecHead, x, s->size);
|
|
133 /*printf("dc_callvm_argStruct_x64 size = %d\n", (int)s->size);@@@*/
|
|
134 if (s->size <= 64)
|
|
135 dcArgStructUnroll(in_self, s, x);
|
|
136 /*else@@@*/
|
|
137 /* dcVecAppend(&self->mVecHead, &x, sizeof(DCpointer));@@@*/
|
|
138 }
|
|
139
|
|
140
|
|
141 /* Call. */
|
|
142 void dc_callvm_call_x64(DCCallVM* in_self, DCpointer target)
|
|
143 {
|
|
144 DCCallVM_x64* self = (DCCallVM_x64*)in_self;
|
|
145 #if defined(DC_UNIX)
|
|
146 dcCall_x64_sysv(
|
|
147 #else
|
|
148 dcCall_x64_win64(
|
|
149 #endif
|
|
150 dcVecSize(&self->mVecHead), /* Size of stack data. */
|
|
151 dcVecData(&self->mVecHead), /* Pointer to stack arguments. */
|
|
152 self->mRegData.i, /* Pointer to register arguments (ints on SysV). */
|
|
153 #if defined(DC_UNIX)
|
|
154 self->mRegData.f, /* Pointer to floating point register arguments. */
|
|
155 #endif
|
|
156 target
|
|
157 );
|
|
158 }
|
|
159
|
|
160
|
84
|
161 static void dc_callvm_mode_x64(DCCallVM* in_self, DCint mode);
|
|
162
|
0
|
163 DCCallVM_vt gVT_x64 =
|
|
164 {
|
|
165 &dc_callvm_free_x64
|
|
166 , &dc_callvm_reset_x64
|
|
167 , &dc_callvm_mode_x64
|
|
168 , &dc_callvm_argBool_x64
|
|
169 , &dc_callvm_argChar_x64
|
339
|
170 , &dc_callvm_argShort_x64
|
0
|
171 , &dc_callvm_argInt_x64
|
|
172 , &dc_callvm_argLong_x64
|
|
173 , &dc_callvm_argLongLong_x64
|
|
174 , &dc_callvm_argFloat_x64
|
|
175 , &dc_callvm_argDouble_x64
|
|
176 , &dc_callvm_argPointer_x64
|
|
177 , &dc_callvm_argStruct_x64
|
|
178 , (DCvoidvmfunc*) &dc_callvm_call_x64
|
|
179 , (DCboolvmfunc*) &dc_callvm_call_x64
|
|
180 , (DCcharvmfunc*) &dc_callvm_call_x64
|
|
181 , (DCshortvmfunc*) &dc_callvm_call_x64
|
|
182 , (DCintvmfunc*) &dc_callvm_call_x64
|
|
183 , (DClongvmfunc*) &dc_callvm_call_x64
|
|
184 , (DClonglongvmfunc*) &dc_callvm_call_x64
|
|
185 , (DCfloatvmfunc*) &dc_callvm_call_x64
|
|
186 , (DCdoublevmfunc*) &dc_callvm_call_x64
|
|
187 , (DCpointervmfunc*) &dc_callvm_call_x64
|
|
188 , NULL /* callStruct */
|
|
189 };
|
|
190
|
339
|
191
|
|
192 /* --- syscall ------------------------------------------------------------- */
|
|
193
|
|
194 #include <assert.h>
|
341
|
195 void dc_callvm_call_x64_syscall_sysv(DCCallVM* in_self, DCpointer target)
|
339
|
196 {
|
347
|
197 DCCallVM_x64* self;
|
|
198
|
339
|
199 /* syscalls can have up to 6 args, required to be "Only values of class INTEGER or class MEMORY" (from */
|
347
|
200 /* SysV manual), so we can use self->mRegData.i directly; verify this has space for at least 6 values, though. */
|
339
|
201 assert(numIntRegs >= 6);
|
|
202
|
347
|
203 self = (DCCallVM_x64*)in_self;
|
341
|
204 dcCall_x64_syscall_sysv(self->mRegData.i, target);
|
339
|
205 }
|
|
206
|
341
|
207 DCCallVM_vt gVT_x64_syscall_sysv =
|
339
|
208 {
|
|
209 &dc_callvm_free_x64
|
|
210 , &dc_callvm_reset_x64
|
|
211 , &dc_callvm_mode_x64
|
|
212 , &dc_callvm_argBool_x64
|
|
213 , &dc_callvm_argChar_x64
|
|
214 , &dc_callvm_argShort_x64
|
|
215 , &dc_callvm_argInt_x64
|
|
216 , &dc_callvm_argLong_x64
|
|
217 , &dc_callvm_argLongLong_x64
|
|
218 , &dc_callvm_argFloat_x64
|
|
219 , &dc_callvm_argDouble_x64
|
|
220 , &dc_callvm_argPointer_x64
|
|
221 , NULL /* argStruct */
|
341
|
222 , (DCvoidvmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
223 , (DCboolvmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
224 , (DCcharvmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
225 , (DCshortvmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
226 , (DCintvmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
227 , (DClongvmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
228 , (DClonglongvmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
229 , (DCfloatvmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
230 , (DCdoublevmfunc*) &dc_callvm_call_x64_syscall_sysv
|
|
231 , (DCpointervmfunc*) &dc_callvm_call_x64_syscall_sysv
|
339
|
232 , NULL /* callStruct */
|
|
233 };
|
|
234
|
|
235
|
|
236
|
|
237 /* mode */
|
|
238
|
84
|
239 static void dc_callvm_mode_x64(DCCallVM* in_self, DCint mode)
|
|
240 {
|
|
241 DCCallVM_x64* self = (DCCallVM_x64*)in_self;
|
|
242 DCCallVM_vt* vt;
|
0
|
243
|
84
|
244 switch(mode) {
|
|
245 case DC_CALL_C_DEFAULT:
|
|
246 #if defined(DC_UNIX)
|
|
247 case DC_CALL_C_X64_SYSV:
|
|
248 #else
|
|
249 case DC_CALL_C_X64_WIN64:
|
|
250 #endif
|
|
251 case DC_CALL_C_ELLIPSIS:
|
|
252 case DC_CALL_C_ELLIPSIS_VARARGS:
|
|
253 vt = &gVT_x64;
|
|
254 break;
|
339
|
255 case DC_CALL_SYS_DEFAULT:
|
|
256 # if defined DC_UNIX
|
|
257 case DC_CALL_SYS_X64_SYSCALL_SYSV:
|
341
|
258 vt = &gVT_x64_syscall_sysv; break;
|
339
|
259 # else
|
|
260 self->mInterface.mError = DC_ERROR_UNSUPPORTED_MODE; return;
|
|
261 # endif
|
84
|
262 default:
|
|
263 self->mInterface.mError = DC_ERROR_UNSUPPORTED_MODE;
|
|
264 return;
|
|
265 }
|
|
266 dc_callvm_base_init(&self->mInterface, vt);
|
0
|
267 }
|
|
268
|
84
|
269 /* Public API. */
|
0
|
270 DCCallVM* dcNewCallVM(DCsize size)
|
|
271 {
|
84
|
272 DCCallVM_x64* p = (DCCallVM_x64*)dcAllocMem(sizeof(DCCallVM_x64)+size);
|
|
273
|
|
274 dc_callvm_mode_x64((DCCallVM*)p, DC_CALL_C_DEFAULT);
|
|
275
|
|
276 /* Since we store register parameters in DCCallVM_x64 directly, adjust the stack size. */
|
|
277 size -= sizeof(DCRegData_x64);
|
|
278 size = (((signed long)size) < 0) ? 0 : size;
|
|
279 dcVecInit(&p->mVecHead, size);
|
|
280 dc_callvm_reset_x64((DCCallVM*)p);
|
|
281
|
|
282 return (DCCallVM*)p;
|
0
|
283 }
|
|
284
|