as_callfunc_x64_mingw.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. // This code was adapted from as_callfunc_x64_msvc by _Vicious_ on August 20th, 2011.
  25. //
  26. #include <stdio.h>
  27. #include "as_config.h"
  28. #ifndef AS_MAX_PORTABILITY
  29. #ifdef AS_X64_MINGW
  30. #include "as_callfunc.h"
  31. #include "as_scriptengine.h"
  32. #include "as_texts.h"
  33. BEGIN_AS_NAMESPACE
  34. static asQWORD CallX64(const asQWORD *args, const asQWORD *floatArgs, const int paramSize, asQWORD func)
  35. {
  36. asQWORD ret = 0;
  37. __asm__ __volatile__ (
  38. "# Move function param to non-scratch register\n"
  39. "mov %4,%%r14 # r14 = function\n" // Copy func into r14
  40. "# Allocate space on the stack for the arguments\n"
  41. "# Make room for at least 4 arguments even if there are less. When\n"
  42. "# the compiler does optimizations for speed it may use these for \n"
  43. "# temporary storage.\n"
  44. "xor %%rdi, %%rdi\n"
  45. "mov %3, %%edi\n"
  46. "add $32,%%edi\n"
  47. "# Make sure the stack pointer is 16byte aligned so the\n"
  48. "# whole program optimizations will work properly\n"
  49. "# TODO: optimize: Can this be optimized with fewer instructions?\n"
  50. "mov %%rsp,%%rsi\n"
  51. "sub %%rdi,%%rsi\n"
  52. "and $0x8,%%rsi\n"
  53. "add %%rsi,%%rdi\n"
  54. "sub %%rdi,%%rsp\n"
  55. "# Jump straight to calling the function if no parameters\n"
  56. "cmp $0,%3 # Compare paramSize with 0\n"
  57. "je callfunc # Jump to call funtion if (paramSize == 0)\n"
  58. "# Move params to non-scratch registers\n"
  59. "mov %1,%%rsi # rsi = pArgs\n" // Copy args into rsi
  60. "mov %2,%%r11 # r11 = pFloatArgs (can be NULL)\n" // Copy floatArgs into r11
  61. "mov %3,%%r12d # r12 = paramSize\n" // Copy paramSize into r12
  62. "# Copy arguments from script stack to application stack\n"
  63. "# Order is (first to last):\n"
  64. "# rcx, rdx, r8, r9 & everything else goes on stack\n"
  65. "movq (%%rsi),%%rcx\n"
  66. "movq 8(%%rsi),%%rdx\n"
  67. "movq 16(%%rsi),%%r8\n"
  68. "movq 24(%%rsi),%%r9\n"
  69. "# Negate the 4 params from the size to be copied\n"
  70. "sub $32,%%r12d\n"
  71. "js copyfloat # Jump if negative result\n"
  72. "jz copyfloat # Jump if zero result\n"
  73. "# Now copy all remaining params onto stack allowing space for first four\n"
  74. "# params to be flushed back to the stack if required by the callee.\n"
  75. "add $32,%%rsi # Position input pointer 4 args ahead\n"
  76. "mov %%rsp,%%r13 # Put the stack pointer into r13\n"
  77. "add $32,%%r13 # Leave space for first 4 args on stack\n"
  78. "copyoverflow:\n"
  79. "movq (%%rsi),%%r15 # Read param from source stack into r15\n"
  80. "movq %%r15,(%%r13) # Copy param to real stack\n"
  81. "add $8,%%r13 # Move virtual stack pointer\n"
  82. "add $8,%%rsi # Move source stack pointer\n"
  83. "sub $8,%%r12d # Decrement remaining count\n"
  84. "jnz copyoverflow # Continue if more params\n"
  85. "copyfloat:\n"
  86. "# Any floating point params?\n"
  87. "cmp $0,%%r11\n"
  88. "je callfunc\n"
  89. "movlpd (%%r11),%%xmm0\n"
  90. "movlpd 8(%%r11),%%xmm1\n"
  91. "movlpd 16(%%r11),%%xmm2\n"
  92. "movlpd 24(%%r11),%%xmm3\n"
  93. "callfunc:\n"
  94. "call *%%r14\n"
  95. "lea %0, %%rbx\n" // Load the address of the ret variable into rbx
  96. "movq %%rax,(%%rbx)\n" // Copy the returned value into the ret variable
  97. : // no output
  98. : "m" (ret), "r" (args), "r" (floatArgs), "r" (paramSize), "r" (func)
  99. : "rdi", "rsi", "rsp", "rbx", "r11", "%r12", "r13", "r14", "r15"
  100. );
  101. return ret;
  102. }
  103. static asDWORD GetReturnedFloat()
  104. {
  105. float retval = 0.0f;
  106. asDWORD ret = 0;
  107. __asm__ __volatile__ (
  108. "lea %0, %%rax\n"
  109. "movss %%xmm0, (%%rax)"
  110. : /* no output */
  111. : "m" (retval)
  112. : "%rax"
  113. );
  114. // We need to avoid implicit conversions from float to unsigned - we need
  115. // a bit-wise-correct-and-complete copy of the value
  116. memcpy( &ret, &retval, sizeof( ret ) );
  117. return ( asDWORD )ret;
  118. }
  119. static asQWORD GetReturnedDouble()
  120. {
  121. double retval = 0.0f;
  122. asQWORD ret = 0;
  123. __asm__ __volatile__ (
  124. "lea %0, %%rax\n"
  125. "movlpd %%xmm0, (%%rax)"
  126. : /* no optput */
  127. : "m" (retval)
  128. : "%rax"
  129. );
  130. // We need to avoid implicit conversions from double to unsigned long long - we need
  131. // a bit-wise-correct-and-complete copy of the value
  132. memcpy( &ret, &retval, sizeof( ret ) );
  133. return ret;
  134. }
  135. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/)
  136. {
  137. asCScriptEngine *engine = context->engine;
  138. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  139. asQWORD retQW = 0;
  140. void *func = (void*)sysFunc->func;
  141. asUINT paramSize = 0; // QWords
  142. void **vftable;
  143. asQWORD allArgBuffer[64];
  144. asQWORD floatArgBuffer[4];
  145. int callConv = sysFunc->callConv;
  146. if( sysFunc->hostReturnInMemory )
  147. {
  148. // The return is made in memory
  149. callConv++;
  150. // Set the return pointer as the first argument
  151. allArgBuffer[paramSize++] = (asQWORD)retPointer;
  152. }
  153. if( callConv == ICC_THISCALL ||
  154. callConv == ICC_THISCALL_RETURNINMEM ||
  155. callConv == ICC_VIRTUAL_THISCALL ||
  156. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM )
  157. {
  158. // Add the object pointer as the first parameter
  159. allArgBuffer[paramSize++] = (asQWORD)obj;
  160. }
  161. if( callConv == ICC_CDECL_OBJFIRST ||
  162. callConv == ICC_CDECL_OBJFIRST_RETURNINMEM )
  163. {
  164. // Add the object pointer as the first parameter
  165. allArgBuffer[paramSize++] = (asQWORD)obj;
  166. }
  167. if( callConv == ICC_VIRTUAL_THISCALL ||
  168. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM )
  169. {
  170. // Get the true function pointer from the virtual function table
  171. vftable = *(void***)obj;
  172. func = vftable[size_t(func)>>3];
  173. }
  174. // Move the arguments to the buffer
  175. asUINT dpos = paramSize;
  176. asUINT spos = 0;
  177. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  178. {
  179. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  180. {
  181. if( descr->parameterTypes[n].GetSizeInMemoryDWords() >= AS_LARGE_OBJ_MIN_SIZE ||
  182. (descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK) )
  183. {
  184. allArgBuffer[dpos++] = *(asQWORD*)&args[spos];
  185. spos += AS_PTR_SIZE;
  186. paramSize++;
  187. }
  188. else
  189. {
  190. // Copy the object's memory to the buffer
  191. memcpy(&allArgBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  192. // Delete the original memory
  193. engine->CallFree(*(char**)(args+spos));
  194. spos += AS_PTR_SIZE;
  195. asUINT dwords = descr->parameterTypes[n].GetSizeInMemoryDWords();
  196. asUINT qwords = (dwords >> 1) + (dwords & 1);
  197. dpos += qwords;
  198. paramSize += qwords;
  199. }
  200. }
  201. else if( descr->parameterTypes[n].GetTokenType() == ttQuestion )
  202. {
  203. // Copy the reference and the type id
  204. allArgBuffer[dpos++] = *(asQWORD*)&args[spos];
  205. spos += 2;
  206. allArgBuffer[dpos++] = args[spos++];
  207. paramSize += 2;
  208. }
  209. else
  210. {
  211. // Copy the value directly
  212. asUINT dwords = descr->parameterTypes[n].GetSizeOnStackDWords();
  213. if( dwords > 1 )
  214. {
  215. allArgBuffer[dpos] = *(asQWORD*)&args[spos];
  216. // Double arguments are moved to a separate buffer in order to be placed in the XMM registers,
  217. // though this is only done for first 4 arguments, the rest are placed on the stack
  218. if( paramSize < 4 && descr->parameterTypes[n].IsDoubleType() )
  219. floatArgBuffer[dpos] = *(asQWORD*)&args[spos];
  220. dpos++;
  221. spos += 2;
  222. }
  223. else
  224. {
  225. allArgBuffer[dpos] = args[spos];
  226. // Float arguments are moved to a separate buffer in order to be placed in the XMM registers,
  227. // though this is only done for first 4 arguments, the rest are placed on the stack
  228. if( paramSize < 4 && descr->parameterTypes[n].IsFloatType() )
  229. floatArgBuffer[dpos] = args[spos];
  230. dpos++;
  231. spos++;
  232. }
  233. paramSize++;
  234. }
  235. }
  236. if( callConv == ICC_CDECL_OBJLAST ||
  237. callConv == ICC_CDECL_OBJLAST_RETURNINMEM )
  238. {
  239. // Add the object pointer as the last parameter
  240. allArgBuffer[paramSize++] = (asQWORD)obj;
  241. }
  242. context->isCallingSystemFunction = true;
  243. retQW = CallX64(allArgBuffer, floatArgBuffer, paramSize*8, (size_t)func);
  244. context->isCallingSystemFunction = false;
  245. // If the return is a float value we need to get the value from the FP register
  246. if( sysFunc->hostReturnFloat )
  247. {
  248. if( sysFunc->hostReturnSize == 1 )
  249. *(asDWORD*)&retQW = GetReturnedFloat();
  250. else
  251. retQW = GetReturnedDouble();
  252. }
  253. return retQW;
  254. }
  255. END_AS_NAMESPACE
  256. #endif // AS_X64_MSVC
  257. #endif // AS_MAX_PORTABILITY