as_callfunc.cpp 29 KB

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