as_callfunc_x64_msvc.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2015 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. // 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*/, void *secondObject)
  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. // Optimization to avoid check 12 values (all ICC_ that contains THISCALL)
  51. if( (callConv >= ICC_THISCALL && callConv <= ICC_VIRTUAL_THISCALL_RETURNINMEM) ||
  52. (callConv >= ICC_THISCALL_OBJLAST && callConv <= ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM) )
  53. {
  54. // Add the object pointer as the first parameter
  55. allArgBuffer[paramSize++] = (asQWORD)obj;
  56. }
  57. if( sysFunc->hostReturnInMemory )
  58. {
  59. // The return is made in memory
  60. callConv++;
  61. // Set the return pointer as the first argument
  62. allArgBuffer[paramSize++] = (asQWORD)retPointer;
  63. }
  64. if( callConv == ICC_CDECL_OBJFIRST ||
  65. callConv == ICC_CDECL_OBJFIRST_RETURNINMEM )
  66. {
  67. // Add the object pointer as the first parameter
  68. allArgBuffer[paramSize++] = (asQWORD)obj;
  69. }
  70. else if( callConv == ICC_THISCALL_OBJFIRST ||
  71. callConv == ICC_THISCALL_OBJFIRST_RETURNINMEM ||
  72. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
  73. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM )
  74. {
  75. // Add the object pointer as the first parameter
  76. allArgBuffer[paramSize++] = (asQWORD)secondObject;
  77. }
  78. if( callConv == ICC_VIRTUAL_THISCALL ||
  79. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM ||
  80. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
  81. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM ||
  82. callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
  83. callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
  84. {
  85. // Get the true function pointer from the virtual function table
  86. vftable = *(void***)obj;
  87. func = vftable[asPWORD(func)>>2];
  88. }
  89. // Move the arguments to the buffer
  90. asUINT dpos = paramSize;
  91. asUINT spos = 0;
  92. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  93. {
  94. asCDataType &dt = descr->parameterTypes[n];
  95. if( dt.IsObject() && !dt.IsObjectHandle() && !dt.IsReference() )
  96. {
  97. if( dt.GetSizeInMemoryDWords() >= AS_LARGE_OBJ_MIN_SIZE ||
  98. (dt.GetTypeInfo()->flags & COMPLEX_MASK) )
  99. {
  100. allArgBuffer[dpos++] = *(asQWORD*)&args[spos];
  101. spos += AS_PTR_SIZE;
  102. paramSize++;
  103. }
  104. else
  105. {
  106. // Copy the object's memory to the buffer
  107. memcpy(&allArgBuffer[dpos], *(void**)(args+spos), dt.GetSizeInMemoryBytes());
  108. // Delete the original memory
  109. engine->CallFree(*(char**)(args+spos));
  110. spos += AS_PTR_SIZE;
  111. asUINT dwords = dt.GetSizeInMemoryDWords();
  112. asUINT qwords = (dwords >> 1) + (dwords & 1);
  113. dpos += qwords;
  114. paramSize += qwords;
  115. }
  116. }
  117. else if( dt.GetTokenType() == ttQuestion )
  118. {
  119. // Copy the reference and the type id
  120. allArgBuffer[dpos++] = *(asQWORD*)&args[spos];
  121. spos += 2;
  122. allArgBuffer[dpos++] = args[spos++];
  123. paramSize += 2;
  124. }
  125. else
  126. {
  127. // Copy the value directly
  128. asUINT dwords = dt.GetSizeOnStackDWords();
  129. if( dwords > 1 )
  130. {
  131. allArgBuffer[dpos] = *(asQWORD*)&args[spos];
  132. // Double arguments are moved to a separate buffer in order to be placed in the XMM registers,
  133. // though this is only done for first 4 arguments, the rest are placed on the stack
  134. if( paramSize < 4 && dt.IsDoubleType() )
  135. floatArgBuffer[dpos] = *(asQWORD*)&args[spos];
  136. dpos++;
  137. spos += 2;
  138. }
  139. else
  140. {
  141. allArgBuffer[dpos] = args[spos];
  142. // Float arguments are moved to a separate buffer in order to be placed in the XMM registers,
  143. // though this is only done for first 4 arguments, the rest are placed on the stack
  144. if( paramSize < 4 && dt.IsFloatType() )
  145. floatArgBuffer[dpos] = args[spos];
  146. dpos++;
  147. spos++;
  148. }
  149. paramSize++;
  150. }
  151. }
  152. if( callConv == ICC_CDECL_OBJLAST ||
  153. callConv == ICC_CDECL_OBJLAST_RETURNINMEM )
  154. {
  155. // Add the object pointer as the last parameter
  156. allArgBuffer[paramSize++] = (asQWORD)obj;
  157. }
  158. else if( callConv == ICC_THISCALL_OBJLAST ||
  159. callConv == ICC_THISCALL_OBJLAST_RETURNINMEM ||
  160. callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
  161. callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
  162. {
  163. // Add the object pointer as the last parameter
  164. allArgBuffer[paramSize++] = (asQWORD)secondObject;
  165. }
  166. retQW = CallX64(allArgBuffer, floatArgBuffer, paramSize*8, (asPWORD)func);
  167. // If the return is a float value we need to get the value from the FP register
  168. if( sysFunc->hostReturnFloat )
  169. {
  170. if( sysFunc->hostReturnSize == 1 )
  171. *(asDWORD*)&retQW = GetReturnedFloat();
  172. else
  173. retQW = GetReturnedDouble();
  174. }
  175. return retQW;
  176. }
  177. END_AS_NAMESPACE
  178. #endif // AS_X64_MSVC
  179. #endif // AS_MAX_PORTABILITY