as_callfunc_x64_gcc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 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. #include "as_config.h"
  31. #ifndef AS_MAX_PORTABILITY
  32. #ifdef AS_X64_GCC
  33. #include "as_scriptengine.h"
  34. #include "as_texts.h"
  35. BEGIN_AS_NAMESPACE
  36. enum argTypes { x64INTARG = 0, x64FLOATARG = 1 };
  37. typedef asQWORD ( *funcptr_t )( void );
  38. #define X64_MAX_ARGS 32
  39. #define MAX_CALL_INT_REGISTERS 6
  40. #define MAX_CALL_SSE_REGISTERS 8
  41. #define X64_CALLSTACK_SIZE ( X64_MAX_ARGS + MAX_CALL_SSE_REGISTERS + 3 )
  42. // Note to self: Always remember to inform the used registers on the clobber line,
  43. // so that the gcc optimizer doesn't try to use them for other things
  44. static asQWORD __attribute__((noinline)) X64_CallFunction(const asQWORD *args, int cnt, funcptr_t func, asQWORD &retQW2, bool returnFloat)
  45. {
  46. // Need to flag the variable as volatile so the compiler doesn't optimize out the variable
  47. volatile asQWORD retQW1 = 0;
  48. // Reference: http://www.x86-64.org/documentation/abi.pdf
  49. __asm__ __volatile__ (
  50. " movq %0, %%rcx \n" // rcx = cnt
  51. " movq %1, %%r10 \n" // r10 = args
  52. " movq %2, %%r11 \n" // r11 = func
  53. // Backup stack pointer in R15 that is guaranteed to maintain its value over function calls
  54. " movq %%rsp, %%r15 \n"
  55. // Skip the first 128 bytes on the stack frame, called "red zone",
  56. // that might be used by the compiler to store temporary values
  57. " sub $128, %%rsp \n"
  58. // Make sure the stack pointer will be aligned to 16 bytes when the function is called
  59. " movq %%rcx, %%rdx \n"
  60. " salq $3, %%rdx \n"
  61. " movq %%rsp, %%rax \n"
  62. " sub %%rdx, %%rax \n"
  63. " and $15, %%rax \n"
  64. " sub %%rax, %%rsp \n"
  65. // Push the stack parameters, i.e. the arguments that won't be loaded into registers
  66. " movq %%rcx, %%rsi \n"
  67. " testl %%esi, %%esi \n"
  68. " jle endstack \n"
  69. " subl $1, %%esi \n"
  70. " xorl %%edx, %%edx \n"
  71. " leaq 8(, %%rsi, 8), %%rcx \n"
  72. "loopstack: \n"
  73. " movq 112(%%r10, %%rdx), %%rax \n"
  74. " pushq %%rax \n"
  75. " addq $8, %%rdx \n"
  76. " cmpq %%rcx, %%rdx \n"
  77. " jne loopstack \n"
  78. "endstack: \n"
  79. // Populate integer and floating point parameters
  80. " movq %%r10, %%rax \n"
  81. " mov (%%rax), %%rdi \n"
  82. " mov 8(%%rax), %%rsi \n"
  83. " mov 16(%%rax), %%rdx \n"
  84. " mov 24(%%rax), %%rcx \n"
  85. " mov 32(%%rax), %%r8 \n"
  86. " mov 40(%%rax), %%r9 \n"
  87. " add $48, %%rax \n"
  88. " movsd (%%rax), %%xmm0 \n"
  89. " movsd 8(%%rax), %%xmm1 \n"
  90. " movsd 16(%%rax), %%xmm2 \n"
  91. " movsd 24(%%rax), %%xmm3 \n"
  92. " movsd 32(%%rax), %%xmm4 \n"
  93. " movsd 40(%%rax), %%xmm5 \n"
  94. " movsd 48(%%rax), %%xmm6 \n"
  95. " movsd 56(%%rax), %%xmm7 \n"
  96. // Call the function
  97. " call *%%r11 \n"
  98. // Restore stack pointer
  99. " mov %%r15, %%rsp \n"
  100. // Put return value in retQW1 and retQW2, using either RAX:RDX or XMM0:XMM1 depending on type of return value
  101. " movl %5, %%ecx \n"
  102. " testb %%cl, %%cl \n"
  103. " je intret \n"
  104. " lea %3, %%rax \n"
  105. " movq %%xmm0, (%%rax) \n"
  106. " lea %4, %%rdx \n"
  107. " movq %%xmm1, (%%rdx) \n"
  108. " jmp endcall \n"
  109. "intret: \n"
  110. " movq %%rax, %3 \n"
  111. " movq %%rdx, %4 \n"
  112. "endcall: \n"
  113. : : "r" ((asQWORD)cnt), "r" (args), "r" (func), "m" (retQW1), "m" (retQW2), "m" (returnFloat)
  114. : "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
  115. "%rdi", "%rsi", "%rax", "%rdx", "%rcx", "%r8", "%r9", "%r10", "%r11", "%r15");
  116. return retQW1;
  117. }
  118. // returns true if the given parameter is a 'variable argument'
  119. static inline bool IsVariableArgument( asCDataType type )
  120. {
  121. return ( type.GetTokenType() == ttQuestion ) ? true : false;
  122. }
  123. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &retQW2)
  124. {
  125. asCScriptEngine *engine = context->m_engine;
  126. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  127. int callConv = sysFunc->callConv;
  128. asQWORD retQW = 0;
  129. asDWORD *stack_pointer = args;
  130. funcptr_t *vftable = NULL;
  131. int totalArgumentCount = 0;
  132. int n = 0;
  133. int param_post = 0;
  134. int argIndex = 0;
  135. funcptr_t func = (funcptr_t)sysFunc->func;
  136. if( sysFunc->hostReturnInMemory )
  137. {
  138. // The return is made in memory
  139. callConv++;
  140. }
  141. // Determine the real function pointer in case of virtual method
  142. if ( obj && ( callConv == ICC_VIRTUAL_THISCALL || callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM ) )
  143. {
  144. vftable = *((funcptr_t**)obj);
  145. func = vftable[FuncPtrToUInt(asFUNCTION_t(func)) >> 3];
  146. }
  147. // Determine the type of the arguments, and prepare the input array for the X64_CallFunction
  148. asQWORD paramBuffer[X64_CALLSTACK_SIZE] = { 0 };
  149. asBYTE argsType[X64_CALLSTACK_SIZE] = { 0 };
  150. switch ( callConv )
  151. {
  152. case ICC_CDECL_RETURNINMEM:
  153. case ICC_STDCALL_RETURNINMEM:
  154. {
  155. paramBuffer[0] = (asPWORD)retPointer;
  156. argsType[0] = x64INTARG;
  157. argIndex = 1;
  158. break;
  159. }
  160. case ICC_THISCALL:
  161. case ICC_VIRTUAL_THISCALL:
  162. case ICC_CDECL_OBJFIRST:
  163. {
  164. paramBuffer[0] = (asPWORD)obj;
  165. argsType[0] = x64INTARG;
  166. argIndex = 1;
  167. break;
  168. }
  169. case ICC_THISCALL_RETURNINMEM:
  170. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  171. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  172. {
  173. paramBuffer[0] = (asPWORD)retPointer;
  174. paramBuffer[1] = (asPWORD)obj;
  175. argsType[0] = x64INTARG;
  176. argsType[1] = x64INTARG;
  177. argIndex = 2;
  178. break;
  179. }
  180. case ICC_CDECL_OBJLAST:
  181. param_post = 1;
  182. break;
  183. case ICC_CDECL_OBJLAST_RETURNINMEM:
  184. {
  185. paramBuffer[0] = (asPWORD)retPointer;
  186. argsType[0] = x64INTARG;
  187. argIndex = 1;
  188. param_post = 1;
  189. break;
  190. }
  191. }
  192. int argumentCount = ( int )descr->parameterTypes.GetLength();
  193. for( int a = 0; a < argumentCount; ++a )
  194. {
  195. const asCDataType &parmType = descr->parameterTypes[a];
  196. if( parmType.IsFloatType() && !parmType.IsReference() )
  197. {
  198. argsType[argIndex] = x64FLOATARG;
  199. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(float));
  200. argIndex++;
  201. stack_pointer++;
  202. }
  203. else if( parmType.IsDoubleType() && !parmType.IsReference() )
  204. {
  205. argsType[argIndex] = x64FLOATARG;
  206. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(double));
  207. argIndex++;
  208. stack_pointer += 2;
  209. }
  210. else if( IsVariableArgument( parmType ) )
  211. {
  212. // The variable args are really two, one pointer and one type id
  213. argsType[argIndex] = x64INTARG;
  214. argsType[argIndex+1] = x64INTARG;
  215. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(void*));
  216. memcpy(paramBuffer + argIndex + 1, stack_pointer + 2, sizeof(asDWORD));
  217. argIndex += 2;
  218. stack_pointer += 3;
  219. }
  220. else if( parmType.IsPrimitive() ||
  221. parmType.IsReference() ||
  222. parmType.IsObjectHandle() )
  223. {
  224. argsType[argIndex] = x64INTARG;
  225. if( parmType.GetSizeOnStackDWords() == 1 )
  226. {
  227. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(asDWORD));
  228. stack_pointer++;
  229. }
  230. else
  231. {
  232. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(asQWORD));
  233. stack_pointer += 2;
  234. }
  235. argIndex++;
  236. }
  237. else
  238. {
  239. // An object is being passed by value
  240. if( (parmType.GetObjectType()->flags & COMPLEX_MASK) ||
  241. parmType.GetSizeInMemoryDWords() > 4 )
  242. {
  243. // Copy the address of the object
  244. argsType[argIndex] = x64INTARG;
  245. memcpy(paramBuffer + argIndex, stack_pointer, sizeof(asQWORD));
  246. argIndex++;
  247. }
  248. else if( (parmType.GetObjectType()->flags & asOBJ_APP_CLASS_ALLINTS) ||
  249. (parmType.GetObjectType()->flags & asOBJ_APP_PRIMITIVE) )
  250. {
  251. // Copy the value of the object
  252. if( parmType.GetSizeInMemoryDWords() > 2 )
  253. {
  254. argsType[argIndex] = x64INTARG;
  255. argsType[argIndex+1] = x64INTARG;
  256. memcpy(paramBuffer + argIndex, *(asDWORD**)stack_pointer, parmType.GetSizeInMemoryBytes());
  257. argIndex += 2;
  258. }
  259. else
  260. {
  261. argsType[argIndex] = x64INTARG;
  262. memcpy(paramBuffer + argIndex, *(asDWORD**)stack_pointer, parmType.GetSizeInMemoryBytes());
  263. argIndex++;
  264. }
  265. // Delete the original memory
  266. engine->CallFree(*(void**)stack_pointer);
  267. }
  268. else if( (parmType.GetObjectType()->flags & asOBJ_APP_CLASS_ALLFLOATS) ||
  269. (parmType.GetObjectType()->flags & asOBJ_APP_FLOAT) )
  270. {
  271. // Copy the value of the object
  272. if( parmType.GetSizeInMemoryDWords() > 2 )
  273. {
  274. argsType[argIndex] = x64FLOATARG;
  275. argsType[argIndex+1] = x64FLOATARG;
  276. memcpy(paramBuffer + argIndex, *(asDWORD**)stack_pointer, parmType.GetSizeInMemoryBytes());
  277. argIndex += 2;
  278. }
  279. else
  280. {
  281. argsType[argIndex] = x64FLOATARG;
  282. memcpy(paramBuffer + argIndex, *(asDWORD**)stack_pointer, parmType.GetSizeInMemoryBytes());
  283. argIndex++;
  284. }
  285. // Delete the original memory
  286. engine->CallFree(*(void**)stack_pointer);
  287. }
  288. stack_pointer += 2;
  289. }
  290. }
  291. // For the CDECL_OBJ_LAST calling convention we need to add the object pointer as the last argument
  292. if( param_post )
  293. {
  294. paramBuffer[argIndex] = (asPWORD)obj;
  295. argsType[argIndex] = x64INTARG;
  296. argIndex++;
  297. }
  298. totalArgumentCount = argIndex;
  299. /*
  300. * Q: WTF is going on here !?
  301. *
  302. * A: The idea is to pre-arange the parameters so that X64_CallFunction() can do
  303. * it's little magic which must work regardless of how the compiler decides to
  304. * allocate registers. Basically:
  305. * - the first MAX_CALL_INT_REGISTERS entries in tempBuff will
  306. * contain the values/types of the x64INTARG parameters - that is the ones who
  307. * go into the registers. If the function has less then MAX_CALL_INT_REGISTERS
  308. * integer parameters then the last entries will be set to 0
  309. * - the next MAX_CALL_SSE_REGISTERS entries will contain the float/double arguments
  310. * that go into the floating point registers. If the function has less than
  311. * MAX_CALL_SSE_REGISTERS floating point parameters then the last entries will
  312. * be set to 0
  313. * - index MAX_CALL_INT_REGISTERS + MAX_CALL_SSE_REGISTERS marks the start of the
  314. * parameters which will get passed on the stack. These are added to the array
  315. * in reverse order so that X64_CallFunction() can simply push them to the stack
  316. * without the need to perform further tests
  317. */
  318. asQWORD tempBuff[X64_CALLSTACK_SIZE] = { 0 };
  319. asBYTE argsSet[X64_CALLSTACK_SIZE] = { 0 };
  320. int used_int_regs = 0;
  321. int used_sse_regs = 0;
  322. int used_stack_args = 0;
  323. int idx = 0;
  324. for ( n = 0; ( n < totalArgumentCount ) && ( used_int_regs < MAX_CALL_INT_REGISTERS ); n++ )
  325. {
  326. if ( argsType[n] == x64INTARG )
  327. {
  328. argsSet[n] = 1;
  329. tempBuff[idx++] = paramBuffer[n];
  330. used_int_regs++;
  331. }
  332. }
  333. idx = MAX_CALL_INT_REGISTERS;
  334. for ( n = 0; ( n < totalArgumentCount ) && ( used_sse_regs < MAX_CALL_SSE_REGISTERS ); n++ )
  335. {
  336. if ( argsType[n] == x64FLOATARG )
  337. {
  338. argsSet[n] = 1;
  339. tempBuff[idx++] = paramBuffer[n];
  340. used_sse_regs++;
  341. }
  342. }
  343. idx = MAX_CALL_INT_REGISTERS + MAX_CALL_SSE_REGISTERS;
  344. for ( n = totalArgumentCount - 1; n >= 0; n-- )
  345. {
  346. if ( !argsSet[n] )
  347. {
  348. tempBuff[idx++] = paramBuffer[n];
  349. used_stack_args++;
  350. }
  351. }
  352. retQW = X64_CallFunction( tempBuff, used_stack_args, func, retQW2, sysFunc->hostReturnFloat );
  353. return retQW;
  354. }
  355. END_AS_NAMESPACE
  356. #endif // AS_X64_GCC
  357. #endif // AS_MAX_PORTABILITY