2
0

as_callfunc_x64_gcc.cpp 13 KB

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