comparison dyncall/dyncall_callvm_mips_n32.c @ 0:3e629dc19168

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:24:28 +0100
parents
children 67961454902b
comparison
equal deleted inserted replaced
-1:000000000000 0:3e629dc19168
1 /*
2
3 Package: dyncall
4 Library: dyncall
5 File: dyncall/dyncall_callvm_mips_n32.c
6 Description: mips "n32" ABI callvm implementation
7 License:
8
9 Copyright (c) 2007-2015 Daniel Adler <dadler@uni-goettingen.de>,
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
30 dyncall callvm for 64bit MIPS family of processors
31
32 SUPPORTED CALLING CONVENTIONS
33 n32
34
35 REVISION
36 2010/05/30 initial
37
38 in contrast to o32, there is no space reserved for parameters
39 passed over registers.
40 more registers are used and registers are always used.
41
42 stack is always 16-byte aligned at entry (call to call-kernel automatically
43 aligns argument stack.
44
45 integer and float register-file is interleaved either taking one slot up
46 skipping the other.
47
48
49 */
50
51
52 #include "dyncall_callvm_mips_n32.h"
53 #include "dyncall_alloc.h"
54 #include "dyncall_utils.h"
55
56 static void dc_callvm_reset_mips_n32(DCCallVM* in_self)
57 {
58 DCCallVM_mips_n32* self = (DCCallVM_mips_n32*)in_self;
59 dcVecReset(&self->mVecHead);
60 self->mRegCount = 0;
61 self->mRegData.mUseDouble = 0LL;
62 }
63
64 static DCCallVM* dc_callvm_new_mips_n32(DCCallVM_vt* vt, DCsize size)
65 {
66 DCCallVM_mips_n32* self = (DCCallVM_mips_n32*)dcAllocMem(sizeof(DCCallVM_mips_n32)+size);
67 dc_callvm_base_init(&self->mInterface, vt);
68 dcVecInit(&self->mVecHead, size);
69 dc_callvm_reset_mips_n32( (DCCallVM*) self );
70 return (DCCallVM*)self;
71 }
72
73
74 static void dc_callvm_free_mips_n32(DCCallVM* in_self)
75 {
76 dcFreeMem(in_self);
77 }
78
79 /* pass arguments :
80
81 - promote to 64-bit integer.
82 - fill up integers and float - left-to-right otherwise go over stack.
83 */
84
85 /* arg int -- fillup 64-bit integer register file OR push on stack */
86
87 static void dc_callvm_argLongLong_mips_n32(DCCallVM* in_self, DClonglong Lv)
88 {
89 DCCallVM_mips_n32* self = (DCCallVM_mips_n32*)in_self;
90 /* fillup integer register file */
91 if (self->mRegCount < 8)
92 self->mRegData.mIntData[self->mRegCount++] = Lv;
93 else
94 dcVecAppend(&self->mVecHead, &Lv, sizeof(DClonglong));
95 }
96
97 static void dc_callvm_argInt_mips_n32(DCCallVM* in_self, DCint i)
98 {
99 dc_callvm_argLongLong_mips_n32(in_self, (DClonglong) i );
100 }
101
102 static void dc_callvm_argPointer_mips_n32(DCCallVM* in_self, DCpointer x)
103 {
104 dc_callvm_argLongLong_mips_n32(in_self, * (DClonglong*) &x );
105 }
106
107 static void dc_callvm_argBool_mips_n32(DCCallVM* in_self, DCbool x)
108 {
109 dc_callvm_argLongLong_mips_n32(in_self, (DClonglong)x);
110 }
111
112 static void dc_callvm_argChar_mips_n32(DCCallVM* in_self, DCchar x)
113 {
114 dc_callvm_argLongLong_mips_n32(in_self, (DClonglong)x);
115 }
116
117 static void dc_callvm_argShort_mips_n32(DCCallVM* in_self, DCshort x)
118 {
119 dc_callvm_argLongLong_mips_n32(in_self, (DClonglong)x);
120 }
121
122 static void dc_callvm_argLong_mips_n32(DCCallVM* in_self, DClong x)
123 {
124 dc_callvm_argLongLong_mips_n32(in_self, (DClonglong)x);
125 }
126
127 static void dc_callvm_argDouble_mips_n32(DCCallVM* in_self, DCdouble x)
128 {
129 DCCallVM_mips_n32* self = (DCCallVM_mips_n32*)in_self;
130 if (self->mRegCount < 8) {
131 self->mRegData.mUseDouble |= 1<<( self->mRegCount );
132 self->mRegData.mFloatData[self->mRegCount++].d = x;
133 } else {
134 dcVecAppend(&self->mVecHead, &x, sizeof(DCdouble) );
135 }
136 }
137
138 static void dc_callvm_argFloat_mips_n32(DCCallVM* in_self, DCfloat x)
139 {
140 DCCallVM_mips_n32* self = (DCCallVM_mips_n32*)in_self;
141 if (self->mRegCount < 8) {
142 /*self->mRegData.mFloatData[self->mRegCount++].d = (DCdouble) x;*/
143 self->mRegData.mFloatData[self->mRegCount++].f = x;
144 } else {
145 dcVecAppend(&self->mVecHead, &x, sizeof(DCfloat) );
146 dcVecSkip(&self->mVecHead, sizeof(DCfloat) );
147 }
148 }
149
150
151 /* Ellipsis calls:
152 - float is promoted to double (due to ANSI C).
153 - double is passed via integer register-file (due to MIPS ABI).
154 */
155
156 static void dc_callvm_argDouble_mips_n32_ellipsis(DCCallVM* in_self, DCdouble x)
157 {
158 dc_callvm_argLongLong_mips_n32(in_self, * ( (DClonglong*) &x ) );
159 }
160
161 static void dc_callvm_argFloat_mips_n32_ellipsis(DCCallVM* in_self, DCfloat x)
162 {
163 dc_callvm_argDouble_mips_n32_ellipsis(in_self, (DCdouble) x );
164 }
165
166
167 /* Call. */
168
169 void dc_callvm_call_mips_n32(DCCallVM* in_self, DCpointer target)
170 {
171 DCCallVM_mips_n32* self = (DCCallVM_mips_n32*)in_self;
172 /* at minimum provide 16-bytes
173 which hold the first four integer register as spill area
174 and are automatically loaded to $4-$7
175 */
176 size_t size = DC_MAX(16, ( ( (unsigned) dcVecSize(&self->mVecHead) ) +7UL ) & (-8UL) );
177 dcCall_mips_n32(target, &self->mRegData, size, dcVecData(&self->mVecHead));
178 }
179
180 /* Forward Declaration. */
181 static void dc_callvm_mode_mips_n32(DCCallVM* in_self,DCint mode);
182
183 DCCallVM_vt gVT_mips_n32 =
184 {
185 &dc_callvm_free_mips_n32
186 , &dc_callvm_reset_mips_n32
187 , &dc_callvm_mode_mips_n32
188 , &dc_callvm_argBool_mips_n32
189 , &dc_callvm_argChar_mips_n32
190 , &dc_callvm_argShort_mips_n32
191 , &dc_callvm_argInt_mips_n32
192 , &dc_callvm_argLong_mips_n32
193 , &dc_callvm_argLongLong_mips_n32
194 , &dc_callvm_argFloat_mips_n32
195 , &dc_callvm_argDouble_mips_n32
196 , &dc_callvm_argPointer_mips_n32
197 , NULL /* argStruct */
198 , (DCvoidvmfunc*) &dc_callvm_call_mips_n32
199 , (DCboolvmfunc*) &dc_callvm_call_mips_n32
200 , (DCcharvmfunc*) &dc_callvm_call_mips_n32
201 , (DCshortvmfunc*) &dc_callvm_call_mips_n32
202 , (DCintvmfunc*) &dc_callvm_call_mips_n32
203 , (DClongvmfunc*) &dc_callvm_call_mips_n32
204 , (DClonglongvmfunc*) &dc_callvm_call_mips_n32
205 , (DCfloatvmfunc*) &dc_callvm_call_mips_n32
206 , (DCdoublevmfunc*) &dc_callvm_call_mips_n32
207 , (DCpointervmfunc*) &dc_callvm_call_mips_n32
208 , NULL /* callStruct */
209 };
210
211 DCCallVM_vt gVT_mips_n32_ellipsis =
212 {
213 &dc_callvm_free_mips_n32
214 , &dc_callvm_reset_mips_n32
215 , &dc_callvm_mode_mips_n32
216 , &dc_callvm_argBool_mips_n32
217 , &dc_callvm_argChar_mips_n32
218 , &dc_callvm_argShort_mips_n32
219 , &dc_callvm_argInt_mips_n32
220 , &dc_callvm_argLong_mips_n32
221 , &dc_callvm_argLongLong_mips_n32
222 , &dc_callvm_argFloat_mips_n32_ellipsis
223 , &dc_callvm_argDouble_mips_n32_ellipsis
224 , &dc_callvm_argPointer_mips_n32
225 , NULL /* argStruct */
226 , (DCvoidvmfunc*) &dc_callvm_call_mips_n32
227 , (DCboolvmfunc*) &dc_callvm_call_mips_n32
228 , (DCcharvmfunc*) &dc_callvm_call_mips_n32
229 , (DCshortvmfunc*) &dc_callvm_call_mips_n32
230 , (DCintvmfunc*) &dc_callvm_call_mips_n32
231 , (DClongvmfunc*) &dc_callvm_call_mips_n32
232 , (DClonglongvmfunc*) &dc_callvm_call_mips_n32
233 , (DCfloatvmfunc*) &dc_callvm_call_mips_n32
234 , (DCdoublevmfunc*) &dc_callvm_call_mips_n32
235 , (DCpointervmfunc*) &dc_callvm_call_mips_n32
236 , NULL /* callStruct */
237 };
238
239 static void dc_callvm_mode_mips_n32(DCCallVM* self,DCint mode)
240 {
241 switch(mode) {
242 case DC_CALL_C_DEFAULT:
243 self->mVTpointer = &gVT_mips_n32;
244 break;
245 case DC_CALL_C_ELLIPSIS:
246 self->mVTpointer = &gVT_mips_n32_ellipsis;
247 break;
248 default:
249 self->mError = DC_ERROR_UNSUPPORTED_MODE;
250 break;
251 }
252 }
253
254 DCCallVM* dcNewCallVM_mips_n32(DCsize size)
255 {
256 return dc_callvm_new_mips_n32(&gVT_mips_n32, size);
257 }
258
259 DCCallVM* dcNewCallVM_mips_n32_ellipsis(DCsize size)
260 {
261 return dc_callvm_new_mips_n32(&gVT_mips_n32_ellipsis, size);
262 }
263
264
265 DCCallVM* dcNewCallVM(DCsize size)
266 {
267 return dcNewCallVM_mips_n32(size);
268 }
269