as_callfunc_x64_gcc.cpp 13 KB

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