as_callfunc_x64_mingw.cpp 10 KB

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