| 1 |
/* |
| 2 |
** This file has been pre-processed with DynASM. |
| 3 |
** https://luajit.org/dynasm.html |
| 4 |
** DynASM version 1.5.0, DynASM x64 version 1.4.0 |
| 5 |
** DO NOT EDIT! The original file is in "call_x86.dasc". |
| 6 |
*/ |
| 7 |
|
| 8 |
/* vim: ts=4 sw=4 sts=4 et tw=78 |
| 9 |
* Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved. |
| 10 |
* Portions copyright (c) 2011 James R. McKaskill. |
| 11 |
* |
| 12 |
* This source code is licensed under the BSD-style license found in the |
| 13 |
* LICENSE file in the root directory of this source tree. An additional grant |
| 14 |
* of patent rights can be found in the PATENTS file in the same directory. |
| 15 |
*/ |
| 16 |
#if DASM_VERSION != 10500 |
| 17 |
#error "Version mismatch between DynASM and included encoding engine" |
| 18 |
#endif |
| 19 |
|
| 20 |
static const unsigned char build_actionlist[2100] = { |
| 21 |
72,139,141,233,255,72,137,132,253,36,233,255,221,133,233,255,217,133,233, |
| 22 |
255,252,243,15,126,133,233,255,252,243,15,90,133,233,255,221,156,253,36,233, |
| 23 |
255,217,156,253,36,233,255,102,15,214,132,253,36,233,255,252,242,15,90,192, |
| 24 |
102,15,214,132,253,36,233,255,252,242,15,90,192,102,15,126,132,253,36,233, |
| 25 |
255,85,72,137,229,65,84,72,129,252,236,239,72,137,77,16,72,137,85,24,76,137, |
| 26 |
69,32,76,137,77,40,102,15,214,69,252,240,102,15,214,77,232,102,15,214,85, |
| 27 |
224,102,15,214,93,216,255,73,188,237,237,255,73,199,192,237,72,199,194,237, |
| 28 |
76,137,225,232,251,1,0,255,73,199,192,237,72,199,194,252,255,252,255,252, |
| 29 |
255,252,255,76,137,225,232,251,1,0,255,73,199,192,237,72,199,194,237,76,137, |
| 30 |
225,232,251,1,0,73,184,237,237,72,199,194,252,255,252,255,252,255,252,255, |
| 31 |
76,137,225,232,251,1,1,255,72,137,8,72,199,194,252,254,252,255,252,255,252, |
| 32 |
255,76,137,225,232,251,1,2,255,73,184,237,237,72,199,194,0,0,0,0,76,137,225, |
| 33 |
232,251,1,1,255,72,137,8,255,102,15,214,0,255,217,24,255,217,88,4,255,102, |
| 34 |
15,214,64,8,255,252,243,15,126,200,76,137,225,232,251,1,3,255,15,182,201, |
| 35 |
72,137,202,76,137,225,232,251,1,4,255,15,182,201,255,15,190,201,255,72,137, |
| 36 |
202,76,137,225,232,251,1,5,255,15,183,201,255,15,191,201,255,72,137,202,76, |
| 37 |
137,225,232,251,1,6,255,73,185,237,237,73,199,192,237,72,199,194,237,76,137, |
| 38 |
225,232,251,1,7,255,73,199,192,237,72,199,194,252,254,252,255,252,255,252, |
| 39 |
255,76,137,225,232,251,1,0,73,185,237,237,73,199,192,252,255,252,255,252, |
| 40 |
255,252,255,72,199,194,252,254,252,255,252,255,252,255,76,137,225,232,251, |
| 41 |
1,8,72,137,68,36,32,72,199,194,252,252,252,255,252,255,252,255,76,137,225, |
| 42 |
232,251,1,9,72,139,68,36,32,255,73,199,192,237,72,199,194,252,254,252,255, |
| 43 |
252,255,252,255,76,137,225,232,251,1,0,73,185,237,237,73,199,192,252,255, |
| 44 |
252,255,252,255,252,255,72,199,194,252,254,252,255,252,255,252,255,76,137, |
| 45 |
225,232,251,1,10,137,68,36,32,72,199,194,252,252,252,255,252,255,252,255, |
| 46 |
76,137,225,232,251,1,9,139,68,36,32,255,72,199,194,252,254,252,255,252,255, |
| 47 |
252,255,76,137,225,232,251,1,9,255,72,199,194,252,255,252,255,252,255,252, |
| 48 |
255,76,137,225,232,251,1,11,255,72,199,194,252,255,252,255,252,255,252,255, |
| 49 |
76,137,225,232,251,1,12,255,137,68,36,32,72,199,194,252,253,252,255,252,255, |
| 50 |
252,255,76,137,225,232,251,1,9,139,68,36,32,255,72,199,194,252,255,252,255, |
| 51 |
252,255,252,255,76,137,225,232,251,1,13,255,72,199,194,252,255,252,255,252, |
| 52 |
255,252,255,76,137,225,232,251,1,14,255,72,137,68,36,32,72,199,194,252,253, |
| 53 |
252,255,252,255,252,255,76,137,225,232,251,1,9,72,139,68,36,32,255,72,199, |
| 54 |
194,252,255,252,255,252,255,252,255,76,137,225,232,251,1,15,72,137,68,36, |
| 55 |
32,72,199,194,252,253,252,255,252,255,252,255,76,137,225,232,251,1,9,72,139, |
| 56 |
68,36,32,255,72,199,194,252,255,252,255,252,255,252,255,76,137,225,232,251, |
| 57 |
1,16,102,15,214,68,36,32,72,199,194,252,253,252,255,252,255,252,255,76,137, |
| 58 |
225,232,251,1,9,255,252,242,15,90,68,36,32,255,252,243,15,126,68,36,32,255, |
| 59 |
72,199,194,252,255,252,255,252,255,252,255,76,137,225,232,251,1,17,102,15, |
| 60 |
214,68,36,32,72,199,194,252,253,252,255,252,255,252,255,76,137,225,232,251, |
| 61 |
1,9,252,243,15,126,68,36,32,255,72,199,194,252,255,252,255,252,255,252,255, |
| 62 |
76,137,225,232,251,1,18,102,15,214,68,36,32,102,15,214,76,36,40,72,199,194, |
| 63 |
252,253,252,255,252,255,252,255,76,137,225,232,251,1,9,252,243,15,126,68, |
| 64 |
36,32,252,243,15,126,76,36,40,255,72,139,141,233,73,199,192,252,255,252,255, |
| 65 |
252,255,252,255,76,137,226,72,137,201,232,251,1,18,72,131,252,236,4,72,199, |
| 66 |
194,252,253,252,255,252,255,252,255,76,137,225,232,251,1,9,255,76,139,101, |
| 67 |
252,248,72,137,252,236,93,194,236,255,85,72,137,229,65,84,65,85,73,137,204, |
| 68 |
72,131,252,236,32,76,137,225,232,251,1,19,73,137,197,72,129,252,248,239,255, |
| 69 |
15,141,244,248,102,184,0,0,72,186,237,237,76,137,225,232,251,1,20,248,2,15, |
| 70 |
142,244,247,102,184,0,0,72,186,237,237,76,137,225,232,251,1,20,255,15,141, |
| 71 |
244,247,102,184,0,0,72,186,237,237,76,137,225,232,251,1,20,255,248,1,255, |
| 72 |
72,193,224,4,72,41,196,72,129,252,236,239,255,73,184,237,237,72,199,194,0, |
| 73 |
0,0,0,76,137,225,232,251,1,1,72,131,252,236,16,255,73,185,237,237,73,199, |
| 74 |
192,237,72,199,194,237,76,137,225,232,251,1,8,255,73,185,237,237,73,199,192, |
| 75 |
237,72,199,194,237,76,137,225,232,251,1,21,255,73,185,237,237,73,199,192, |
| 76 |
237,72,199,194,237,76,137,225,232,251,1,10,255,72,199,194,237,76,137,225, |
| 77 |
232,251,1,12,255,15,182,192,255,15,190,192,255,15,183,192,255,15,191,192, |
| 78 |
255,72,199,194,237,76,137,225,232,251,1,12,131,252,248,0,15,149,208,15,182, |
| 79 |
192,255,72,199,194,237,76,137,225,232,251,1,11,255,72,199,194,237,76,137, |
| 80 |
225,232,251,1,15,255,72,199,194,237,76,137,225,232,251,1,13,255,72,199,194, |
| 81 |
237,76,137,225,232,251,1,14,255,72,199,194,237,76,137,225,232,251,1,16,255, |
| 82 |
72,199,194,237,76,137,225,232,251,1,18,255,252,243,15,126,193,255,72,141, |
| 83 |
132,253,36,233,72,131,252,236,4,73,199,192,237,76,137,226,72,137,193,232, |
| 84 |
251,1,18,255,72,199,194,237,76,137,225,232,251,1,17,255,72,199,194,237,76, |
| 85 |
137,225,232,251,1,17,137,4,36,217,4,36,255,137,20,36,217,4,36,255,73,129, |
| 86 |
252,253,239,15,142,244,247,72,137,224,72,129,192,239,73,137,193,77,137,232, |
| 87 |
72,199,194,237,76,137,225,232,251,1,22,72,137,224,72,129,192,239,73,137,193, |
| 88 |
73,199,192,237,72,199,194,237,76,137,225,232,251,1,23,252,233,244,248,248, |
| 89 |
1,72,137,224,72,129,192,239,73,137,193,77,137,232,72,199,194,237,76,137,225, |
| 90 |
232,251,1,23,248,2,255,72,137,224,72,129,192,239,73,137,193,77,137,232,72, |
| 91 |
199,194,237,76,137,225,232,251,1,22,255,72,185,237,237,139,1,72,137,193,232, |
| 92 |
251,1,24,255,72,131,196,32,255,252,243,15,126,156,253,36,233,255,76,139,140, |
| 93 |
253,36,233,255,252,243,15,126,148,253,36,233,255,76,139,132,253,36,233,255, |
| 94 |
252,243,15,126,140,253,36,233,255,72,139,148,253,36,233,255,252,243,15,126, |
| 95 |
4,36,255,72,139,12,36,255,232,251,1,25,72,131,252,236,48,255,72,137,68,36, |
| 96 |
32,232,251,1,26,72,185,237,237,137,1,73,184,237,237,72,199,194,237,76,137, |
| 97 |
225,232,251,1,1,72,139,76,36,32,72,137,8,184,1,0,0,0,76,139,109,252,240,76, |
| 98 |
139,101,252,248,72,137,252,236,93,195,255,72,137,68,36,32,232,251,1,26,72, |
| 99 |
185,237,237,137,1,72,139,68,36,32,72,137,194,76,137,225,232,251,1,27,184, |
| 100 |
1,0,0,0,76,139,109,252,240,76,139,101,252,248,72,137,252,236,93,195,255,72, |
| 101 |
137,68,36,32,232,251,1,26,72,185,237,237,137,1,73,184,237,237,72,199,194, |
| 102 |
0,0,0,0,76,137,225,232,251,1,1,72,139,76,36,32,72,137,8,184,1,0,0,0,76,139, |
| 103 |
109,252,240,76,139,101,252,248,72,137,252,236,93,195,255,102,15,214,68,36, |
| 104 |
32,232,251,1,26,72,185,237,237,137,1,73,184,237,237,72,199,194,237,76,137, |
| 105 |
225,232,251,1,1,72,139,76,36,32,72,137,8,184,1,0,0,0,76,139,109,252,240,76, |
| 106 |
139,101,252,248,72,137,252,236,93,195,255,102,15,214,76,36,40,102,15,214, |
| 107 |
68,36,32,232,251,1,26,72,185,237,237,137,1,73,184,237,237,72,199,194,237, |
| 108 |
76,137,225,232,251,1,1,72,139,76,36,40,72,137,72,8,72,139,76,36,32,72,137, |
| 109 |
8,184,1,0,0,0,76,139,109,252,240,76,139,101,252,248,72,137,252,236,93,195, |
| 110 |
255,232,251,1,26,72,185,237,237,137,1,184,0,0,0,0,76,139,109,252,240,76,139, |
| 111 |
101,252,248,72,137,252,236,93,195,255,15,182,192,137,68,36,32,232,251,1,26, |
| 112 |
72,185,237,237,137,1,139,68,36,32,72,137,194,76,137,225,232,251,1,4,184,1, |
| 113 |
0,0,0,76,139,109,252,240,76,139,101,252,248,72,137,252,236,93,195,255,137, |
| 114 |
68,36,32,232,251,1,26,72,185,237,237,137,1,139,68,36,32,72,137,194,76,137, |
| 115 |
225,232,251,1,5,184,1,0,0,0,76,139,109,252,240,76,139,101,252,248,72,137, |
| 116 |
252,236,93,195,255,137,68,36,32,232,251,1,26,72,185,237,237,137,1,139,68, |
| 117 |
36,32,72,137,194,76,137,225,232,251,1,6,184,1,0,0,0,76,139,109,252,240,76, |
| 118 |
139,101,252,248,72,137,252,236,93,195,255,252,243,15,90,192,102,15,214,68, |
| 119 |
36,32,232,251,1,26,72,185,237,237,137,1,252,243,15,126,76,36,32,76,137,225, |
| 120 |
232,251,1,3,184,1,0,0,0,76,139,109,252,240,76,139,101,252,248,72,137,252, |
| 121 |
236,93,195,255 |
| 122 |
}; |
| 123 |
|
| 124 |
static const char *const globnames[] = { |
| 125 |
(const char *)0 |
| 126 |
}; |
| 127 |
static const char *const extnames[] = { |
| 128 |
"lua_rawgeti", |
| 129 |
"push_cdata", |
| 130 |
"lua_remove", |
| 131 |
"lua_pushnumber", |
| 132 |
"lua_pushboolean", |
| 133 |
"push_int", |
| 134 |
"push_uint", |
| 135 |
"lua_callk", |
| 136 |
"check_typed_pointer", |
| 137 |
"lua_settop", |
| 138 |
"check_enum", |
| 139 |
"check_uint32", |
| 140 |
"check_int32", |
| 141 |
"check_uint64", |
| 142 |
"check_int64", |
| 143 |
"check_uintptr", |
| 144 |
"check_double", |
| 145 |
"check_complex_float", |
| 146 |
"check_complex_double", |
| 147 |
"lua_gettop", |
| 148 |
"luaL_error", |
| 149 |
"check_typed_cfunction", |
| 150 |
"unpack_varargs_stack", |
| 151 |
"unpack_varargs_reg", |
| 152 |
"SetLastError", |
| 153 |
"FUNCTION", |
| 154 |
"GetLastError", |
| 155 |
"lua_pushinteger", |
| 156 |
(const char *)0 |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#if defined _WIN64 || defined __amd64__ |
| 176 |
#define JUMP_SIZE 14 |
| 177 |
#else |
| 178 |
#define JUMP_SIZE 4 |
| 179 |
#endif |
| 180 |
|
| 181 |
#define MIN_BRANCH INT32_MIN |
| 182 |
#define MAX_BRANCH INT32_MAX |
| 183 |
#define BRANCH_OFF 4 |
| 184 |
|
| 185 |
static void compile_extern_jump(struct jit* jit, lua_State* L, cfunction func, uint8_t* code) |
| 186 |
{ |
| 187 |
/* The jump code is the function pointer followed by a stub to call the |
| 188 |
* function pointer. The stub exists in 64 bit so we can jump to functions |
| 189 |
* with an offset greater than 2 GB. |
| 190 |
* |
| 191 |
* Note we have to manually set this up since there are commands buffered |
| 192 |
* in the jit state and dynasm doesn't support rip relative addressing. |
| 193 |
* |
| 194 |
* eg on 64 bit: |
| 195 |
* 0-8: function ptr |
| 196 |
* 8-14: jmp aword [rip-14] |
| 197 |
* |
| 198 |
* for 32 bit we only set the function ptr as it can always fit in a 32 |
| 199 |
* bit displacement |
| 200 |
*/ |
| 201 |
#if defined _WIN64 || defined __amd64__ |
| 202 |
*(cfunction*) code = func; |
| 203 |
code[8] = 0xFF; /* FF /4 operand for jmp */ |
| 204 |
code[9] = 0x25; /* RIP displacement */ |
| 205 |
*(int32_t*) &code[10] = -14; |
| 206 |
#else |
| 207 |
*(cfunction*) code = func; |
| 208 |
#endif |
| 209 |
} |
| 210 |
|
| 211 |
void compile_globals(struct jit* jit, lua_State* L) |
| 212 |
{ |
| 213 |
struct jit* Dst = jit; |
| 214 |
int* perr = &jit->last_errno; |
| 215 |
dasm_setup(Dst, build_actionlist); |
| 216 |
|
| 217 |
/* Note: since the return code uses EBP to reset the stack pointer, we |
| 218 |
* don't have to track the amount of stack space used. It also means we |
| 219 |
* can handle stdcall and cdecl with the same code. |
| 220 |
*/ |
| 221 |
|
| 222 |
/* Note the various call_* functions want 32 bytes of 16 byte aligned |
| 223 |
* stack |
| 224 |
*/ |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
compile(Dst, L, NULL, LUA_NOREF); |
| 229 |
} |
| 230 |
|
| 231 |
int x86_return_size(lua_State* L, int usr, const struct ctype* ct) |
| 232 |
{ |
| 233 |
int ret = 0; |
| 234 |
const struct ctype* mt; |
| 235 |
|
| 236 |
if (ct->calling_convention != C_CALL) { |
| 237 |
size_t i; |
| 238 |
size_t argn = lua_rawlen(L, usr); |
| 239 |
for (i = 1; i <= argn; i++) { |
| 240 |
lua_rawgeti(L, usr, (int) i); |
| 241 |
mt = (const struct ctype*) lua_touserdata(L, -1); |
| 242 |
|
| 243 |
if (mt->pointers || mt->is_reference) { |
| 244 |
ret += sizeof(void*); |
| 245 |
} else { |
| 246 |
switch (mt->type) { |
| 247 |
case DOUBLE_TYPE: |
| 248 |
case COMPLEX_FLOAT_TYPE: |
| 249 |
case INT64_TYPE: |
| 250 |
ret += 8; |
| 251 |
break; |
| 252 |
case COMPLEX_DOUBLE_TYPE: |
| 253 |
ret += 16; |
| 254 |
break; |
| 255 |
case INTPTR_TYPE: |
| 256 |
ret += sizeof(intptr_t); |
| 257 |
break; |
| 258 |
case FUNCTION_PTR_TYPE: |
| 259 |
ret += sizeof(cfunction); |
| 260 |
break; |
| 261 |
case BOOL_TYPE: |
| 262 |
case FLOAT_TYPE: |
| 263 |
case INT8_TYPE: |
| 264 |
case INT16_TYPE: |
| 265 |
case INT32_TYPE: |
| 266 |
case ENUM_TYPE: |
| 267 |
ret += 4; |
| 268 |
break; |
| 269 |
default: |
| 270 |
return luaL_error(L, "NYI - argument type"); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
lua_pop(L, 1); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
#if !defined _WIN64 && !defined __amd64__ |
| 279 |
lua_rawgeti(L, usr, 0); |
| 280 |
mt = (const struct ctype*) lua_touserdata(L, -1); |
| 281 |
if (!mt->pointers && !mt->is_reference && mt->type == COMPLEX_DOUBLE_TYPE) { |
| 282 |
ret += sizeof(void*); |
| 283 |
} |
| 284 |
lua_pop(L, 1); |
| 285 |
#endif |
| 286 |
|
| 287 |
return ret; |
| 288 |
} |
| 289 |
|
| 290 |
#ifdef _WIN64 |
| 291 |
#define MAX_REGISTERS(ct) 4 /* rcx, rdx, r8, r9 */ |
| 292 |
|
| 293 |
#elif defined __amd64__ |
| 294 |
#define MAX_INT_REGISTERS(ct) 6 /* rdi, rsi, rdx, rcx, r8, r9 */ |
| 295 |
#define MAX_FLOAT_REGISTERS(ct) 8 /* xmm0-7 */ |
| 296 |
|
| 297 |
#else |
| 298 |
#define MAX_INT_REGISTERS(ct) ((ct)->calling_convention == FAST_CALL ? 2 /* ecx, edx */ : 0) |
| 299 |
#define MAX_FLOAT_REGISTERS(ct) 0 |
| 300 |
#endif |
| 301 |
|
| 302 |
struct reg_alloc { |
| 303 |
#ifdef _WIN64 |
| 304 |
int regs; |
| 305 |
int is_float[4]; |
| 306 |
int is_int[4]; |
| 307 |
#else |
| 308 |
int floats; |
| 309 |
int ints; |
| 310 |
#endif |
| 311 |
int off; |
| 312 |
}; |
| 313 |
|
| 314 |
#ifdef _WIN64 |
| 315 |
#define REGISTER_STACK_SPACE(ct) (4*8) |
| 316 |
#elif defined __amd64__ |
| 317 |
#define REGISTER_STACK_SPACE(ct) (14*8) |
| 318 |
#else |
| 319 |
#define REGISTER_STACK_SPACE(ct) ALIGN_UP(((ct)->calling_convention == FAST_CALL ? 2*4 : 0), 15) |
| 320 |
#endif |
| 321 |
|
| 322 |
/* Fastcall: |
| 323 |
* Uses ecx, edx as first two int registers |
| 324 |
* Everything else on stack (include 64bit ints) |
| 325 |
* No overflow stack space |
| 326 |
* Pops the stack before returning |
| 327 |
* Returns int in eax, float in ST0 |
| 328 |
* We use the same register allocation logic as posix x64 with 2 int regs and 0 float regs |
| 329 |
*/ |
| 330 |
|
| 331 |
static void get_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_int64) |
| 332 |
{ |
| 333 |
/* grab the register from the shadow space */ |
| 334 |
#ifdef _WIN64 |
| 335 |
if (reg->regs < MAX_REGISTERS(ct)) { |
| 336 |
dasm_put(Dst, 0, 16 + 8*reg->regs); |
| 337 |
reg->regs++; |
| 338 |
} |
| 339 |
#elif __amd64__ |
| 340 |
if (reg->ints < MAX_INT_REGISTERS(ct)) { |
| 341 |
dasm_put(Dst, 0, - 80 - 8*reg->ints); |
| 342 |
reg->ints++; |
| 343 |
} |
| 344 |
#else |
| 345 |
if (!is_int64 && reg->ints < MAX_INT_REGISTERS(ct)) { |
| 346 |
dasm_put(Dst, 1, - 8 - 4*reg->ints); |
| 347 |
reg->ints++; |
| 348 |
} |
| 349 |
#endif |
| 350 |
else if (is_int64) { |
| 351 |
dasm_put(Dst, 0, reg->off); |
| 352 |
reg->off += 8; |
| 353 |
} else { |
| 354 |
dasm_put(Dst, 1, reg->off); |
| 355 |
#if defined __amd64__ || defined _WIN64 |
| 356 |
/* The parameters to a function on stack are always 8 byte aligned. */ |
| 357 |
reg->off += 8; |
| 358 |
#else |
| 359 |
reg->off += 4; |
| 360 |
#endif |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
static void add_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_int64) |
| 365 |
{ |
| 366 |
#ifdef _WIN64 |
| 367 |
if (reg->regs < MAX_REGISTERS(ct)) { |
| 368 |
dasm_put(Dst, 5, 32 + 8*(reg->regs)); |
| 369 |
reg->is_int[reg->regs++] = 1; |
| 370 |
} |
| 371 |
#elif __amd64__ |
| 372 |
if (reg->ints < MAX_INT_REGISTERS(ct)) { |
| 373 |
dasm_put(Dst, 5, 32 + 8*reg->ints); |
| 374 |
reg->ints++; |
| 375 |
} |
| 376 |
#else |
| 377 |
if (!is_int64 && reg->ints < MAX_INT_REGISTERS(ct)) { |
| 378 |
dasm_put(Dst, 5, 32 + 4*reg->ints); |
| 379 |
reg->ints++; |
| 380 |
} |
| 381 |
#endif |
| 382 |
else { |
| 383 |
#if defined _WIN64 || defined __amd64__ |
| 384 |
if (reg->off % 8 != 0) { |
| 385 |
reg->off += 8 - (reg->off % 8); |
| 386 |
} |
| 387 |
#endif |
| 388 |
if (is_int64) { |
| 389 |
dasm_put(Dst, 5, reg->off); |
| 390 |
reg->off += 8; |
| 391 |
} else { |
| 392 |
dasm_put(Dst, 6, reg->off); |
| 393 |
reg->off += 4; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
static void get_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double) |
| 399 |
{ |
| 400 |
#if !defined _WIN64 && !defined __amd64__ |
| 401 |
assert(MAX_FLOAT_REGISTERS(ct) == 0); |
| 402 |
if (is_double) { |
| 403 |
dasm_put(Dst, 12, reg->off); |
| 404 |
reg->off += 8; |
| 405 |
} else { |
| 406 |
dasm_put(Dst, 16, reg->off); |
| 407 |
reg->off += 4; |
| 408 |
} |
| 409 |
#else |
| 410 |
int off; |
| 411 |
|
| 412 |
#ifdef _WIN64 |
| 413 |
if (reg->regs < MAX_REGISTERS(ct)) { |
| 414 |
off = -16 - 8*reg->regs; |
| 415 |
reg->regs++; |
| 416 |
} |
| 417 |
#else |
| 418 |
if (reg->floats < MAX_FLOAT_REGISTERS(ct)) { |
| 419 |
off = -16 - 8*reg->floats; |
| 420 |
reg->floats++; |
| 421 |
} |
| 422 |
#endif |
| 423 |
else { |
| 424 |
off = reg->off; |
| 425 |
reg->off += is_double ? 8 : 4; |
| 426 |
} |
| 427 |
|
| 428 |
if (is_double) { |
| 429 |
dasm_put(Dst, 20, off); |
| 430 |
} else { |
| 431 |
dasm_put(Dst, 27, off); |
| 432 |
} |
| 433 |
#endif |
| 434 |
} |
| 435 |
|
| 436 |
static void add_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double) |
| 437 |
{ |
| 438 |
#if !defined _WIN64 && !defined __amd64__ |
| 439 |
assert(MAX_FLOAT_REGISTERS(ct) == 0); |
| 440 |
if (is_double) { |
| 441 |
dasm_put(Dst, 34, reg->off); |
| 442 |
reg->off += 8; |
| 443 |
} else { |
| 444 |
dasm_put(Dst, 40, reg->off); |
| 445 |
reg->off += 4; |
| 446 |
} |
| 447 |
#else |
| 448 |
|
| 449 |
#ifdef _WIN64 |
| 450 |
if (reg->regs < MAX_REGISTERS(ct)) { |
| 451 |
if (is_double) { |
| 452 |
dasm_put(Dst, 46, 32 + 8*(reg->regs)); |
| 453 |
} else { |
| 454 |
dasm_put(Dst, 54, 32 + 8*(reg->regs)); |
| 455 |
} |
| 456 |
reg->is_float[reg->regs++] = 1; |
| 457 |
} |
| 458 |
#else |
| 459 |
if (reg->floats < MAX_FLOAT_REGISTERS(ct)) { |
| 460 |
if (is_double) { |
| 461 |
dasm_put(Dst, 46, 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats)); |
| 462 |
} else { |
| 463 |
dasm_put(Dst, 54, 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats)); |
| 464 |
} |
| 465 |
reg->floats++; |
| 466 |
} |
| 467 |
#endif |
| 468 |
|
| 469 |
else if (is_double) { |
| 470 |
dasm_put(Dst, 46, reg->off); |
| 471 |
reg->off += 8; |
| 472 |
} else { |
| 473 |
dasm_put(Dst, 67, reg->off); |
| 474 |
reg->off += 4; |
| 475 |
} |
| 476 |
#endif |
| 477 |
} |
| 478 |
|
| 479 |
#if defined _WIN64 || defined __amd64__ |
| 480 |
#define add_pointer(jit, ct, reg) add_int(jit, ct, reg, 1) |
| 481 |
#define get_pointer(jit, ct, reg) get_int(jit, ct, reg, 1) |
| 482 |
#else |
| 483 |
#define add_pointer(jit, ct, reg) add_int(jit, ct, reg, 0) |
| 484 |
#define get_pointer(jit, ct, reg) get_int(jit, ct, reg, 0) |
| 485 |
#endif |
| 486 |
|
| 487 |
cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct) |
| 488 |
{ |
| 489 |
int i, nargs; |
| 490 |
cfunction* pf; |
| 491 |
struct ctype ct2 = *ct; |
| 492 |
const struct ctype* mt; |
| 493 |
struct reg_alloc reg; |
| 494 |
int num_upvals = 0; |
| 495 |
int top = lua_gettop(L); |
| 496 |
struct jit* Dst = get_jit(L); |
| 497 |
int ref; |
| 498 |
int hidden_arg_off = 0; |
| 499 |
|
| 500 |
ct_usr = lua_absindex(L, ct_usr); |
| 501 |
fidx = lua_absindex(L, fidx); |
| 502 |
|
| 503 |
assert(lua_isnil(L, fidx) || lua_isfunction(L, fidx)); |
| 504 |
|
| 505 |
memset(®, 0, sizeof(reg)); |
| 506 |
#ifdef _WIN64 |
| 507 |
reg.off = 16 + REGISTER_STACK_SPACE(ct); /* stack registers are above the shadow space */ |
| 508 |
#elif __amd64__ |
| 509 |
reg.off = 16; |
| 510 |
#else |
| 511 |
reg.off = 8; |
| 512 |
#endif |
| 513 |
|
| 514 |
dasm_setup(Dst, build_actionlist); |
| 515 |
|
| 516 |
// add a table to store ctype and function upvalues |
| 517 |
// callback_set assumes the first value is the lua function |
| 518 |
nargs = (int) lua_rawlen(L, ct_usr); |
| 519 |
lua_newtable(L); |
| 520 |
lua_pushvalue(L, -1); |
| 521 |
ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 522 |
|
| 523 |
if (ct->has_var_arg) { |
| 524 |
luaL_error(L, "can't create callbacks with varargs"); |
| 525 |
} |
| 526 |
|
| 527 |
// setup a stack frame to hold args for the call into lua_call |
| 528 |
|
| 529 |
dasm_put(Dst, 80, 8 + 16 + 32 + REGISTER_STACK_SPACE(ct)); |
| 530 |
if (ct->calling_convention == FAST_CALL) { |
| 531 |
} |
| 532 |
|
| 533 |
// hardcode the lua_State* value into the assembly |
| 534 |
dasm_put(Dst, 129, (unsigned int)((uintptr_t)(L)), (unsigned int)(((uintptr_t)(L))>>32)); |
| 535 |
|
| 536 |
/* get the upval table */ |
| 537 |
dasm_put(Dst, 134, ref, LUA_REGISTRYINDEX); |
| 538 |
|
| 539 |
/* get the lua function */ |
| 540 |
lua_pushvalue(L, fidx); |
| 541 |
lua_rawseti(L, -2, ++num_upvals); |
| 542 |
assert(num_upvals == CALLBACK_FUNC_USR_IDX); |
| 543 |
dasm_put(Dst, 150, num_upvals); |
| 544 |
|
| 545 |
#if !defined _WIN64 && !defined __amd64__ |
| 546 |
lua_rawgeti(L, ct_usr, 0); |
| 547 |
mt = (const struct ctype*) lua_touserdata(L, -1); |
| 548 |
if (!mt->pointers && !mt->is_reference && mt->type == COMPLEX_DOUBLE_TYPE) { |
| 549 |
hidden_arg_off = reg.off; |
| 550 |
reg.off += sizeof(void*); |
| 551 |
} |
| 552 |
lua_pop(L, 1); |
| 553 |
#else |
| 554 |
(void) hidden_arg_off; |
| 555 |
#endif |
| 556 |
|
| 557 |
for (i = 1; i <= nargs; i++) { |
| 558 |
lua_rawgeti(L, ct_usr, i); |
| 559 |
mt = (const struct ctype*) lua_touserdata(L, -1); |
| 560 |
|
| 561 |
if (mt->pointers || mt->is_reference) { |
| 562 |
lua_getuservalue(L, -1); |
| 563 |
lua_rawseti(L, -3, ++num_upvals); /* usr value */ |
| 564 |
lua_rawseti(L, -2, ++num_upvals); /* mt */ |
| 565 |
/* on the lua stack in the callback: |
| 566 |
* upval tbl, lua func, i-1 args |
| 567 |
*/ |
| 568 |
dasm_put(Dst, 173, num_upvals-1, -i-1, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32)); |
| 569 |
get_pointer(Dst, ct, ®); |
| 570 |
dasm_put(Dst, 211); |
| 571 |
} else { |
| 572 |
switch (mt->type) { |
| 573 |
case INT64_TYPE: |
| 574 |
lua_getuservalue(L, -1); |
| 575 |
lua_rawseti(L, -3, ++num_upvals); /* mt */ |
| 576 |
lua_pop(L, 1); |
| 577 |
dasm_put(Dst, 233, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32)); |
| 578 |
get_int(Dst, ct, ®, 1); |
| 579 |
dasm_put(Dst, 252); |
| 580 |
break; |
| 581 |
|
| 582 |
case INTPTR_TYPE: |
| 583 |
lua_getuservalue(L, -1); |
| 584 |
lua_rawseti(L, -3, ++num_upvals); /* mt */ |
| 585 |
lua_pop(L, 1); |
| 586 |
dasm_put(Dst, 233, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32)); |
| 587 |
get_pointer(Dst, ct, ®); |
| 588 |
dasm_put(Dst, 252); |
| 589 |
break; |
| 590 |
|
| 591 |
case COMPLEX_FLOAT_TYPE: |
| 592 |
lua_pop(L, 1); |
| 593 |
#if defined _WIN64 || defined __amd64__ |
| 594 |
/* complex floats are two floats packed into a double */ |
| 595 |
dasm_put(Dst, 233, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32)); |
| 596 |
get_float(Dst, ct, ®, 1); |
| 597 |
dasm_put(Dst, 256); |
| 598 |
#else |
| 599 |
/* complex floats are real followed by imag on the stack */ |
| 600 |
dasm_put(Dst, 233, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32)); |
| 601 |
get_float(Dst, ct, ®, 0); |
| 602 |
dasm_put(Dst, 261); |
| 603 |
get_float(Dst, ct, ®, 0); |
| 604 |
dasm_put(Dst, 264); |
| 605 |
#endif |
| 606 |
break; |
| 607 |
|
| 608 |
case COMPLEX_DOUBLE_TYPE: |
| 609 |
lua_pop(L, 1); |
| 610 |
dasm_put(Dst, 233, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32)); |
| 611 |
/* real */ |
| 612 |
get_float(Dst, ct, ®, 1); |
| 613 |
dasm_put(Dst, 256); |
| 614 |
/* imag */ |
| 615 |
get_float(Dst, ct, ®, 1); |
| 616 |
dasm_put(Dst, 268); |
| 617 |
break; |
| 618 |
|
| 619 |
case FLOAT_TYPE: |
| 620 |
case DOUBLE_TYPE: |
| 621 |
lua_pop(L, 1); |
| 622 |
get_float(Dst, ct, ®, mt->type == DOUBLE_TYPE); |
| 623 |
dasm_put(Dst, 274); |
| 624 |
break; |
| 625 |
|
| 626 |
case BOOL_TYPE: |
| 627 |
lua_pop(L, 1); |
| 628 |
get_int(Dst, ct, ®, 0); |
| 629 |
dasm_put(Dst, 287); |
| 630 |
break; |
| 631 |
|
| 632 |
case INT8_TYPE: |
| 633 |
lua_pop(L, 1); |
| 634 |
get_int(Dst, ct, ®, 0); |
| 635 |
if (mt->is_unsigned) { |
| 636 |
dasm_put(Dst, 301); |
| 637 |
} else { |
| 638 |
dasm_put(Dst, 305); |
| 639 |
} |
| 640 |
dasm_put(Dst, 309); |
| 641 |
break; |
| 642 |
|
| 643 |
case INT16_TYPE: |
| 644 |
lua_pop(L, 1); |
| 645 |
get_int(Dst, ct, ®, 0); |
| 646 |
if (mt->is_unsigned) { |
| 647 |
dasm_put(Dst, 320); |
| 648 |
} else { |
| 649 |
dasm_put(Dst, 324); |
| 650 |
} |
| 651 |
dasm_put(Dst, 309); |
| 652 |
break; |
| 653 |
|
| 654 |
case ENUM_TYPE: |
| 655 |
case INT32_TYPE: |
| 656 |
lua_pop(L, 1); |
| 657 |
get_int(Dst, ct, ®, 0); |
| 658 |
if (mt->is_unsigned) { |
| 659 |
dasm_put(Dst, 328); |
| 660 |
} else { |
| 661 |
dasm_put(Dst, 309); |
| 662 |
} |
| 663 |
break; |
| 664 |
|
| 665 |
default: |
| 666 |
luaL_error(L, "NYI: callback arg type"); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
lua_rawgeti(L, ct_usr, 0); |
| 672 |
mt = (const struct ctype*) lua_touserdata(L, -1); |
| 673 |
|
| 674 |
dasm_put(Dst, 339, (unsigned int)((uintptr_t)(0)), (unsigned int)(((uintptr_t)(0))>>32), (mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0, nargs); |
| 675 |
|
| 676 |
// Unpack the return argument if not "void", also clean-up the lua stack |
| 677 |
// to remove the return argument and bind table. Use lua_settop rather |
| 678 |
// than lua_pop as lua_pop is implemented as a macro. |
| 679 |
if (mt->pointers || mt->is_reference) { |
| 680 |
lua_getuservalue(L, -1); |
| 681 |
lua_rawseti(L, -3, ++num_upvals); /* usr value */ |
| 682 |
lua_rawseti(L, -2, ++num_upvals); /* mt */ |
| 683 |
dasm_put(Dst, 359, num_upvals-1, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32)); |
| 684 |
|
| 685 |
} else { |
| 686 |
switch (mt->type) { |
| 687 |
case ENUM_TYPE: |
| 688 |
lua_getuservalue(L, -1); |
| 689 |
lua_rawseti(L, -3, ++num_upvals); /* usr value */ |
| 690 |
lua_rawseti(L, -2, ++num_upvals); /* mt */ |
| 691 |
dasm_put(Dst, 443, num_upvals-1, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32)); |
| 692 |
break; |
| 693 |
|
| 694 |
case VOID_TYPE: |
| 695 |
lua_pop(L, 1); |
| 696 |
dasm_put(Dst, 525); |
| 697 |
break; |
| 698 |
|
| 699 |
case BOOL_TYPE: |
| 700 |
case INT8_TYPE: |
| 701 |
case INT16_TYPE: |
| 702 |
case INT32_TYPE: |
| 703 |
lua_pop(L, 1); |
| 704 |
if (mt->is_unsigned) { |
| 705 |
dasm_put(Dst, 544); |
| 706 |
} else { |
| 707 |
dasm_put(Dst, 563); |
| 708 |
} |
| 709 |
dasm_put(Dst, 582); |
| 710 |
break; |
| 711 |
|
| 712 |
case INT64_TYPE: |
| 713 |
lua_pop(L, 1); |
| 714 |
|
| 715 |
if (mt->is_unsigned) { |
| 716 |
dasm_put(Dst, 609); |
| 717 |
} else { |
| 718 |
dasm_put(Dst, 628); |
| 719 |
} |
| 720 |
|
| 721 |
dasm_put(Dst, 647); |
| 722 |
break; |
| 723 |
|
| 724 |
case INTPTR_TYPE: |
| 725 |
lua_pop(L, 1); |
| 726 |
dasm_put(Dst, 676); |
| 727 |
break; |
| 728 |
|
| 729 |
case FLOAT_TYPE: |
| 730 |
case DOUBLE_TYPE: |
| 731 |
lua_pop(L, 1); |
| 732 |
dasm_put(Dst, 723); |
| 733 |
if (mt->type == FLOAT_TYPE) { |
| 734 |
dasm_put(Dst, 766); |
| 735 |
} else { |
| 736 |
dasm_put(Dst, 774); |
| 737 |
} |
| 738 |
break; |
| 739 |
|
| 740 |
case COMPLEX_FLOAT_TYPE: |
| 741 |
lua_pop(L, 1); |
| 742 |
#if !defined HAVE_COMPLEX |
| 743 |
luaL_error(L, "ffi lib compiled without complex number support"); |
| 744 |
#endif |
| 745 |
/* on 64 bit complex floats are two floats packed into a double, |
| 746 |
* on 32 bit returned complex floats use eax and edx */ |
| 747 |
dasm_put(Dst, 782); |
| 748 |
break; |
| 749 |
|
| 750 |
case COMPLEX_DOUBLE_TYPE: |
| 751 |
lua_pop(L, 1); |
| 752 |
#if !defined HAVE_COMPLEX |
| 753 |
luaL_error(L, "ffi lib compiled without complex number support"); |
| 754 |
#endif |
| 755 |
/* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit |
| 756 |
* there is a hidden first parameter that points to 16 bytes where |
| 757 |
* the returned arg is stored which is popped by the called |
| 758 |
* function */ |
| 759 |
#if defined _WIN64 || defined __amd64__ |
| 760 |
dasm_put(Dst, 832); |
| 761 |
#else |
| 762 |
dasm_put(Dst, 895, hidden_arg_off); |
| 763 |
#endif |
| 764 |
break; |
| 765 |
|
| 766 |
default: |
| 767 |
luaL_error(L, "NYI: callback return type"); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
dasm_put(Dst, 944, x86_return_size(L, ct_usr, ct)); |
| 772 |
|
| 773 |
lua_pop(L, 1); /* upval table - already in registry */ |
| 774 |
assert(lua_gettop(L) == top); |
| 775 |
|
| 776 |
ct2.is_jitted = 1; |
| 777 |
pf = (cfunction*) push_cdata(L, ct_usr, &ct2); |
| 778 |
*pf = compile(Dst, L, NULL, ref); |
| 779 |
|
| 780 |
assert(lua_gettop(L) == top + 1); |
| 781 |
|
| 782 |
return *pf; |
| 783 |
} |
| 784 |
|
| 785 |
void compile_function(lua_State* L, cfunction func, int ct_usr, const struct ctype* ct) |
| 786 |
{ |
| 787 |
size_t i, nargs; |
| 788 |
int num_upvals; |
| 789 |
const struct ctype* mbr_ct; |
| 790 |
struct jit* Dst = get_jit(L); |
| 791 |
struct reg_alloc reg; |
| 792 |
void* p; |
| 793 |
int top = lua_gettop(L); |
| 794 |
int* perr = &Dst->last_errno; |
| 795 |
|
| 796 |
ct_usr = lua_absindex(L, ct_usr); |
| 797 |
|
| 798 |
memset(®, 0, sizeof(reg)); |
| 799 |
reg.off = 32 + REGISTER_STACK_SPACE(ct); |
| 800 |
|
| 801 |
dasm_setup(Dst, build_actionlist); |
| 802 |
|
| 803 |
p = push_cdata(L, ct_usr, ct); |
| 804 |
*(cfunction*) p = func; |
| 805 |
num_upvals = 1; |
| 806 |
|
| 807 |
nargs = lua_rawlen(L, ct_usr); |
| 808 |
|
| 809 |
if (ct->calling_convention != C_CALL && ct->has_var_arg) { |
| 810 |
luaL_error(L, "vararg is only allowed with the c calling convention"); |
| 811 |
} |
| 812 |
|
| 813 |
dasm_put(Dst, 957, nargs); |
| 814 |
if (!ct->has_var_arg) { |
| 815 |
dasm_put(Dst, 989, (unsigned int)((uintptr_t)(&"too few arguments")), (unsigned int)(((uintptr_t)(&"too few arguments"))>>32), (unsigned int)((uintptr_t)(&"too many arguments")), (unsigned int)(((uintptr_t)(&"too many arguments"))>>32)); |
| 816 |
} else { |
| 817 |
dasm_put(Dst, 1030, (unsigned int)((uintptr_t)(&"too few arguments")), (unsigned int)(((uintptr_t)(&"too few arguments"))>>32)); |
| 818 |
} |
| 819 |
|
| 820 |
dasm_put(Dst, 1050); |
| 821 |
|
| 822 |
/* no need to zero extend eax returned by lua_gettop to rax as x86-64 |
| 823 |
* preguarentees that the upper 32 bits will be zero */ |
| 824 |
dasm_put(Dst, 1053, 32 + REGISTER_STACK_SPACE(ct)); |
| 825 |
|
| 826 |
#if !defined _WIN64 && !defined __amd64__ |
| 827 |
/* Returned complex doubles require a hidden first parameter where the |
| 828 |
* data is stored, which is popped by the calling code. */ |
| 829 |
lua_rawgeti(L, ct_usr, 0); |
| 830 |
mbr_ct = (const struct ctype*) lua_touserdata(L, -1); |
| 831 |
if (!mbr_ct->pointers && !mbr_ct->is_reference && mbr_ct->type == COMPLEX_DOUBLE_TYPE) { |
| 832 |
/* we can allocate more space for arguments as long as no add_* |
| 833 |
* function has been called yet, mbr_ct will be added as an upvalue in |
| 834 |
* the return processing later */ |
| 835 |
dasm_put(Dst, 1066, (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32)); |
| 836 |
add_pointer(Dst, ct, ®); |
| 837 |
} |
| 838 |
lua_pop(L, 1); |
| 839 |
#endif |
| 840 |
|
| 841 |
for (i = 1; i <= nargs; i++) { |
| 842 |
lua_rawgeti(L, ct_usr, (int) i); |
| 843 |
mbr_ct = (const struct ctype*) lua_touserdata(L, -1); |
| 844 |
|
| 845 |
if (mbr_ct->pointers || mbr_ct->is_reference) { |
| 846 |
lua_getuservalue(L, -1); |
| 847 |
num_upvals += 2; |
| 848 |
dasm_put(Dst, 1090, (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals), i); |
| 849 |
add_pointer(Dst, ct, ®); |
| 850 |
} else { |
| 851 |
switch (mbr_ct->type) { |
| 852 |
case FUNCTION_PTR_TYPE: |
| 853 |
lua_getuservalue(L, -1); |
| 854 |
num_upvals += 2; |
| 855 |
dasm_put(Dst, 1110, (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals), i); |
| 856 |
add_pointer(Dst, ct, ®); |
| 857 |
break; |
| 858 |
|
| 859 |
case ENUM_TYPE: |
| 860 |
lua_getuservalue(L, -1); |
| 861 |
num_upvals += 2; |
| 862 |
dasm_put(Dst, 1130, (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals), i); |
| 863 |
add_int(Dst, ct, ®, 0); |
| 864 |
break; |
| 865 |
|
| 866 |
case INT8_TYPE: |
| 867 |
dasm_put(Dst, 1150, i); |
| 868 |
if (mbr_ct->is_unsigned) { |
| 869 |
dasm_put(Dst, 1162); |
| 870 |
} else { |
| 871 |
dasm_put(Dst, 1166); |
| 872 |
} |
| 873 |
add_int(Dst, ct, ®, 0); |
| 874 |
lua_pop(L, 1); |
| 875 |
break; |
| 876 |
|
| 877 |
case INT16_TYPE: |
| 878 |
dasm_put(Dst, 1150, i); |
| 879 |
if (mbr_ct->is_unsigned) { |
| 880 |
dasm_put(Dst, 1170); |
| 881 |
} else { |
| 882 |
dasm_put(Dst, 1174); |
| 883 |
} |
| 884 |
add_int(Dst, ct, ®, 0); |
| 885 |
lua_pop(L, 1); |
| 886 |
break; |
| 887 |
|
| 888 |
case BOOL_TYPE: |
| 889 |
dasm_put(Dst, 1178, i); |
| 890 |
add_int(Dst, ct, ®, 0); |
| 891 |
lua_pop(L, 1); |
| 892 |
break; |
| 893 |
|
| 894 |
case INT32_TYPE: |
| 895 |
if (mbr_ct->is_unsigned) { |
| 896 |
dasm_put(Dst, 1200, i); |
| 897 |
} else { |
| 898 |
dasm_put(Dst, 1150, i); |
| 899 |
} |
| 900 |
add_int(Dst, ct, ®, 0); |
| 901 |
lua_pop(L, 1); |
| 902 |
break; |
| 903 |
|
| 904 |
case INTPTR_TYPE: |
| 905 |
dasm_put(Dst, 1212, i); |
| 906 |
add_pointer(Dst, ct, ®); |
| 907 |
lua_pop(L, 1); |
| 908 |
break; |
| 909 |
|
| 910 |
case INT64_TYPE: |
| 911 |
if (mbr_ct->is_unsigned) { |
| 912 |
dasm_put(Dst, 1224, i); |
| 913 |
} else { |
| 914 |
dasm_put(Dst, 1236, i); |
| 915 |
} |
| 916 |
add_int(Dst, ct, ®, 1); |
| 917 |
lua_pop(L, 1); |
| 918 |
break; |
| 919 |
|
| 920 |
case DOUBLE_TYPE: |
| 921 |
dasm_put(Dst, 1248, i); |
| 922 |
add_float(Dst, ct, ®, 1); |
| 923 |
lua_pop(L, 1); |
| 924 |
break; |
| 925 |
|
| 926 |
case COMPLEX_DOUBLE_TYPE: |
| 927 |
/* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit |
| 928 |
* there is a hidden first parameter that points to 16 bytes where |
| 929 |
* the returned arg is stored (this is popped by the called |
| 930 |
* function) */ |
| 931 |
#if defined _WIN64 || defined __amd64__ |
| 932 |
dasm_put(Dst, 1260, i); |
| 933 |
add_float(Dst, ct, ®, 1); |
| 934 |
dasm_put(Dst, 1272); |
| 935 |
add_float(Dst, ct, ®, 1); |
| 936 |
#else |
| 937 |
dasm_put(Dst, 1278, reg.off, i); |
| 938 |
reg.off += 16; |
| 939 |
#endif |
| 940 |
lua_pop(L, 1); |
| 941 |
break; |
| 942 |
|
| 943 |
case FLOAT_TYPE: |
| 944 |
dasm_put(Dst, 1248, i); |
| 945 |
add_float(Dst, ct, ®, 0); |
| 946 |
lua_pop(L, 1); |
| 947 |
break; |
| 948 |
|
| 949 |
case COMPLEX_FLOAT_TYPE: |
| 950 |
#if defined _WIN64 || defined __amd64__ |
| 951 |
dasm_put(Dst, 1304, i); |
| 952 |
/* complex floats are two floats packed into a double */ |
| 953 |
add_float(Dst, ct, ®, 1); |
| 954 |
#else |
| 955 |
/* returned complex floats use eax and edx */ |
| 956 |
dasm_put(Dst, 1316, i); |
| 957 |
add_float(Dst, ct, ®, 0); |
| 958 |
dasm_put(Dst, 1334); |
| 959 |
add_float(Dst, ct, ®, 0); |
| 960 |
#endif |
| 961 |
lua_pop(L, 1); |
| 962 |
break; |
| 963 |
|
| 964 |
default: |
| 965 |
luaL_error(L, "NYI: call arg type"); |
| 966 |
} |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
if (ct->has_var_arg) { |
| 971 |
#ifdef _WIN64 |
| 972 |
if (reg.regs < MAX_REGISTERS(ct)) { |
| 973 |
assert(reg.regs == nargs); |
| 974 |
dasm_put(Dst, 1341, MAX_REGISTERS(ct), 32 + 8*MAX_REGISTERS(ct), MAX_REGISTERS(ct)+1, 32 + 8*(reg.regs), MAX_REGISTERS(ct), nargs+1, 32 + 8*(reg.regs), nargs+1); |
| 975 |
} else { |
| 976 |
dasm_put(Dst, 1432, reg.off, nargs+1); |
| 977 |
} |
| 978 |
|
| 979 |
for (i = nargs; i < MAX_REGISTERS(ct); i++) { |
| 980 |
reg.is_int[i] = reg.is_float[i] = 1; |
| 981 |
} |
| 982 |
reg.regs = MAX_REGISTERS(ct); |
| 983 |
#elif defined __amd64__ |
| 984 |
if (reg.floats < MAX_FLOAT_REGISTERS(ct)) { |
| 985 |
} |
| 986 |
|
| 987 |
if (reg.ints < MAX_INT_REGISTERS(ct)) { |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
reg.floats = MAX_FLOAT_REGISTERS(ct); |
| 992 |
reg.ints = MAX_INT_REGISTERS(ct); |
| 993 |
#else |
| 994 |
#endif |
| 995 |
} |
| 996 |
|
| 997 |
dasm_put(Dst, 1457, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 998 |
|
| 999 |
/* remove the stack space to call local functions */ |
| 1000 |
dasm_put(Dst, 1471); |
| 1001 |
|
| 1002 |
#ifdef _WIN64 |
| 1003 |
switch (reg.regs) { |
| 1004 |
case 4: |
| 1005 |
if (reg.is_float[3]) { |
| 1006 |
dasm_put(Dst, 1476, 8*3); |
| 1007 |
} |
| 1008 |
if (reg.is_int[3]) { |
| 1009 |
dasm_put(Dst, 1485, 8*3); |
| 1010 |
} |
| 1011 |
case 3: |
| 1012 |
if (reg.is_float[2]) { |
| 1013 |
dasm_put(Dst, 1492, 8*2); |
| 1014 |
} |
| 1015 |
if (reg.is_int[2]) { |
| 1016 |
dasm_put(Dst, 1501, 8*2); |
| 1017 |
} |
| 1018 |
case 2: |
| 1019 |
if (reg.is_float[1]) { |
| 1020 |
dasm_put(Dst, 1508, 8*1); |
| 1021 |
} |
| 1022 |
if (reg.is_int[1]) { |
| 1023 |
dasm_put(Dst, 1517, 8*1); |
| 1024 |
} |
| 1025 |
case 1: |
| 1026 |
if (reg.is_float[0]) { |
| 1027 |
dasm_put(Dst, 1524); |
| 1028 |
} |
| 1029 |
if (reg.is_int[0]) { |
| 1030 |
dasm_put(Dst, 1531); |
| 1031 |
} |
| 1032 |
case 0: |
| 1033 |
break; |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* don't remove the space for the registers as we need 32 bytes of register overflow space */ |
| 1037 |
assert(REGISTER_STACK_SPACE(ct) == 32); |
| 1038 |
|
| 1039 |
#elif defined __amd64__ |
| 1040 |
switch (reg.floats) { |
| 1041 |
case 8: |
| 1042 |
case 7: |
| 1043 |
case 6: |
| 1044 |
case 5: |
| 1045 |
case 4: |
| 1046 |
case 3: |
| 1047 |
case 2: |
| 1048 |
case 1: |
| 1049 |
case 0: |
| 1050 |
break; |
| 1051 |
} |
| 1052 |
|
| 1053 |
switch (reg.ints) { |
| 1054 |
case 6: |
| 1055 |
case 5: |
| 1056 |
case 4: |
| 1057 |
case 3: |
| 1058 |
case 2: |
| 1059 |
case 1: |
| 1060 |
case 0: |
| 1061 |
break; |
| 1062 |
} |
| 1063 |
|
| 1064 |
#else |
| 1065 |
if (ct->calling_convention == FAST_CALL) { |
| 1066 |
switch (reg.ints) { |
| 1067 |
case 2: |
| 1068 |
case 1: |
| 1069 |
case 0: |
| 1070 |
break; |
| 1071 |
} |
| 1072 |
|
| 1073 |
} |
| 1074 |
#endif |
| 1075 |
|
| 1076 |
#ifdef __amd64__ |
| 1077 |
if (ct->has_var_arg) { |
| 1078 |
/* al stores an upper limit on the number of float register, note that |
| 1079 |
* its allowed to be more than the actual number of float registers used as |
| 1080 |
* long as its 0-8 */ |
| 1081 |
} |
| 1082 |
#endif |
| 1083 |
|
| 1084 |
dasm_put(Dst, 1536); |
| 1085 |
|
| 1086 |
/* note on windows X86 the stack may be only aligned to 4 (stdcall will |
| 1087 |
* have popped a multiple of 4 bytes), but we don't need 16 byte alignment on |
| 1088 |
* that platform |
| 1089 |
*/ |
| 1090 |
|
| 1091 |
lua_rawgeti(L, ct_usr, 0); |
| 1092 |
mbr_ct = (const struct ctype*) lua_touserdata(L, -1); |
| 1093 |
|
| 1094 |
if (mbr_ct->pointers || mbr_ct->is_reference || mbr_ct->type == INTPTR_TYPE) { |
| 1095 |
lua_getuservalue(L, -1); |
| 1096 |
num_upvals += 2; |
| 1097 |
dasm_put(Dst, 1546, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals)); |
| 1098 |
|
| 1099 |
} else { |
| 1100 |
switch (mbr_ct->type) { |
| 1101 |
case FUNCTION_PTR_TYPE: |
| 1102 |
lua_getuservalue(L, -1); |
| 1103 |
num_upvals += 2; |
| 1104 |
dasm_put(Dst, 1546, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals)); |
| 1105 |
break; |
| 1106 |
|
| 1107 |
case INT64_TYPE: |
| 1108 |
#if LUA_VERSION_NUM >= 503 |
| 1109 |
lua_pop(L, 1); |
| 1110 |
if (mbr_ct->is_unsigned) { |
| 1111 |
dasm_put(Dst, 1606, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1112 |
} else { |
| 1113 |
dasm_put(Dst, 1606, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1114 |
} |
| 1115 |
#else |
| 1116 |
num_upvals++; |
| 1117 |
dasm_put(Dst, 1658, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32)); |
| 1118 |
#endif |
| 1119 |
break; |
| 1120 |
|
| 1121 |
case COMPLEX_FLOAT_TYPE: |
| 1122 |
lua_getuservalue(L, -1); |
| 1123 |
num_upvals += 2; |
| 1124 |
dasm_put(Dst, 1721, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals)); |
| 1125 |
break; |
| 1126 |
|
| 1127 |
case COMPLEX_DOUBLE_TYPE: |
| 1128 |
lua_getuservalue(L, -1); |
| 1129 |
num_upvals += 2; |
| 1130 |
dasm_put(Dst, 1782, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals)); |
| 1131 |
break; |
| 1132 |
|
| 1133 |
case VOID_TYPE: |
| 1134 |
lua_pop(L, 1); |
| 1135 |
dasm_put(Dst, 1858, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1136 |
break; |
| 1137 |
|
| 1138 |
case BOOL_TYPE: |
| 1139 |
lua_pop(L, 1); |
| 1140 |
dasm_put(Dst, 1890, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1141 |
break; |
| 1142 |
|
| 1143 |
case INT8_TYPE: |
| 1144 |
lua_pop(L, 1); |
| 1145 |
if (mbr_ct->is_unsigned) { |
| 1146 |
dasm_put(Dst, 1162); |
| 1147 |
} else { |
| 1148 |
dasm_put(Dst, 1166); |
| 1149 |
} |
| 1150 |
dasm_put(Dst, 1943, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1151 |
break; |
| 1152 |
|
| 1153 |
case INT16_TYPE: |
| 1154 |
lua_pop(L, 1); |
| 1155 |
if (mbr_ct->is_unsigned) { |
| 1156 |
dasm_put(Dst, 1170); |
| 1157 |
} else { |
| 1158 |
dasm_put(Dst, 1174); |
| 1159 |
} |
| 1160 |
dasm_put(Dst, 1943, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1161 |
break; |
| 1162 |
|
| 1163 |
case INT32_TYPE: |
| 1164 |
case ENUM_TYPE: |
| 1165 |
lua_pop(L, 1); |
| 1166 |
if (mbr_ct->is_unsigned) { |
| 1167 |
dasm_put(Dst, 1993, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1168 |
} else { |
| 1169 |
dasm_put(Dst, 1943, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1170 |
} |
| 1171 |
break; |
| 1172 |
|
| 1173 |
case FLOAT_TYPE: |
| 1174 |
lua_pop(L, 1); |
| 1175 |
dasm_put(Dst, 2043, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1176 |
break; |
| 1177 |
|
| 1178 |
case DOUBLE_TYPE: |
| 1179 |
lua_pop(L, 1); |
| 1180 |
dasm_put(Dst, 2048, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32)); |
| 1181 |
break; |
| 1182 |
|
| 1183 |
default: |
| 1184 |
luaL_error(L, "NYI: call return type"); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
assert(lua_gettop(L) == top + num_upvals); |
| 1189 |
{ |
| 1190 |
cfunction f = compile(Dst, L, func, LUA_NOREF); |
| 1191 |
/* add a callback as an upval so that the jitted code gets cleaned up when |
| 1192 |
* the function gets gc'd */ |
| 1193 |
push_callback(L, f, func); |
| 1194 |
lua_pushcclosure(L, (lua_CFunction) f, num_upvals+1); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|