as_callfunc.cpp 27 KB

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