as_callfunc_x64_msvc.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include <stdio.h>
  24. #include "as_config.h"
  25. #ifndef AS_MAX_PORTABILITY
  26. #ifdef AS_X64_MSVC
  27. #include "as_callfunc.h"
  28. #include "as_scriptengine.h"
  29. #include "as_texts.h"
  30. #include "as_context.h"
  31. BEGIN_AS_NAMESPACE
  32. // These functions are implemented in as_callfunc_x64_msvc.asm
  33. extern "C" asQWORD CallX64(const asQWORD *args, const asQWORD *floatArgs, int paramSize, asQWORD func);
  34. extern "C" asDWORD GetReturnedFloat();
  35. extern "C" asQWORD GetReturnedDouble();
  36. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/)
  37. {
  38. asCScriptEngine *engine = context->m_engine;
  39. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  40. asQWORD retQW = 0;
  41. void *func = (void*)sysFunc->func;
  42. asUINT paramSize = 0; // QWords
  43. void **vftable;
  44. asQWORD allArgBuffer[64];
  45. asQWORD floatArgBuffer[4];
  46. int callConv = sysFunc->callConv;
  47. if( callConv == ICC_THISCALL ||
  48. callConv == ICC_THISCALL_RETURNINMEM ||
  49. callConv == ICC_VIRTUAL_THISCALL ||
  50. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM )
  51. {
  52. // Add the object pointer as the first parameter
  53. allArgBuffer[paramSize++] = (asQWORD)obj;
  54. }
  55. if( sysFunc->hostReturnInMemory )
  56. {
  57. // The return is made in memory
  58. callConv++;
  59. // Set the return pointer as the first argument
  60. allArgBuffer[paramSize++] = (asQWORD)retPointer;
  61. }
  62. if( callConv == ICC_CDECL_OBJFIRST ||
  63. callConv == ICC_CDECL_OBJFIRST_RETURNINMEM )
  64. {
  65. // Add the object pointer as the first parameter
  66. allArgBuffer[paramSize++] = (asQWORD)obj;
  67. }
  68. if( callConv == ICC_VIRTUAL_THISCALL ||
  69. callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM )
  70. {
  71. // Get the true function pointer from the virtual function table
  72. vftable = *(void***)obj;
  73. func = vftable[asPWORD(func)>>2];
  74. }
  75. // Move the arguments to the buffer
  76. asUINT dpos = paramSize;
  77. asUINT spos = 0;
  78. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  79. {
  80. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  81. {
  82. if( descr->parameterTypes[n].GetSizeInMemoryDWords() >= AS_LARGE_OBJ_MIN_SIZE ||
  83. (descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK) )
  84. {
  85. allArgBuffer[dpos++] = *(asQWORD*)&args[spos];
  86. spos += AS_PTR_SIZE;
  87. paramSize++;
  88. }
  89. else
  90. {
  91. // Copy the object's memory to the buffer
  92. memcpy(&allArgBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  93. // Delete the original memory
  94. engine->CallFree(*(char**)(args+spos));
  95. spos += AS_PTR_SIZE;
  96. asUINT dwords = descr->parameterTypes[n].GetSizeInMemoryDWords();
  97. asUINT qwords = (dwords >> 1) + (dwords & 1);
  98. dpos += qwords;
  99. paramSize += qwords;
  100. }
  101. }
  102. else if( descr->parameterTypes[n].GetTokenType() == ttQuestion )
  103. {
  104. // Copy the reference and the type id
  105. allArgBuffer[dpos++] = *(asQWORD*)&args[spos];
  106. spos += 2;
  107. allArgBuffer[dpos++] = args[spos++];
  108. paramSize += 2;
  109. }
  110. else
  111. {
  112. // Copy the value directly
  113. asUINT dwords = descr->parameterTypes[n].GetSizeOnStackDWords();
  114. if( dwords > 1 )
  115. {
  116. allArgBuffer[dpos] = *(asQWORD*)&args[spos];
  117. // Double arguments are moved to a separate buffer in order to be placed in the XMM registers,
  118. // though this is only done for first 4 arguments, the rest are placed on the stack
  119. if( paramSize < 4 && descr->parameterTypes[n].IsDoubleType() )
  120. floatArgBuffer[dpos] = *(asQWORD*)&args[spos];
  121. dpos++;
  122. spos += 2;
  123. }
  124. else
  125. {
  126. allArgBuffer[dpos] = args[spos];
  127. // Float arguments are moved to a separate buffer in order to be placed in the XMM registers,
  128. // though this is only done for first 4 arguments, the rest are placed on the stack
  129. if( paramSize < 4 && descr->parameterTypes[n].IsFloatType() )
  130. floatArgBuffer[dpos] = args[spos];
  131. dpos++;
  132. spos++;
  133. }
  134. paramSize++;
  135. }
  136. }
  137. if( callConv == ICC_CDECL_OBJLAST ||
  138. callConv == ICC_CDECL_OBJLAST_RETURNINMEM )
  139. {
  140. // Add the object pointer as the last parameter
  141. allArgBuffer[paramSize++] = (asQWORD)obj;
  142. }
  143. retQW = CallX64(allArgBuffer, floatArgBuffer, paramSize*8, (asPWORD)func);
  144. // If the return is a float value we need to get the value from the FP register
  145. if( sysFunc->hostReturnFloat )
  146. {
  147. if( sysFunc->hostReturnSize == 1 )
  148. *(asDWORD*)&retQW = GetReturnedFloat();
  149. else
  150. retQW = GetReturnedDouble();
  151. }
  152. return retQW;
  153. }
  154. END_AS_NAMESPACE
  155. #endif // AS_X64_MSVC
  156. #endif // AS_MAX_PORTABILITY