as_callfunc_x64_mingw.cpp 9.0 KB

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