as_callfunc_x64_gcc.cpp 12 KB

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