as_callfunc.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-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.cpp
  25. //
  26. // These functions handle the actual calling of system functions
  27. //
  28. #include "as_config.h"
  29. #include "as_callfunc.h"
  30. #include "as_scriptengine.h"
  31. #include "as_texts.h"
  32. #include "as_context.h"
  33. BEGIN_AS_NAMESPACE
  34. // ref: Member Function Pointers and the Fastest Possible C++ Delegates
  35. // describes the structure of class method pointers for most compilers
  36. // http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible
  37. // ref: The code comments for ItaniumCXXABI::EmitLoadOfMemberFunctionPointer in the LLVM compiler
  38. // describes the structure for class method pointers on Itanium and arm64 ABI
  39. // http://clang.llvm.org/doxygen/CodeGen_2ItaniumCXXABI_8cpp_source.html#l00937
  40. int DetectCallingConvention(bool isMethod, const asSFuncPtr &ptr, int callConv, void *auxiliary, asSSystemFunctionInterface *internal)
  41. {
  42. internal->Clear();
  43. internal->func = ptr.ptr.f.func;
  44. internal->auxiliary = 0;
  45. // Was a compatible calling convention specified?
  46. if( internal->func )
  47. {
  48. if( ptr.flag == 1 && callConv != asCALL_GENERIC )
  49. return asWRONG_CALLING_CONV;
  50. else if( ptr.flag == 2 && (callConv == asCALL_GENERIC || callConv == asCALL_THISCALL || callConv == asCALL_THISCALL_ASGLOBAL || callConv == asCALL_THISCALL_OBJFIRST || callConv == asCALL_THISCALL_OBJLAST) )
  51. return asWRONG_CALLING_CONV;
  52. else if( ptr.flag == 3 && !(callConv == asCALL_THISCALL || callConv == asCALL_THISCALL_ASGLOBAL || callConv == asCALL_THISCALL_OBJFIRST || callConv == asCALL_THISCALL_OBJLAST) )
  53. return asWRONG_CALLING_CONV;
  54. }
  55. asDWORD base = callConv;
  56. if( !isMethod )
  57. {
  58. if( base == asCALL_CDECL )
  59. internal->callConv = ICC_CDECL;
  60. else if( base == asCALL_STDCALL )
  61. internal->callConv = ICC_STDCALL;
  62. else if( base == asCALL_THISCALL_ASGLOBAL )
  63. {
  64. if(auxiliary == 0)
  65. return asINVALID_ARG;
  66. internal->auxiliary = auxiliary;
  67. internal->callConv = ICC_THISCALL;
  68. // This is really a thiscall, so it is necessary to check for virtual method pointers
  69. base = asCALL_THISCALL;
  70. isMethod = true;
  71. }
  72. else if (base == asCALL_GENERIC)
  73. {
  74. internal->callConv = ICC_GENERIC_FUNC;
  75. // The auxiliary object is optional for generic calling convention
  76. internal->auxiliary = auxiliary;
  77. }
  78. else
  79. return asNOT_SUPPORTED;
  80. }
  81. if( isMethod )
  82. {
  83. #ifndef AS_NO_CLASS_METHODS
  84. if( base == asCALL_THISCALL || base == asCALL_THISCALL_OBJFIRST || base == asCALL_THISCALL_OBJLAST )
  85. {
  86. internalCallConv thisCallConv;
  87. if( base == asCALL_THISCALL )
  88. {
  89. if(callConv != asCALL_THISCALL_ASGLOBAL && auxiliary)
  90. return asINVALID_ARG;
  91. thisCallConv = ICC_THISCALL;
  92. }
  93. else
  94. {
  95. #ifdef AS_NO_THISCALL_FUNCTOR_METHOD
  96. return asNOT_SUPPORTED;
  97. #else
  98. if(auxiliary == 0)
  99. return asINVALID_ARG;
  100. internal->auxiliary = auxiliary;
  101. if( base == asCALL_THISCALL_OBJFIRST )
  102. thisCallConv = ICC_THISCALL_OBJFIRST;
  103. else //if( base == asCALL_THISCALL_OBJLAST )
  104. thisCallConv = ICC_THISCALL_OBJLAST;
  105. #endif
  106. }
  107. internal->callConv = thisCallConv;
  108. #ifdef GNU_STYLE_VIRTUAL_METHOD
  109. if( (size_t(ptr.ptr.f.func) & 1) )
  110. internal->callConv = (internalCallConv)(thisCallConv + 2);
  111. #endif
  112. internal->baseOffset = ( int )MULTI_BASE_OFFSET(ptr);
  113. #if (defined(AS_ARM64) || defined(AS_ARM) || defined(AS_MIPS)) && (defined(__GNUC__) || defined(AS_PSVITA))
  114. // As the least significant bit in func is used to switch to THUMB mode
  115. // on ARM processors, the LSB in the __delta variable is used instead of
  116. // the one in __pfn on ARM processors.
  117. // MIPS also appear to use the base offset to indicate virtual method.
  118. if( (size_t(internal->baseOffset) & 1) )
  119. internal->callConv = (internalCallConv)(thisCallConv + 2);
  120. #endif
  121. #ifdef HAVE_VIRTUAL_BASE_OFFSET
  122. // We don't support virtual inheritance
  123. if( VIRTUAL_BASE_OFFSET(ptr) != 0 )
  124. return asNOT_SUPPORTED;
  125. #endif
  126. }
  127. else
  128. #endif
  129. if( base == asCALL_CDECL_OBJLAST )
  130. internal->callConv = ICC_CDECL_OBJLAST;
  131. else if( base == asCALL_CDECL_OBJFIRST )
  132. internal->callConv = ICC_CDECL_OBJFIRST;
  133. else if (base == asCALL_GENERIC)
  134. {
  135. internal->callConv = ICC_GENERIC_METHOD;
  136. internal->auxiliary = auxiliary;
  137. }
  138. else
  139. return asNOT_SUPPORTED;
  140. }
  141. return 0;
  142. }
  143. // This function should prepare system functions so that it will be faster to call them
  144. int PrepareSystemFunctionGeneric(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine)
  145. {
  146. asASSERT(internal->callConv == ICC_GENERIC_METHOD || internal->callConv == ICC_GENERIC_FUNC);
  147. // Calculate the size needed for the parameters
  148. internal->paramSize = func->GetSpaceNeededForArguments();
  149. // Prepare the clean up instructions for the function arguments
  150. internal->cleanArgs.SetLength(0);
  151. int offset = 0;
  152. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  153. {
  154. asCDataType &dt = func->parameterTypes[n];
  155. if( (dt.IsObject() || dt.IsFuncdef()) && !dt.IsReference() )
  156. {
  157. if (dt.IsFuncdef())
  158. {
  159. // If the generic call mode is set to old behaviour then always release handles
  160. // else only release the handle if the function is declared with auto handles
  161. if (engine->ep.genericCallMode == 0 || (internal->paramAutoHandles.GetLength() > n && internal->paramAutoHandles[n]))
  162. {
  163. asSSystemFunctionInterface::SClean clean;
  164. clean.op = 0; // call release
  165. clean.ot = &engine->functionBehaviours;
  166. clean.off = short(offset);
  167. internal->cleanArgs.PushLast(clean);
  168. }
  169. }
  170. else if( dt.GetTypeInfo()->flags & asOBJ_REF )
  171. {
  172. // If the generic call mode is set to old behaviour then always release handles
  173. // else only release the handle if the function is declared with auto handles
  174. if (!dt.IsObjectHandle() ||
  175. engine->ep.genericCallMode == 0 ||
  176. (internal->paramAutoHandles.GetLength() > n && internal->paramAutoHandles[n]) )
  177. {
  178. asSTypeBehaviour *beh = &CastToObjectType(dt.GetTypeInfo())->beh;
  179. asASSERT((dt.GetTypeInfo()->flags & asOBJ_NOCOUNT) || beh->release);
  180. if (beh->release)
  181. {
  182. asSSystemFunctionInterface::SClean clean;
  183. clean.op = 0; // call release
  184. clean.ot = CastToObjectType(dt.GetTypeInfo());
  185. clean.off = short(offset);
  186. internal->cleanArgs.PushLast(clean);
  187. }
  188. }
  189. }
  190. else
  191. {
  192. asSSystemFunctionInterface::SClean clean;
  193. clean.op = 1; // call free
  194. clean.ot = CastToObjectType(dt.GetTypeInfo());
  195. clean.off = short(offset);
  196. // Call the destructor then free the memory
  197. asSTypeBehaviour *beh = &CastToObjectType(dt.GetTypeInfo())->beh;
  198. if( beh->destruct )
  199. clean.op = 2; // call destruct, then free
  200. internal->cleanArgs.PushLast(clean);
  201. }
  202. }
  203. if( dt.IsObject() && !dt.IsObjectHandle() && !dt.IsReference() )
  204. offset += AS_PTR_SIZE;
  205. else
  206. offset += dt.GetSizeOnStackDWords();
  207. }
  208. return 0;
  209. }
  210. // This function should prepare system functions so that it will be faster to call them
  211. int PrepareSystemFunction(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine)
  212. {
  213. #ifdef AS_MAX_PORTABILITY
  214. UNUSED_VAR(func);
  215. UNUSED_VAR(internal);
  216. UNUSED_VAR(engine);
  217. // This should never happen, as when AS_MAX_PORTABILITY is on, all functions
  218. // are asCALL_GENERIC, which are prepared by PrepareSystemFunctionGeneric
  219. asASSERT(false);
  220. #else
  221. // References are always returned as primitive data
  222. if( func->returnType.IsReference() || func->returnType.IsObjectHandle() )
  223. {
  224. internal->hostReturnInMemory = false;
  225. internal->hostReturnSize = sizeof(void*)/4;
  226. internal->hostReturnFloat = false;
  227. }
  228. // Registered types have special flags that determine how they are returned
  229. else if( func->returnType.IsObject() )
  230. {
  231. asDWORD objType = func->returnType.GetTypeInfo()->flags;
  232. // Only value types can be returned by value
  233. asASSERT( objType & asOBJ_VALUE );
  234. if( !(objType & (asOBJ_APP_CLASS | asOBJ_APP_PRIMITIVE | asOBJ_APP_FLOAT | asOBJ_APP_ARRAY)) )
  235. {
  236. // If the return is by value then we need to know the true type
  237. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  238. asCString str;
  239. str.Format(TXT_CANNOT_RET_TYPE_s_BY_VAL, func->returnType.GetTypeInfo()->name.AddressOf());
  240. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  241. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  242. }
  243. else if( objType & asOBJ_APP_ARRAY )
  244. {
  245. // Array types are always returned in memory
  246. internal->hostReturnInMemory = true;
  247. internal->hostReturnSize = sizeof(void*)/4;
  248. internal->hostReturnFloat = false;
  249. }
  250. else if( objType & asOBJ_APP_CLASS )
  251. {
  252. internal->hostReturnFloat = false;
  253. if( objType & COMPLEX_RETURN_MASK )
  254. {
  255. internal->hostReturnInMemory = true;
  256. internal->hostReturnSize = sizeof(void*)/4;
  257. }
  258. else
  259. {
  260. #ifdef HAS_128_BIT_PRIMITIVES
  261. if( func->returnType.GetSizeInMemoryDWords() > 4 )
  262. #else
  263. if( func->returnType.GetSizeInMemoryDWords() > 2 )
  264. #endif
  265. {
  266. internal->hostReturnInMemory = true;
  267. internal->hostReturnSize = sizeof(void*)/4;
  268. }
  269. else
  270. {
  271. internal->hostReturnInMemory = false;
  272. internal->hostReturnSize = func->returnType.GetSizeInMemoryDWords();
  273. #ifdef SPLIT_OBJS_BY_MEMBER_TYPES
  274. if( func->returnType.GetTypeInfo()->flags & asOBJ_APP_CLASS_ALLFLOATS )
  275. internal->hostReturnFloat = true;
  276. #endif
  277. }
  278. #ifdef THISCALL_RETURN_SIMPLE_IN_MEMORY
  279. if((internal->callConv == ICC_THISCALL ||
  280. #ifdef AS_NO_THISCALL_FUNCTOR_METHOD
  281. internal->callConv == ICC_VIRTUAL_THISCALL) &&
  282. #else
  283. internal->callConv == ICC_VIRTUAL_THISCALL ||
  284. internal->callConv == ICC_THISCALL_OBJFIRST ||
  285. internal->callConv == ICC_THISCALL_OBJLAST) &&
  286. #endif
  287. func->returnType.GetSizeInMemoryDWords() >= THISCALL_RETURN_SIMPLE_IN_MEMORY_MIN_SIZE)
  288. {
  289. internal->hostReturnInMemory = true;
  290. internal->hostReturnSize = sizeof(void*)/4;
  291. }
  292. #endif
  293. #ifdef CDECL_RETURN_SIMPLE_IN_MEMORY
  294. if((internal->callConv == ICC_CDECL ||
  295. internal->callConv == ICC_CDECL_OBJLAST ||
  296. internal->callConv == ICC_CDECL_OBJFIRST) &&
  297. func->returnType.GetSizeInMemoryDWords() >= CDECL_RETURN_SIMPLE_IN_MEMORY_MIN_SIZE)
  298. {
  299. internal->hostReturnInMemory = true;
  300. internal->hostReturnSize = sizeof(void*)/4;
  301. }
  302. #endif
  303. #ifdef STDCALL_RETURN_SIMPLE_IN_MEMORY
  304. if( internal->callConv == ICC_STDCALL &&
  305. func->returnType.GetSizeInMemoryDWords() >= STDCALL_RETURN_SIMPLE_IN_MEMORY_MIN_SIZE)
  306. {
  307. internal->hostReturnInMemory = true;
  308. internal->hostReturnSize = sizeof(void*)/4;
  309. }
  310. #endif
  311. }
  312. #ifdef SPLIT_OBJS_BY_MEMBER_TYPES
  313. // It's not safe to return objects by value because different registers
  314. // will be used depending on the memory layout of the object.
  315. // Ref: http://www.x86-64.org/documentation/abi.pdf
  316. // Ref: http://www.agner.org/optimize/calling_conventions.pdf
  317. // If the application informs that the class should be treated as all integers, then we allow it
  318. if( !internal->hostReturnInMemory &&
  319. !(func->returnType.GetTypeInfo()->flags & (asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
  320. {
  321. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  322. asCString str;
  323. str.Format(TXT_DONT_SUPPORT_RET_TYPE_s_BY_VAL, func->returnType.Format(func->nameSpace).AddressOf());
  324. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  325. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  326. }
  327. #endif
  328. }
  329. else if( objType & asOBJ_APP_PRIMITIVE )
  330. {
  331. internal->hostReturnInMemory = false;
  332. internal->hostReturnSize = func->returnType.GetSizeInMemoryDWords();
  333. internal->hostReturnFloat = false;
  334. }
  335. else if( objType & asOBJ_APP_FLOAT )
  336. {
  337. internal->hostReturnInMemory = false;
  338. internal->hostReturnSize = func->returnType.GetSizeInMemoryDWords();
  339. internal->hostReturnFloat = true;
  340. }
  341. }
  342. // Primitive types can easily be determined
  343. #ifdef HAS_128_BIT_PRIMITIVES
  344. else if( func->returnType.GetSizeInMemoryDWords() > 4 )
  345. {
  346. // Shouldn't be possible to get here
  347. asASSERT(false);
  348. }
  349. else if( func->returnType.GetSizeInMemoryDWords() == 4 )
  350. {
  351. internal->hostReturnInMemory = false;
  352. internal->hostReturnSize = 4;
  353. internal->hostReturnFloat = false;
  354. }
  355. #else
  356. else if( func->returnType.GetSizeInMemoryDWords() > 2 )
  357. {
  358. // Shouldn't be possible to get here
  359. asASSERT(false);
  360. }
  361. #endif
  362. else if( func->returnType.GetSizeInMemoryDWords() == 2 )
  363. {
  364. internal->hostReturnInMemory = false;
  365. internal->hostReturnSize = 2;
  366. internal->hostReturnFloat = func->returnType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttDouble, true));
  367. }
  368. else if( func->returnType.GetSizeInMemoryDWords() == 1 )
  369. {
  370. internal->hostReturnInMemory = false;
  371. internal->hostReturnSize = 1;
  372. internal->hostReturnFloat = func->returnType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttFloat, true));
  373. }
  374. else
  375. {
  376. internal->hostReturnInMemory = false;
  377. internal->hostReturnSize = 0;
  378. internal->hostReturnFloat = false;
  379. }
  380. // Calculate the size needed for the parameters
  381. internal->paramSize = func->GetSpaceNeededForArguments();
  382. // Verify if the function takes any objects by value
  383. asUINT n;
  384. internal->takesObjByVal = false;
  385. for( n = 0; n < func->parameterTypes.GetLength(); n++ )
  386. {
  387. if( func->parameterTypes[n].IsObject() && !func->parameterTypes[n].IsObjectHandle() && !func->parameterTypes[n].IsReference() )
  388. {
  389. internal->takesObjByVal = true;
  390. // Can't pass objects by value unless the application type is informed
  391. if( !(func->parameterTypes[n].GetTypeInfo()->flags & (asOBJ_APP_CLASS | asOBJ_APP_PRIMITIVE | asOBJ_APP_FLOAT | asOBJ_APP_ARRAY)) )
  392. {
  393. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  394. asCString str;
  395. str.Format(TXT_CANNOT_PASS_TYPE_s_BY_VAL, func->parameterTypes[n].GetTypeInfo()->name.AddressOf());
  396. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  397. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  398. }
  399. #ifdef SPLIT_OBJS_BY_MEMBER_TYPES
  400. // It's not safe to pass objects by value because different registers
  401. // will be used depending on the memory layout of the object
  402. // Ref: http://www.x86-64.org/documentation/abi.pdf
  403. // Ref: http://www.agner.org/optimize/calling_conventions.pdf
  404. if(
  405. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  406. !(func->parameterTypes[n].GetTypeInfo()->flags & COMPLEX_MASK) &&
  407. #endif
  408. #ifdef LARGE_OBJS_PASS_BY_REF
  409. func->parameterTypes[n].GetSizeInMemoryDWords() < AS_LARGE_OBJ_MIN_SIZE &&
  410. #endif
  411. !(func->parameterTypes[n].GetTypeInfo()->flags & (asOBJ_APP_PRIMITIVE | asOBJ_APP_FLOAT | asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
  412. {
  413. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  414. asCString str;
  415. str.Format(TXT_DONT_SUPPORT_TYPE_s_BY_VAL, func->parameterTypes[n].GetTypeInfo()->name.AddressOf());
  416. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  417. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  418. }
  419. #endif
  420. break;
  421. }
  422. }
  423. // Prepare the clean up instructions for the function arguments
  424. internal->cleanArgs.SetLength(0);
  425. int offset = 0;
  426. for( n = 0; n < func->parameterTypes.GetLength(); n++ )
  427. {
  428. asCDataType &dt = func->parameterTypes[n];
  429. #if defined(COMPLEX_OBJS_PASSED_BY_REF) || defined(AS_LARGE_OBJS_PASSED_BY_REF)
  430. bool needFree = false;
  431. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  432. if( dt.GetTypeInfo() && dt.GetTypeInfo()->flags & COMPLEX_MASK ) needFree = true;
  433. #endif
  434. #ifdef AS_LARGE_OBJS_PASSED_BY_REF
  435. if( dt.GetSizeInMemoryDWords() >= AS_LARGE_OBJ_MIN_SIZE ) needFree = true;
  436. #endif
  437. if( needFree &&
  438. dt.IsObject() &&
  439. !dt.IsObjectHandle() &&
  440. !dt.IsReference() )
  441. {
  442. asSSystemFunctionInterface::SClean clean;
  443. clean.op = 1; // call free
  444. clean.ot = CastToObjectType(dt.GetTypeInfo());
  445. clean.off = short(offset);
  446. #ifndef AS_CALLEE_DESTROY_OBJ_BY_VAL
  447. // If the called function doesn't destroy objects passed by value we must do so here
  448. asSTypeBehaviour *beh = &CastToObjectType(dt.GetTypeInfo())->beh;
  449. if( beh->destruct )
  450. clean.op = 2; // call destruct, then free
  451. #endif
  452. internal->cleanArgs.PushLast(clean);
  453. }
  454. #endif
  455. if( n < internal->paramAutoHandles.GetLength() && internal->paramAutoHandles[n] )
  456. {
  457. asSSystemFunctionInterface::SClean clean;
  458. clean.op = 0; // call release
  459. if (dt.IsFuncdef())
  460. clean.ot = &engine->functionBehaviours;
  461. else
  462. clean.ot = CastToObjectType(dt.GetTypeInfo());
  463. clean.off = short(offset);
  464. internal->cleanArgs.PushLast(clean);
  465. }
  466. if( dt.IsObject() && !dt.IsObjectHandle() && !dt.IsReference() )
  467. offset += AS_PTR_SIZE;
  468. else
  469. offset += dt.GetSizeOnStackDWords();
  470. }
  471. #endif // !defined(AS_MAX_PORTABILITY)
  472. return 0;
  473. }
  474. #ifdef AS_MAX_PORTABILITY
  475. int CallSystemFunction(int id, asCContext *context)
  476. {
  477. asCScriptEngine *engine = context->m_engine;
  478. asCScriptFunction *func = engine->scriptFunctions[id];
  479. asSSystemFunctionInterface *sysFunc = func->sysFuncIntf;
  480. int callConv = sysFunc->callConv;
  481. if( callConv == ICC_GENERIC_FUNC || callConv == ICC_GENERIC_METHOD )
  482. return context->CallGeneric(func);
  483. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  484. return 0;
  485. }
  486. #else
  487. //
  488. // CallSystemFunctionNative
  489. //
  490. // This function is implemented for each platform where the native calling conventions is supported.
  491. // See the various as_callfunc_xxx.cpp files for their implementation. It is responsible for preparing
  492. // the arguments for the function call, calling the function, and then retrieving the return value.
  493. //
  494. // Parameters:
  495. //
  496. // context - This is the context that can be used to retrieve specific information from the engine
  497. // descr - This is the script function object that holds the information on how to call the function
  498. // obj - This is the object pointer, if the call is for a class method, otherwise it is null
  499. // args - This is the function arguments, which are packed as in AngelScript
  500. // retPointer - This points to a the memory buffer where the return object is to be placed, if the function returns the value in memory rather than in registers
  501. // retQW2 - This output parameter should be used if the function returns a value larger than 64bits in registers
  502. // secondObj - This is the object pointer that the proxy method should invoke its method on when the call convention is THISCALL_OBJFIRST/LAST
  503. //
  504. // Return value:
  505. //
  506. // The function should return the value that is returned in registers.
  507. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &retQW2, void *secondObj);
  508. int CallSystemFunction(int id, asCContext *context)
  509. {
  510. asCScriptEngine *engine = context->m_engine;
  511. asCScriptFunction *descr = engine->scriptFunctions[id];
  512. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  513. int callConv = sysFunc->callConv;
  514. if( callConv == ICC_GENERIC_FUNC || callConv == ICC_GENERIC_METHOD )
  515. return context->CallGeneric(descr);
  516. asQWORD retQW = 0;
  517. asQWORD retQW2 = 0;
  518. asDWORD *args = context->m_regs.stackPointer;
  519. void *retPointer = 0;
  520. int popSize = sysFunc->paramSize;
  521. // TODO: clean-up: CallSystemFunctionNative should have two arguments for object pointers
  522. // objForThiscall is the object pointer that should be used for the thiscall
  523. // objForArg is the object pointer that should be passed as argument when using OBJFIRST or OBJLAST
  524. // Used to save two object pointers with THISCALL_OBJLAST or THISCALL_OBJFIRST
  525. void *obj = 0;
  526. void *secondObj = 0;
  527. #ifdef AS_NO_THISCALL_FUNCTOR_METHOD
  528. if( callConv >= ICC_THISCALL )
  529. {
  530. if(sysFunc->auxiliary)
  531. {
  532. // This class method is being called as if it is a global function
  533. obj = sysFunc->auxiliary;
  534. }
  535. else
  536. {
  537. // The object pointer should be popped from the context stack
  538. popSize += AS_PTR_SIZE;
  539. // Check for null pointer
  540. obj = (void*)*(asPWORD*)(args);
  541. if( obj == 0 )
  542. {
  543. context->SetInternalException(TXT_NULL_POINTER_ACCESS);
  544. return 0;
  545. }
  546. // Skip the object pointer
  547. args += AS_PTR_SIZE;
  548. }
  549. // Add the base offset for multiple inheritance
  550. #if (defined(__GNUC__) && (defined(AS_ARM64) || defined(AS_ARM) || defined(AS_MIPS))) || defined(AS_PSVITA)
  551. // On GNUC + ARM the lsb of the offset is used to indicate a virtual function
  552. // and the whole offset is thus shifted one bit left to keep the original
  553. // offset resolution
  554. // MIPS also work like ARM in this regard
  555. obj = (void*)(asPWORD(obj) + (sysFunc->baseOffset>>1));
  556. #else
  557. obj = (void*)(asPWORD(obj) + sysFunc->baseOffset);
  558. #endif
  559. }
  560. #else // !defined(AS_NO_THISCALL_FUNCTOR_METHOD)
  561. if( callConv >= ICC_THISCALL )
  562. {
  563. bool continueCheck = true; // True if need check objectPointer or context stack for object
  564. int continueCheckIndex = 0; // Index into objectsPtrs to save the object if continueCheck
  565. if( callConv >= ICC_THISCALL_OBJLAST )
  566. {
  567. asASSERT( sysFunc->auxiliary != 0 );
  568. // This class method is being called as object method (sysFunc->auxiliary must be set).
  569. obj = sysFunc->auxiliary;
  570. continueCheckIndex = 1;
  571. }
  572. else if(sysFunc->auxiliary)
  573. {
  574. // This class method is being called as if it is a global function
  575. obj = sysFunc->auxiliary;
  576. continueCheck = false;
  577. }
  578. if( obj )
  579. {
  580. // Add the base offset for multiple inheritance
  581. #if (defined(__GNUC__) && (defined(AS_ARM64) || defined(AS_ARM) || defined(AS_MIPS))) || defined(AS_PSVITA)
  582. // On GNUC + ARM the lsb of the offset is used to indicate a virtual function
  583. // and the whole offset is thus shifted one bit left to keep the original
  584. // offset resolution
  585. // MIPS also work like ARM in this regard
  586. obj = (void*)(asPWORD(obj) + (sysFunc->baseOffset>>1));
  587. #else
  588. obj = (void*)(asPWORD(obj) + sysFunc->baseOffset);
  589. #endif
  590. }
  591. if( continueCheck )
  592. {
  593. void *tempPtr = 0;
  594. // The object pointer should be popped from the context stack
  595. popSize += AS_PTR_SIZE;
  596. // Check for null pointer
  597. tempPtr = (void*)*(asPWORD*)(args);
  598. if( tempPtr == 0 )
  599. {
  600. context->SetInternalException(TXT_NULL_POINTER_ACCESS);
  601. return 0;
  602. }
  603. // Add the base offset for multiple inheritance
  604. #if (defined(__GNUC__) && (defined(AS_ARM64) || defined(AS_ARM) || defined(AS_MIPS))) || defined(AS_PSVITA)
  605. // On GNUC + ARM the lsb of the offset is used to indicate a virtual function
  606. // and the whole offset is thus shifted one bit left to keep the original
  607. // offset resolution
  608. // MIPS also work like ARM in this regard
  609. tempPtr = (void*)(asPWORD(tempPtr) + (sysFunc->baseOffset>>1));
  610. #else
  611. tempPtr = (void*)(asPWORD(tempPtr) + sysFunc->baseOffset);
  612. #endif
  613. // Skip the object pointer
  614. args += AS_PTR_SIZE;
  615. if( continueCheckIndex )
  616. secondObj = tempPtr;
  617. else
  618. {
  619. asASSERT( obj == 0 );
  620. obj = tempPtr;
  621. }
  622. }
  623. }
  624. #endif // AS_NO_THISCALL_FUNCTOR_METHOD
  625. if( descr->DoesReturnOnStack() )
  626. {
  627. // Get the address of the location for the return value from the stack
  628. retPointer = (void*)*(asPWORD*)(args);
  629. popSize += AS_PTR_SIZE;
  630. args += AS_PTR_SIZE;
  631. // When returning the value on the location allocated by the called
  632. // we shouldn't set the object type in the register
  633. context->m_regs.objectType = 0;
  634. }
  635. else
  636. {
  637. // Set the object type of the reference held in the register
  638. context->m_regs.objectType = descr->returnType.GetTypeInfo();
  639. }
  640. // For composition we need to add the offset and/or dereference the pointer
  641. if(obj)
  642. {
  643. obj = (void*) ((char*) obj + sysFunc->compositeOffset);
  644. if(sysFunc->isCompositeIndirect) obj = *((void**)obj);
  645. }
  646. context->m_callingSystemFunction = descr;
  647. bool cppException = false;
  648. #ifdef AS_NO_EXCEPTIONS
  649. retQW = CallSystemFunctionNative(context, descr, obj, args, sysFunc->hostReturnInMemory ? retPointer : 0, retQW2, secondObj);
  650. #else
  651. // This try/catch block is to catch potential exception that may
  652. // be thrown by the registered function. The implementation of the
  653. // CallSystemFunctionNative() must make sure not to have any manual
  654. // clean-up after the call to the real function, or that won't be
  655. // executed in case of an exception.
  656. try
  657. {
  658. retQW = CallSystemFunctionNative(context, descr, obj, args, sysFunc->hostReturnInMemory ? retPointer : 0, retQW2, secondObj);
  659. }
  660. catch(...)
  661. {
  662. cppException = true;
  663. // Convert the exception to a script exception so the VM can
  664. // properly report the error to the application and then clean up
  665. context->HandleAppException();
  666. }
  667. #endif
  668. context->m_callingSystemFunction = 0;
  669. // Store the returned value in our stack
  670. if( (descr->returnType.IsObject() || descr->returnType.IsFuncdef()) && !descr->returnType.IsReference() )
  671. {
  672. if( descr->returnType.IsObjectHandle() )
  673. {
  674. #if defined(AS_BIG_ENDIAN) && AS_PTR_SIZE == 1
  675. // Since we're treating the system function as if it is returning a QWORD we are
  676. // actually receiving the value in the high DWORD of retQW.
  677. retQW >>= 32;
  678. #endif
  679. context->m_regs.objectRegister = (void*)(asPWORD)retQW;
  680. if( sysFunc->returnAutoHandle && context->m_regs.objectRegister )
  681. {
  682. asASSERT( !(descr->returnType.GetTypeInfo()->flags & asOBJ_NOCOUNT) );
  683. engine->CallObjectMethod(context->m_regs.objectRegister, CastToObjectType(descr->returnType.GetTypeInfo())->beh.addref);
  684. }
  685. }
  686. else
  687. {
  688. asASSERT( retPointer );
  689. if( !sysFunc->hostReturnInMemory )
  690. {
  691. // Copy the returned value to the pointer sent by the script engine
  692. if( sysFunc->hostReturnSize == 1 )
  693. {
  694. #if defined(AS_BIG_ENDIAN) && AS_PTR_SIZE == 1
  695. // Since we're treating the system function as if it is returning a QWORD we are
  696. // actually receiving the value in the high DWORD of retQW.
  697. retQW >>= 32;
  698. #endif
  699. *(asDWORD*)retPointer = (asDWORD)retQW;
  700. }
  701. else if( sysFunc->hostReturnSize == 2 )
  702. *(asQWORD*)retPointer = retQW;
  703. else if( sysFunc->hostReturnSize == 3 )
  704. {
  705. *(asQWORD*)retPointer = retQW;
  706. *(((asDWORD*)retPointer) + 2) = (asDWORD)retQW2;
  707. }
  708. else // if( sysFunc->hostReturnSize == 4 )
  709. {
  710. *(asQWORD*)retPointer = retQW;
  711. *(((asQWORD*)retPointer) + 1) = retQW2;
  712. }
  713. }
  714. if( context->m_status == asEXECUTION_EXCEPTION && !cppException )
  715. {
  716. // If the function raised a script exception it really shouldn't have
  717. // initialized the object. However, as it is a soft exception there is
  718. // no way for the application to not return a value, so instead we simply
  719. // destroy it here, to pretend it was never created.
  720. if(CastToObjectType(descr->returnType.GetTypeInfo())->beh.destruct )
  721. engine->CallObjectMethod(retPointer, CastToObjectType(descr->returnType.GetTypeInfo())->beh.destruct);
  722. }
  723. }
  724. }
  725. else
  726. {
  727. // Store value in value register
  728. if( sysFunc->hostReturnSize == 1 )
  729. {
  730. #if defined(AS_BIG_ENDIAN)
  731. // Since we're treating the system function as if it is returning a QWORD we are
  732. // actually receiving the value in the high DWORD of retQW.
  733. retQW >>= 32;
  734. // Due to endian issues we need to handle return values that are
  735. // less than a DWORD (32 bits) in size specially
  736. int numBytes = descr->returnType.GetSizeInMemoryBytes();
  737. if( descr->returnType.IsReference() ) numBytes = 4;
  738. switch( numBytes )
  739. {
  740. case 1:
  741. {
  742. // 8 bits
  743. asBYTE *val = (asBYTE*)&context->m_regs.valueRegister;
  744. val[0] = (asBYTE)retQW;
  745. val[1] = 0;
  746. val[2] = 0;
  747. val[3] = 0;
  748. val[4] = 0;
  749. val[5] = 0;
  750. val[6] = 0;
  751. val[7] = 0;
  752. }
  753. break;
  754. case 2:
  755. {
  756. // 16 bits
  757. asWORD *val = (asWORD*)&context->m_regs.valueRegister;
  758. val[0] = (asWORD)retQW;
  759. val[1] = 0;
  760. val[2] = 0;
  761. val[3] = 0;
  762. }
  763. break;
  764. default:
  765. {
  766. // 32 bits
  767. asDWORD *val = (asDWORD*)&context->m_regs.valueRegister;
  768. val[0] = (asDWORD)retQW;
  769. val[1] = 0;
  770. }
  771. break;
  772. }
  773. #else
  774. *(asDWORD*)&context->m_regs.valueRegister = (asDWORD)retQW;
  775. #endif
  776. }
  777. else
  778. context->m_regs.valueRegister = retQW;
  779. }
  780. // Clean up arguments
  781. const asUINT cleanCount = sysFunc->cleanArgs.GetLength();
  782. if( cleanCount )
  783. {
  784. args = context->m_regs.stackPointer;
  785. // Skip the hidden argument for the return pointer
  786. // TODO: runtime optimize: This check and increment should have been done in PrepareSystemFunction
  787. if( descr->DoesReturnOnStack() )
  788. args += AS_PTR_SIZE;
  789. // Skip the object pointer on the stack
  790. // TODO: runtime optimize: This check and increment should have been done in PrepareSystemFunction
  791. if( callConv >= ICC_THISCALL && sysFunc->auxiliary == 0 )
  792. args += AS_PTR_SIZE;
  793. asSSystemFunctionInterface::SClean *clean = sysFunc->cleanArgs.AddressOf();
  794. for( asUINT n = 0; n < cleanCount; n++, clean++ )
  795. {
  796. void **addr = (void**)&args[clean->off];
  797. if( clean->op == 0 )
  798. {
  799. if( *addr != 0 )
  800. {
  801. engine->CallObjectMethod(*addr, clean->ot->beh.release);
  802. *addr = 0;
  803. }
  804. }
  805. else
  806. {
  807. asASSERT( clean->op == 1 || clean->op == 2 );
  808. asASSERT( *addr );
  809. if( clean->op == 2 )
  810. engine->CallObjectMethod(*addr, clean->ot->beh.destruct);
  811. engine->CallFree(*addr);
  812. }
  813. }
  814. }
  815. return popSize;
  816. }
  817. #endif // AS_MAX_PORTABILITY
  818. END_AS_NAMESPACE