as_callfunc_x64_gcc.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. /*
  24. * Implements the AMD64 calling convention for gcc-based 64bit Unices
  25. *
  26. * Author: Ionut "gargltk" Leonte <[email protected]>
  27. *
  28. * Initial author: niteice
  29. *
  30. * Added support for functor methods by Jordi Oliveras Rovira in April, 2014.
  31. */
  32. // Useful references for the System V AMD64 ABI:
  33. // http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64/
  34. // http://math-atlas.sourceforge.net/devel/assembly/abi_sysV_amd64.pdf
  35. #include "as_config.h"
  36. #ifndef AS_MAX_PORTABILITY
  37. #ifdef AS_X64_GCC
  38. #include "as_scriptengine.h"
  39. #include "as_texts.h"
  40. #include "as_context.h"
  41. BEGIN_AS_NAMESPACE
  42. enum argTypes { x64INTARG = 0, x64FLOATARG = 1 };
  43. typedef asQWORD ( *funcptr_t )( void );
  44. #define X64_MAX_ARGS 32
  45. #define MAX_CALL_INT_REGISTERS 6
  46. #define MAX_CALL_SSE_REGISTERS 8
  47. #define X64_CALLSTACK_SIZE ( X64_MAX_ARGS + MAX_CALL_SSE_REGISTERS + 3 )
  48. // Note to self: Always remember to inform the used registers on the clobber line,
  49. // so that the gcc optimizer doesn't try to use them for other things
  50. static asQWORD __attribute__((noinline)) X64_CallFunction(const asQWORD *args, int cnt, funcptr_t func, asQWORD &retQW2, bool returnFloat)
  51. {
  52. // Need to flag the variable as volatile so the compiler doesn't optimize out the variable
  53. volatile asQWORD retQW1 = 0;
  54. // Reference: http://www.x86-64.org/documentation/abi.pdf
  55. __asm__ __volatile__ (
  56. " movq %0, %%rcx \n" // rcx = cnt
  57. " movq %1, %%r10 \n" // r10 = args
  58. " movq %2, %%r11 \n" // r11 = func
  59. // Backup stack pointer in R15 that is guaranteed to maintain its value over function calls
  60. " movq %%rsp, %%r15 \n"
  61. // Make sure the stack unwind logic knows we've backed up the stack pointer in register r15
  62. " .cfi_def_cfa_register r15 \n"
  63. // Skip the first 128 bytes on the stack frame, called "red zone",
  64. // that might be used by the compiler to store temporary values
  65. " sub $128, %%rsp \n"
  66. // Make sure the stack pointer will be aligned to 16 bytes when the function is called
  67. " movq %%rcx, %%rdx \n"
  68. " salq $3, %%rdx \n"
  69. " movq %%rsp, %%rax \n"
  70. " sub %%rdx, %%rax \n"
  71. " and $15, %%rax \n"
  72. " sub %%rax, %%rsp \n"
  73. // Push the stack parameters, i.e. the arguments that won't be loaded into registers
  74. " movq %%rcx, %%rsi \n"
  75. " testl %%esi, %%esi \n"
  76. " jle endstack \n"
  77. " subl $1, %%esi \n"
  78. " xorl %%edx, %%edx \n"
  79. " leaq 8(, %%rsi, 8), %%rcx \n"
  80. "loopstack: \n"
  81. " movq 112(%%r10, %%rdx), %%rax \n"
  82. " pushq %%rax \n"
  83. " addq $8, %%rdx \n"
  84. " cmpq %%rcx, %%rdx \n"
  85. " jne loopstack \n"
  86. "endstack: \n"
  87. // Populate integer and floating point parameters
  88. " movq %%r10, %%rax \n"
  89. " mov (%%rax), %%rdi \n"
  90. " mov 8(%%rax), %%rsi \n"
  91. " mov 16(%%rax), %%rdx \n"
  92. " mov 24(%%rax), %%rcx \n"
  93. " mov 32(%%rax), %%r8 \n"
  94. " mov 40(%%rax), %%r9 \n"
  95. " add $48, %%rax \n"
  96. " movsd (%%rax), %%xmm0 \n"
  97. " movsd 8(%%rax), %%xmm1 \n"
  98. " movsd 16(%%rax), %%xmm2 \n"
  99. " movsd 24(%%rax), %%xmm3 \n"
  100. " movsd 32(%%rax), %%xmm4 \n"
  101. " movsd 40(%%rax), %%xmm5 \n"
  102. " movsd 48(%%rax), %%xmm6 \n"
  103. " movsd 56(%%rax), %%xmm7 \n"
  104. // Call the function
  105. " call *%%r11 \n"
  106. // Restore stack pointer
  107. " mov %%r15, %%rsp \n"
  108. // Inform the stack unwind logic that the stack pointer has been restored
  109. " .cfi_def_cfa_register rsp \n"
  110. // Put return value in retQW1 and retQW2, using either RAX:RDX or XMM0:XMM1 depending on type of return value
  111. " movl %5, %%ecx \n"
  112. " testb %%cl, %%cl \n"
  113. " je intret \n"
  114. " lea %3, %%rax \n"
  115. " movq %%xmm0, (%%rax) \n"
  116. " lea %4, %%rdx \n"
  117. " movq %%xmm1, (%%rdx) \n"
  118. " jmp endcall \n"
  119. "intret: \n"
  120. " movq %%rax, %3 \n"
  121. " movq %%rdx, %4 \n"
  122. "endcall: \n"
  123. : : "r" ((asQWORD)cnt), "r" (args), "r" (func), "m" (retQW1), "m" (retQW2), "m" (returnFloat)
  124. : "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
  125. "%rdi", "%rsi", "%rax", "%rdx", "%rcx", "%r8", "%r9", "%r10", "%r11", "%r15");
  126. return retQW1;
  127. }
  128. // returns true if the given parameter is a 'variable argument'
  129. static inline bool IsVariableArgument( asCDataType type )
  130. {
  131. return ( type.GetTokenType() == ttQuestion ) ? true : false;
  132. }
  133. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &retQW2)
  134. {
  135. asCScriptEngine *engine = context->m_engine;
  136. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  137. int callConv = sysFunc->callConv;
  138. asQWORD retQW = 0;
  139. asDWORD *stack_pointer = args;
  140. funcptr_t *vftable = NULL;
  141. int totalArgumentCount = 0;
  142. int n = 0;
  143. int param_post = 0;
  144. int argIndex = 0;
  145. funcptr_t func = (funcptr_t)sysFunc->func;
  146. if( sysFunc->hostReturnInMemory )
  147. {
  148. // The return is made in memory
  149. callConv++;
  150. }
  151. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  152. // Unpack the two object pointers
  153. void **objectsPtrs = (void**)obj;
  154. void *secondObject = objectsPtrs[1];
  155. obj = objectsPtrs[0];
  156. // param_post has value 2 if is an THISCALL_OBJLAST
  157. #endif
  158. #ifdef AS_NO_THISCALL_FUNCTOR_METHOD
  159. // Determine the real function pointer in case of virtual method
  160. if ( obj && ( callConv == ICC_VIRTUAL_THISCALL || callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM ) )
  161. #else
  162. if ( obj && ( callConv == ICC_VIRTUAL_THISCALL ||
  163. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM ||
  164. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
  165. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM ||
  166. callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
  167. callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM) )
  168. #endif
  169. {
  170. vftable = *((funcptr_t**)obj);
  171. func = vftable[FuncPtrToUInt(asFUNCTION_t(func)) >> 3];
  172. }
  173. // Determine the type of the arguments, and prepare the input array for the X64_CallFunction
  174. asQWORD paramBuffer[X64_CALLSTACK_SIZE] = { 0 };
  175. asBYTE argsType[X64_CALLSTACK_SIZE] = { 0 };
  176. switch ( callConv )
  177. {
  178. case ICC_CDECL_RETURNINMEM:
  179. case ICC_STDCALL_RETURNINMEM:
  180. {
  181. paramBuffer[0] = (asPWORD)retPointer;
  182. argsType[0] = x64INTARG;
  183. argIndex = 1;
  184. break;
  185. }
  186. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  187. case ICC_THISCALL_OBJLAST:
  188. case ICC_VIRTUAL_THISCALL_OBJLAST:
  189. param_post = 2;
  190. #endif
  191. case ICC_THISCALL:
  192. case ICC_VIRTUAL_THISCALL:
  193. case ICC_CDECL_OBJFIRST:
  194. {
  195. paramBuffer[0] = (asPWORD)obj;
  196. argsType[0] = x64INTARG;
  197. argIndex = 1;
  198. break;
  199. }
  200. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  201. case ICC_THISCALL_OBJLAST_RETURNINMEM:
  202. case ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM:
  203. param_post = 2;
  204. #endif
  205. case ICC_THISCALL_RETURNINMEM:
  206. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  207. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  208. {
  209. paramBuffer[0] = (asPWORD)retPointer;
  210. paramBuffer[1] = (asPWORD)obj;
  211. argsType[0] = x64INTARG;
  212. argsType[1] = x64INTARG;
  213. argIndex = 2;
  214. break;
  215. }
  216. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  217. case ICC_THISCALL_OBJFIRST:
  218. case ICC_VIRTUAL_THISCALL_OBJFIRST:
  219. {
  220. paramBuffer[0] = (asPWORD)obj;
  221. paramBuffer[1] = (asPWORD)secondObject;
  222. argsType[0] = x64INTARG;
  223. argsType[1] = x64INTARG;
  224. argIndex = 2;
  225. break;
  226. }
  227. case ICC_THISCALL_OBJFIRST_RETURNINMEM:
  228. case ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM:
  229. {
  230. paramBuffer[0] = (asPWORD)retPointer;
  231. paramBuffer[1] = (asPWORD)obj;
  232. paramBuffer[2] = (asPWORD)secondObject;
  233. argsType[0] = x64INTARG;
  234. argsType[1] = x64INTARG;
  235. argsType[2] = x64INTARG;
  236. argIndex = 3;
  237. break;
  238. }
  239. #endif
  240. case ICC_CDECL_OBJLAST:
  241. param_post = 1;
  242. break;
  243. case ICC_CDECL_OBJLAST_RETURNINMEM:
  244. {
  245. paramBuffer[0] = (asPWORD)retPointer;
  246. argsType[0] = x64INTARG;
  247. argIndex = 1;
  248. param_post = 1;
  249. break;
  250. }
  251. }
  252. int argumentCount = ( int )descr->parameterTypes.GetLength();
  253. for( int a = 0; a < argumentCount; ++a )
  254. {
  255. const asCDataType &parmType = descr->parameterTypes[a];
  256. if( parmType.IsFloatType() && !parmType.IsReference() )
  257. {
  258. argsType[argIndex] = x64FLOATARG;
  259. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(float));
  260. argIndex++;
  261. stack_pointer++;
  262. }
  263. else if( parmType.IsDoubleType() && !parmType.IsReference() )
  264. {
  265. argsType[argIndex] = x64FLOATARG;
  266. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(double));
  267. argIndex++;
  268. stack_pointer += 2;
  269. }
  270. else if( IsVariableArgument( parmType ) )
  271. {
  272. // The variable args are really two, one pointer and one type id
  273. argsType[argIndex] = x64INTARG;
  274. argsType[argIndex+1] = x64INTARG;
  275. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(void*));
  276. memcpy(paramBuffer + argIndex + 1, stack_pointer + 2, sizeof(asDWORD));
  277. argIndex += 2;
  278. stack_pointer += 3;
  279. }
  280. else if( parmType.IsPrimitive() ||
  281. parmType.IsReference() ||
  282. parmType.IsObjectHandle() )
  283. {
  284. argsType[argIndex] = x64INTARG;
  285. if( parmType.GetSizeOnStackDWords() == 1 )
  286. {
  287. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(asDWORD));
  288. stack_pointer++;
  289. }
  290. else
  291. {
  292. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(asQWORD));
  293. stack_pointer += 2;
  294. }
  295. argIndex++;
  296. }
  297. else
  298. {
  299. // An object is being passed by value
  300. if( (parmType.GetObjectType()->flags & COMPLEX_MASK) ||
  301. parmType.GetSizeInMemoryDWords() > 4 )
  302. {
  303. // Copy the address of the object
  304. argsType[argIndex] = x64INTARG;
  305. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(asQWORD));
  306. argIndex++;
  307. }
  308. else if( (parmType.GetObjectType()->flags & asOBJ_APP_CLASS_ALLINTS) ||
  309. (parmType.GetObjectType()->flags & asOBJ_APP_PRIMITIVE) )
  310. {
  311. // Copy the value of the object
  312. if( parmType.GetSizeInMemoryDWords() > 2 )
  313. {
  314. argsType[argIndex] = x64INTARG;
  315. argsType[argIndex+1] = x64INTARG;
  316. memcpy(paramBuffer + argIndex, *(asDWORD**)stack_pointer, parmType.GetSizeInMemoryBytes());
  317. argIndex += 2;
  318. }
  319. else
  320. {
  321. argsType[argIndex] = x64INTARG;
  322. memcpy(paramBuffer + argIndex, *(asDWORD**)stack_pointer, parmType.GetSizeInMemoryBytes());
  323. argIndex++;
  324. }
  325. // Delete the original memory
  326. engine->CallFree(*(void**)stack_pointer);
  327. }
  328. else if( (parmType.GetObjectType()->flags & asOBJ_APP_CLASS_ALLFLOATS) ||
  329. (parmType.GetObjectType()->flags & asOBJ_APP_FLOAT) )
  330. {
  331. // Copy the value of the object
  332. if( parmType.GetSizeInMemoryDWords() > 2 )
  333. {
  334. argsType[argIndex] = x64FLOATARG;
  335. argsType[argIndex+1] = x64FLOATARG;
  336. memcpy(paramBuffer + argIndex, *(asDWORD**)stack_pointer, parmType.GetSizeInMemoryBytes());
  337. argIndex += 2;
  338. }
  339. else
  340. {
  341. argsType[argIndex] = x64FLOATARG;
  342. memcpy(paramBuffer + argIndex, *(asDWORD**)stack_pointer, parmType.GetSizeInMemoryBytes());
  343. argIndex++;
  344. }
  345. // Delete the original memory
  346. engine->CallFree(*(void**)stack_pointer);
  347. }
  348. stack_pointer += 2;
  349. }
  350. }
  351. // For the CDECL_OBJ_LAST calling convention we need to add the object pointer as the last argument
  352. if( param_post )
  353. {
  354. #ifdef AS_NO_THISCALL_FUNCTOR_METHOD
  355. paramBuffer[argIndex] = (asPWORD)obj;
  356. #else
  357. paramBuffer[argIndex] = (asPWORD)(param_post > 1 ? secondObject : obj);
  358. #endif
  359. argsType[argIndex] = x64INTARG;
  360. argIndex++;
  361. }
  362. totalArgumentCount = argIndex;
  363. /*
  364. * Q: WTF is going on here !?
  365. *
  366. * A: The idea is to pre-arange the parameters so that X64_CallFunction() can do
  367. * it's little magic which must work regardless of how the compiler decides to
  368. * allocate registers. Basically:
  369. * - the first MAX_CALL_INT_REGISTERS entries in tempBuff will
  370. * contain the values/types of the x64INTARG parameters - that is the ones who
  371. * go into the registers. If the function has less then MAX_CALL_INT_REGISTERS
  372. * integer parameters then the last entries will be set to 0
  373. * - the next MAX_CALL_SSE_REGISTERS entries will contain the float/double arguments
  374. * that go into the floating point registers. If the function has less than
  375. * MAX_CALL_SSE_REGISTERS floating point parameters then the last entries will
  376. * be set to 0
  377. * - index MAX_CALL_INT_REGISTERS + MAX_CALL_SSE_REGISTERS marks the start of the
  378. * parameters which will get passed on the stack. These are added to the array
  379. * in reverse order so that X64_CallFunction() can simply push them to the stack
  380. * without the need to perform further tests
  381. */
  382. asQWORD tempBuff[X64_CALLSTACK_SIZE] = { 0 };
  383. asBYTE argsSet[X64_CALLSTACK_SIZE] = { 0 };
  384. int used_int_regs = 0;
  385. int used_sse_regs = 0;
  386. int used_stack_args = 0;
  387. int idx = 0;
  388. for ( n = 0; ( n < totalArgumentCount ) && ( used_int_regs < MAX_CALL_INT_REGISTERS ); n++ )
  389. {
  390. if ( argsType[n] == x64INTARG )
  391. {
  392. argsSet[n] = 1;
  393. tempBuff[idx++] = paramBuffer[n];
  394. used_int_regs++;
  395. }
  396. }
  397. idx = MAX_CALL_INT_REGISTERS;
  398. for ( n = 0; ( n < totalArgumentCount ) && ( used_sse_regs < MAX_CALL_SSE_REGISTERS ); n++ )
  399. {
  400. if ( argsType[n] == x64FLOATARG )
  401. {
  402. argsSet[n] = 1;
  403. tempBuff[idx++] = paramBuffer[n];
  404. used_sse_regs++;
  405. }
  406. }
  407. idx = MAX_CALL_INT_REGISTERS + MAX_CALL_SSE_REGISTERS;
  408. for ( n = totalArgumentCount - 1; n >= 0; n-- )
  409. {
  410. if ( !argsSet[n] )
  411. {
  412. tempBuff[idx++] = paramBuffer[n];
  413. used_stack_args++;
  414. }
  415. }
  416. retQW = X64_CallFunction( tempBuff, used_stack_args, func, retQW2, sysFunc->hostReturnFloat );
  417. return retQW;
  418. }
  419. END_AS_NAMESPACE
  420. #endif // AS_X64_GCC
  421. #endif // AS_MAX_PORTABILITY