as_callfunc.cpp 23 KB

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