as_callfunc.cpp 28 KB

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