as_callfunc.cpp 21 KB

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