as_callfunc_x64_msvc.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. [email protected]
  22. */
  23. //
  24. // Added support for thiscall methods by Jordi Oliveras Rovira in April, 2014.
  25. //
  26. #include <stdio.h>
  27. #include "as_config.h"
  28. #ifndef AS_MAX_PORTABILITY
  29. #ifdef AS_X64_MSVC
  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. // These functions are implemented in as_callfunc_x64_msvc.asm
  36. extern "C" asQWORD CallX64(const asQWORD *args, const asQWORD *floatArgs, int paramSize, asQWORD func);
  37. extern "C" asDWORD GetReturnedFloat();
  38. extern "C" asQWORD GetReturnedDouble();
  39. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/)
  40. {
  41. asCScriptEngine *engine = context->m_engine;
  42. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  43. asQWORD retQW = 0;
  44. void *func = (void*)sysFunc->func;
  45. asUINT paramSize = 0; // QWords
  46. void **vftable;
  47. asQWORD allArgBuffer[64];
  48. asQWORD floatArgBuffer[4];
  49. int callConv = sysFunc->callConv;
  50. #ifdef AS_NO_THISCALL_FUNCTOR_METHOD
  51. if( callConv == ICC_THISCALL ||
  52. callConv == ICC_THISCALL_RETURNINMEM ||
  53. callConv == ICC_VIRTUAL_THISCALL ||
  54. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM )
  55. #else
  56. // Unpack the two object pointers
  57. void **objectsPtrs = (void**)obj;
  58. void *secondObject = objectsPtrs[1];
  59. obj = objectsPtrs[0];
  60. // Optimization to avoid check 12 values (all ICC_ that contains THISCALL)
  61. if( (callConv >= ICC_THISCALL && callConv <= ICC_VIRTUAL_THISCALL_RETURNINMEM) ||
  62. (callConv >= ICC_THISCALL_OBJLAST && callConv <= ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM) )
  63. #endif
  64. {
  65. // Add the object pointer as the first parameter
  66. allArgBuffer[paramSize++] = (asQWORD)obj;
  67. }
  68. if( sysFunc->hostReturnInMemory )
  69. {
  70. // The return is made in memory
  71. callConv++;
  72. // Set the return pointer as the first argument
  73. allArgBuffer[paramSize++] = (asQWORD)retPointer;
  74. }
  75. if( callConv == ICC_CDECL_OBJFIRST ||
  76. callConv == ICC_CDECL_OBJFIRST_RETURNINMEM )
  77. {
  78. // Add the object pointer as the first parameter
  79. allArgBuffer[paramSize++] = (asQWORD)obj;
  80. }
  81. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  82. else if( callConv == ICC_THISCALL_OBJFIRST ||
  83. callConv == ICC_THISCALL_OBJFIRST_RETURNINMEM ||
  84. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
  85. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM )
  86. {
  87. // Add the object pointer as the first parameter
  88. allArgBuffer[paramSize++] = (asQWORD)secondObject;
  89. }
  90. #endif
  91. #ifdef AS_NO_THISCALL_FUNCTOR_METHOD
  92. if( callConv == ICC_VIRTUAL_THISCALL ||
  93. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM )
  94. #else
  95. if( callConv == ICC_VIRTUAL_THISCALL ||
  96. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM ||
  97. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
  98. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM ||
  99. callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
  100. callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
  101. #endif
  102. {
  103. // Get the true function pointer from the virtual function table
  104. vftable = *(void***)obj;
  105. func = vftable[asPWORD(func)>>2];
  106. }
  107. // Move the arguments to the buffer
  108. asUINT dpos = paramSize;
  109. asUINT spos = 0;
  110. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  111. {
  112. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  113. {
  114. if( descr->parameterTypes[n].GetSizeInMemoryDWords() >= AS_LARGE_OBJ_MIN_SIZE ||
  115. (descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK) )
  116. {
  117. allArgBuffer[dpos++] = *(asQWORD*)&args[spos];
  118. spos += AS_PTR_SIZE;
  119. paramSize++;
  120. }
  121. else
  122. {
  123. // Copy the object's memory to the buffer
  124. memcpy(&allArgBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  125. // Delete the original memory
  126. engine->CallFree(*(char**)(args+spos));
  127. spos += AS_PTR_SIZE;
  128. asUINT dwords = descr->parameterTypes[n].GetSizeInMemoryDWords();
  129. asUINT qwords = (dwords >> 1) + (dwords & 1);
  130. dpos += qwords;
  131. paramSize += qwords;
  132. }
  133. }
  134. else if( descr->parameterTypes[n].GetTokenType() == ttQuestion )
  135. {
  136. // Copy the reference and the type id
  137. allArgBuffer[dpos++] = *(asQWORD*)&args[spos];
  138. spos += 2;
  139. allArgBuffer[dpos++] = args[spos++];
  140. paramSize += 2;
  141. }
  142. else
  143. {
  144. // Copy the value directly
  145. asUINT dwords = descr->parameterTypes[n].GetSizeOnStackDWords();
  146. if( dwords > 1 )
  147. {
  148. allArgBuffer[dpos] = *(asQWORD*)&args[spos];
  149. // Double arguments are moved to a separate buffer in order to be placed in the XMM registers,
  150. // though this is only done for first 4 arguments, the rest are placed on the stack
  151. if( paramSize < 4 && descr->parameterTypes[n].IsDoubleType() )
  152. floatArgBuffer[dpos] = *(asQWORD*)&args[spos];
  153. dpos++;
  154. spos += 2;
  155. }
  156. else
  157. {
  158. allArgBuffer[dpos] = args[spos];
  159. // Float arguments are moved to a separate buffer in order to be placed in the XMM registers,
  160. // though this is only done for first 4 arguments, the rest are placed on the stack
  161. if( paramSize < 4 && descr->parameterTypes[n].IsFloatType() )
  162. floatArgBuffer[dpos] = args[spos];
  163. dpos++;
  164. spos++;
  165. }
  166. paramSize++;
  167. }
  168. }
  169. if( callConv == ICC_CDECL_OBJLAST ||
  170. callConv == ICC_CDECL_OBJLAST_RETURNINMEM )
  171. {
  172. // Add the object pointer as the last parameter
  173. allArgBuffer[paramSize++] = (asQWORD)obj;
  174. }
  175. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  176. else if( callConv == ICC_THISCALL_OBJLAST ||
  177. callConv == ICC_THISCALL_OBJLAST_RETURNINMEM ||
  178. callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
  179. callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
  180. {
  181. // Add the object pointer as the last parameter
  182. allArgBuffer[paramSize++] = (asQWORD)secondObject;
  183. }
  184. #endif
  185. retQW = CallX64(allArgBuffer, floatArgBuffer, paramSize*8, (asPWORD)func);
  186. // If the return is a float value we need to get the value from the FP register
  187. if( sysFunc->hostReturnFloat )
  188. {
  189. if( sysFunc->hostReturnSize == 1 )
  190. *(asDWORD*)&retQW = GetReturnedFloat();
  191. else
  192. retQW = GetReturnedDouble();
  193. }
  194. return retQW;
  195. }
  196. END_AS_NAMESPACE
  197. #endif // AS_X64_MSVC
  198. #endif // AS_MAX_PORTABILITY