as_callfunc_arm64.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2020-2021 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. // as_callfunc_arm64.cpp
  25. //
  26. // These functions handle the actual calling of system functions on the arm64 platform
  27. //
  28. // Written by Max Waine in July 2020, based on as_callfunc_arm.cpp
  29. //
  30. #include "as_config.h"
  31. #ifndef AS_MAX_PORTABILITY
  32. #ifdef AS_ARM64
  33. #include "as_callfunc.h"
  34. #include "as_scriptengine.h"
  35. #include "as_texts.h"
  36. #include "as_tokendef.h"
  37. #include "as_context.h"
  38. // ARM64 targets use has no software floating-point ABI, it's all hardware (or totally disabled)
  39. #define HFA_RET_REGISTERS 4 // s0-s3/d0-d3
  40. #define GP_ARG_REGISTERS 8 // x0-x7
  41. #define FLOAT_ARG_REGISTERS 8 // v0-v7
  42. BEGIN_AS_NAMESPACE
  43. // x0-7: Argument registers (pass params or return results. OK as volatile local variables)
  44. // x8: Indirect result register (e.g. address of large returned struct)
  45. // x9-15: Volatile local variable registers
  46. // x16-17: Intra-procedure-call temporary registers
  47. // x18: Platform register (reserved for use of platform ABIs)
  48. // x19-29: Non-volatile variable registers (must be saved and restored if modified)
  49. // x29: Frame pointer register
  50. // x30: Link register (where to return to)
  51. extern "C" void GetHFAReturnDouble(asQWORD *out1, asQWORD *out2, asQWORD returnSize);
  52. extern "C" void GetHFAReturnFloat(asQWORD *out1, asQWORD *out2, asQWORD returnSize);
  53. extern "C" asQWORD CallARM64RetInMemory(
  54. const asQWORD *gpRegArgs, asQWORD numGPRegArgs,
  55. const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
  56. const asQWORD *stackArgs, asQWORD numStackArgs,
  57. void *retPointer, asFUNCTION_t func
  58. );
  59. extern "C" double CallARM64Double(
  60. const asQWORD *gpRegArgs, asQWORD numGPRegArgs,
  61. const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
  62. const asQWORD *stackArgs, asQWORD numStackArgs,
  63. asFUNCTION_t func
  64. );
  65. extern "C" float CallARM64Float(
  66. const asQWORD *gpRegArgs, asQWORD numGPRegArgs,
  67. const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
  68. const asQWORD *stackArgs, asQWORD numStackArgs,
  69. asFUNCTION_t func
  70. );
  71. extern "C" asQWORD CallARM64(
  72. const asQWORD *gpRegArgs, asQWORD numGPRegArgs,
  73. const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
  74. const asQWORD *stackArgs, asQWORD numStackArgs,
  75. asFUNCTION_t func
  76. );
  77. extern "C" asQWORD CallARM64Ret128(
  78. const asQWORD *gpRegArgs, asQWORD numGPRegArgs,
  79. const asQWORD *floatRegArgs, asQWORD numFloatRegArgs,
  80. const asQWORD *stackArgs, asQWORD numStackArgs,
  81. asQWORD *higherQWORD, asFUNCTION_t func
  82. );
  83. //
  84. // If it's possible to fit in registers,
  85. // there may not be enough float register space even if true is returned
  86. //
  87. static inline bool IsRegisterHFA(const asCDataType &type)
  88. {
  89. const asCTypeInfo *const typeInfo = type.GetTypeInfo();
  90. if( typeInfo == nullptr ||
  91. (typeInfo->flags & asOBJ_APP_CLASS_ALLFLOATS) == 0 ||
  92. type.IsObjectHandle() || type.IsReference() )
  93. return false;
  94. const bool doubles = (typeInfo->flags & asOBJ_APP_CLASS_ALIGN8) != 0;
  95. const int maxAllowedSize = doubles ? sizeof(double) * HFA_RET_REGISTERS : sizeof(float) * HFA_RET_REGISTERS;
  96. return type.GetSizeInMemoryBytes() <= maxAllowedSize;
  97. }
  98. //
  99. // If it's possible to fit it in registers,
  100. // if true is returned there is enough space to fit
  101. //
  102. static inline bool IsRegisterHFAParameter(const asCDataType &type, const asQWORD numFloatRegArgs)
  103. {
  104. if( !IsRegisterHFA(type) )
  105. return false;
  106. const bool doubles = (type.GetTypeInfo()->flags & asOBJ_APP_CLASS_ALIGN8) != 0;
  107. const int registersUsed = type.GetSizeInMemoryDWords() / (doubles ? sizeof(double) : sizeof(float));
  108. return numFloatRegArgs + registersUsed <= FLOAT_ARG_REGISTERS;
  109. }
  110. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &retQW2, void *secondObject)
  111. {
  112. asCScriptEngine *engine = context->m_engine;
  113. const asSSystemFunctionInterface *const sysFunc = descr->sysFuncIntf;
  114. const asCDataType &retType = descr->returnType;
  115. const asCTypeInfo *const retTypeInfo = retType.GetTypeInfo();
  116. asFUNCTION_t func = sysFunc->func;
  117. int callConv = sysFunc->callConv;
  118. asQWORD retQW = 0;
  119. asQWORD gpRegArgs[GP_ARG_REGISTERS];
  120. asQWORD floatRegArgs[FLOAT_ARG_REGISTERS];
  121. asQWORD stackArgs[64]; // It's how many x64 users can have
  122. asQWORD numGPRegArgs = 0;
  123. asQWORD numFloatRegArgs = 0;
  124. asQWORD numStackArgs = 0;
  125. asFUNCTION_t *vftable;
  126. // Optimization to avoid check 12 values (all ICC_ that contains THISCALL)
  127. if( (callConv >= ICC_THISCALL && callConv <= ICC_VIRTUAL_THISCALL_RETURNINMEM) ||
  128. (callConv >= ICC_THISCALL_OBJLAST && callConv <= ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM) )
  129. {
  130. // Add the object pointer as the first parameter
  131. gpRegArgs[numGPRegArgs++] = (asQWORD)obj;
  132. }
  133. if( callConv == ICC_CDECL_OBJFIRST || callConv == ICC_CDECL_OBJFIRST_RETURNINMEM )
  134. {
  135. // Add the object pointer as the first parameter
  136. gpRegArgs[numGPRegArgs++] = (asQWORD)obj;
  137. }
  138. else if( callConv == ICC_THISCALL_OBJFIRST || callConv == ICC_THISCALL_OBJFIRST_RETURNINMEM ||
  139. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM )
  140. {
  141. // Add the object pointer as the first parameter
  142. gpRegArgs[numGPRegArgs++] = (asQWORD)secondObject;
  143. }
  144. if( callConv == ICC_VIRTUAL_THISCALL || callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
  145. callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJLAST || callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
  146. {
  147. // Get virtual function table from the object pointer
  148. vftable = *(asFUNCTION_t**)obj;
  149. func = vftable[FuncPtrToUInt(func)/sizeof(void*)];
  150. }
  151. asUINT argsPos = 0;
  152. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  153. {
  154. const asCDataType &parmType = descr->parameterTypes[n];
  155. const asCTypeInfo *const parmTypeInfo = parmType.GetTypeInfo();
  156. if( parmType.IsObject() && !parmType.IsObjectHandle() && !parmType.IsReference() )
  157. {
  158. const asUINT parmDWords = parmType.GetSizeInMemoryDWords();
  159. const asUINT parmQWords = (parmDWords >> 1) + (parmDWords & 1);
  160. const bool passedAsPointer = parmQWords <= 2;
  161. const bool fitsInRegisters = passedAsPointer ? (numGPRegArgs < GP_ARG_REGISTERS) : (numGPRegArgs + parmQWords <= GP_ARG_REGISTERS);
  162. asQWORD *const argsArray = fitsInRegisters ? gpRegArgs : stackArgs;
  163. asQWORD &numArgs = fitsInRegisters ? numGPRegArgs : numStackArgs;
  164. if( (parmTypeInfo->flags & COMPLEX_MASK) )
  165. {
  166. argsArray[numArgs++] = *(asQWORD*)&args[argsPos];
  167. argsPos += AS_PTR_SIZE;
  168. }
  169. else if( IsRegisterHFAParameter(parmType, numFloatRegArgs) )
  170. {
  171. if( (parmTypeInfo->flags & asOBJ_APP_CLASS_ALIGN8) != 0 )
  172. {
  173. const asQWORD *const contents = *(asQWORD**)&args[argsPos];
  174. for( asUINT i = 0; i < parmQWords; i++ )
  175. floatRegArgs[numFloatRegArgs++] = *(asQWORD*)&contents[i];
  176. }
  177. else
  178. {
  179. const asDWORD *const contents = *(asDWORD**)&args[argsPos];
  180. for( asUINT i = 0; i < parmDWords; i++ )
  181. floatRegArgs[numFloatRegArgs++] = *(asQWORD*)&contents[i];
  182. }
  183. engine->CallFree(*(char**)(args+argsPos));
  184. argsPos += AS_PTR_SIZE;
  185. }
  186. else
  187. {
  188. // Copy the object's memory to the buffer
  189. memcpy(&argsArray[numArgs], *(void**)(args+argsPos), parmType.GetSizeInMemoryBytes());
  190. // Delete the original memory
  191. engine->CallFree(*(char**)(args+argsPos));
  192. argsPos += AS_PTR_SIZE;
  193. numArgs += parmQWords;
  194. }
  195. }
  196. else if( parmType.IsFloatType() && !parmType.IsReference() )
  197. {
  198. if( numFloatRegArgs >= FLOAT_ARG_REGISTERS )
  199. stackArgs[numStackArgs++] = args[argsPos];
  200. else
  201. floatRegArgs[numFloatRegArgs++] = args[argsPos];
  202. argsPos++;
  203. }
  204. else if( parmType.IsDoubleType() && !parmType.IsReference() )
  205. {
  206. if( numFloatRegArgs >= FLOAT_ARG_REGISTERS )
  207. stackArgs[numStackArgs++] = *(asQWORD*)&args[argsPos];
  208. else
  209. floatRegArgs[numFloatRegArgs++] = *(asQWORD*)&args[argsPos];
  210. argsPos += 2;
  211. }
  212. else
  213. {
  214. // Copy the value directly
  215. const asUINT parmDWords = parmType.GetSizeOnStackDWords();
  216. const asUINT parmQWords = (parmDWords >> 1) + (parmDWords & 1);
  217. const bool fitsInRegisters = numGPRegArgs + parmQWords <= GP_ARG_REGISTERS;
  218. asQWORD *const argsArray = fitsInRegisters ? gpRegArgs : stackArgs;
  219. asQWORD &numArgs = fitsInRegisters ? numGPRegArgs : numStackArgs;
  220. memcpy(&argsArray[numArgs], (void*)(args+argsPos), parmDWords * 4);
  221. argsPos += parmDWords;
  222. numArgs += parmQWords;
  223. }
  224. }
  225. if( callConv == ICC_CDECL_OBJLAST || callConv == ICC_CDECL_OBJLAST_RETURNINMEM )
  226. {
  227. // Add the object pointer as the last parameter
  228. if( numGPRegArgs < GP_ARG_REGISTERS )
  229. gpRegArgs[numGPRegArgs++] = (asQWORD)obj;
  230. else
  231. stackArgs[numStackArgs++] = (asQWORD)obj;
  232. }
  233. else if( callConv == ICC_THISCALL_OBJLAST || callConv == ICC_THISCALL_OBJLAST_RETURNINMEM ||
  234. callConv == ICC_VIRTUAL_THISCALL_OBJLAST || callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
  235. {
  236. // Add the object pointer as the last parameter
  237. if( numGPRegArgs < GP_ARG_REGISTERS )
  238. gpRegArgs[numGPRegArgs++] = (asQWORD)secondObject;
  239. else
  240. stackArgs[numStackArgs++] = (asQWORD)secondObject;
  241. }
  242. if( IsRegisterHFA(retType) && !(retTypeInfo->flags & COMPLEX_MASK) )
  243. {
  244. // This is to deal with HFAs (Homogeneous Floating-point Aggregates):
  245. // ARM64 will place all-float composite types (of equal precision)
  246. // with <= 4 members in the float return registers
  247. const int structSize = retType.GetSizeInMemoryBytes();
  248. CallARM64(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, func);
  249. if( (retTypeInfo->flags & asOBJ_APP_CLASS_ALIGN8) != 0 )
  250. {
  251. if( structSize <= sizeof(double) * 2 )
  252. GetHFAReturnDouble(&retQW, &retQW2, structSize);
  253. else
  254. GetHFAReturnDouble((asQWORD*)retPointer, ((asQWORD*)retPointer) + 1, structSize);
  255. }
  256. else
  257. GetHFAReturnFloat(&retQW, &retQW2, structSize);
  258. }
  259. else if( sysFunc->hostReturnFloat )
  260. {
  261. if( sysFunc->hostReturnSize == 1 )
  262. *(float*)&retQW = CallARM64Float(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, func);
  263. else
  264. *(double*)&retQW = CallARM64Double(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, func);
  265. }
  266. else if( sysFunc->hostReturnInMemory )
  267. retQW = CallARM64RetInMemory(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, retPointer, func);
  268. else
  269. {
  270. if( retType.GetSizeInMemoryBytes() > sizeof(asQWORD) )
  271. retQW = CallARM64Ret128(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, &retQW2, func);
  272. else
  273. retQW = CallARM64(gpRegArgs, numGPRegArgs, floatRegArgs, numFloatRegArgs, stackArgs, numStackArgs, func);
  274. }
  275. return retQW;
  276. }
  277. END_AS_NAMESPACE
  278. #endif // AS_ARM64
  279. #endif // AS_MAX_PORTABILITY