as_callfunc_x64_msvc.cpp 5.8 KB

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