as_callfunc.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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, 0, 0, 0);
  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, 0, 0, 0);
  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, 0, 0, 0);
  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. if(
  283. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  284. !(func->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK) &&
  285. #endif
  286. #ifdef LARGE_OBJS_PASS_BY_REF
  287. func->parameterTypes[n].GetSizeInMemoryDWords() < AS_LARGE_OBJ_MIN_SIZE &&
  288. #endif
  289. !(func->parameterTypes[n].GetObjectType()->flags & (asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
  290. {
  291. engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
  292. asCString str;
  293. str.Format(TXT_DONT_SUPPORT_TYPE_s_BY_VAL, func->parameterTypes[n].GetObjectType()->name.AddressOf());
  294. engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
  295. engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);
  296. }
  297. #endif
  298. break;
  299. }
  300. }
  301. // Verify if the function has any registered autohandles
  302. internal->hasAutoHandles = false;
  303. for( n = 0; n < internal->paramAutoHandles.GetLength(); n++ )
  304. {
  305. if( internal->paramAutoHandles[n] )
  306. {
  307. internal->hasAutoHandles = true;
  308. break;
  309. }
  310. }
  311. return 0;
  312. }
  313. #ifdef AS_MAX_PORTABILITY
  314. int CallSystemFunction(int id, asCContext *context, void *objectPointer)
  315. {
  316. asCScriptEngine *engine = context->engine;
  317. asSSystemFunctionInterface *sysFunc = engine->scriptFunctions[id]->sysFuncIntf;
  318. int callConv = sysFunc->callConv;
  319. if( callConv == ICC_GENERIC_FUNC || callConv == ICC_GENERIC_METHOD )
  320. return context->CallGeneric(id, objectPointer);
  321. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  322. return 0;
  323. }
  324. #else
  325. //
  326. // CallSystemFunctionNative
  327. //
  328. // This function is implemented for each platform where the native calling conventions is supported.
  329. // See the various as_callfunc_xxx.cpp files for their implementation. It is responsible for preparing
  330. // the arguments for the function call, calling the function, and then retrieving the return value.
  331. //
  332. // Parameters:
  333. //
  334. // context - This is the context that can be used to retrieve specific information from the engine
  335. // descr - This is the script function object that holds the information on how to call the function
  336. // obj - This is the object pointer, if the call is for a class method, otherwise it is null
  337. // args - This is the function arguments, which are packed as in AngelScript
  338. // 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
  339. // retQW2 - This output parameter should be used if the function returns a value larger than 64bits in registers
  340. //
  341. // Return value:
  342. //
  343. // The function should return the value that is returned in registers.
  344. //
  345. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &retQW2);
  346. int CallSystemFunction(int id, asCContext *context, void *objectPointer)
  347. {
  348. asCScriptEngine *engine = context->engine;
  349. asCScriptFunction *descr = engine->scriptFunctions[id];
  350. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  351. int callConv = sysFunc->callConv;
  352. if( callConv == ICC_GENERIC_FUNC || callConv == ICC_GENERIC_METHOD )
  353. return context->CallGeneric(id, objectPointer);
  354. asQWORD retQW = 0;
  355. asQWORD retQW2 = 0;
  356. asDWORD *args = context->regs.stackPointer;
  357. void *retPointer = 0;
  358. void *obj = 0;
  359. int popSize = sysFunc->paramSize;
  360. if( callConv >= ICC_THISCALL )
  361. {
  362. if( objectPointer )
  363. {
  364. obj = objectPointer;
  365. }
  366. else
  367. {
  368. // The object pointer should be popped from the context stack
  369. popSize += AS_PTR_SIZE;
  370. // Check for null pointer
  371. obj = (void*)*(size_t*)(args);
  372. if( obj == 0 )
  373. {
  374. context->SetInternalException(TXT_NULL_POINTER_ACCESS);
  375. if( retPointer )
  376. engine->CallFree(retPointer);
  377. return 0;
  378. }
  379. // Add the base offset for multiple inheritance
  380. #if defined(__GNUC__) && defined(AS_ARM)
  381. // On GNUC + ARM the lsb of the offset is used to indicate a virtual function
  382. // and the whole offset is thus shifted one bit left to keep the original
  383. // offset resolution
  384. obj = (void*)(size_t(obj) + (sysFunc->baseOffset>>1));
  385. #else
  386. obj = (void*)(size_t(obj) + sysFunc->baseOffset);
  387. #endif
  388. // Skip the object pointer
  389. args += AS_PTR_SIZE;
  390. }
  391. }
  392. context->regs.objectType = descr->returnType.GetObjectType();
  393. if( descr->returnType.IsObject() && !descr->returnType.IsReference() && !descr->returnType.IsObjectHandle() )
  394. {
  395. #ifndef AS_OLD
  396. // Get the address of the location for the return value from the stack
  397. retPointer = (void*)*(size_t*)(args);
  398. popSize += AS_PTR_SIZE;
  399. args += AS_PTR_SIZE;
  400. // When returning the value on the location allocated by the called we shouldn't set the object type in the register
  401. context->regs.objectType = 0;
  402. #else
  403. // Allocate the memory for the object
  404. retPointer = engine->CallAlloc(descr->returnType.GetObjectType());
  405. #endif
  406. }
  407. retQW = CallSystemFunctionNative(context, descr, obj, args, sysFunc->hostReturnInMemory ? retPointer : 0, retQW2);
  408. #if defined(COMPLEX_OBJS_PASSED_BY_REF) || defined(AS_LARGE_OBJS_PASSED_BY_REF)
  409. if( sysFunc->takesObjByVal )
  410. {
  411. // Need to free the complex objects passed by value, but that the
  412. // calling convention implicitly passes by reference behind the scene
  413. args = context->regs.stackPointer;
  414. if( callConv >= ICC_THISCALL && !objectPointer )
  415. args += AS_PTR_SIZE;
  416. int spos = 0;
  417. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  418. {
  419. if( descr->parameterTypes[n].IsObject() &&
  420. !descr->parameterTypes[n].IsObjectHandle() &&
  421. !descr->parameterTypes[n].IsReference() && (
  422. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  423. (descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK) ||
  424. #endif
  425. #ifdef AS_LARGE_OBJS_PASSED_BY_REF
  426. (descr->parameterTypes[n].GetSizeInMemoryDWords() >= AS_LARGE_OBJ_MIN_SIZE) ||
  427. #endif
  428. 0) )
  429. {
  430. void *obj = (void*)*(size_t*)&args[spos];
  431. spos += AS_PTR_SIZE;
  432. #ifndef AS_CALLEE_DESTROY_OBJ_BY_VAL
  433. // If the called function doesn't destroy objects passed by value we must do so here
  434. asSTypeBehaviour *beh = &descr->parameterTypes[n].GetObjectType()->beh;
  435. if( beh->destruct )
  436. engine->CallObjectMethod(obj, beh->destruct);
  437. #endif
  438. engine->CallFree(obj);
  439. }
  440. else
  441. spos += descr->parameterTypes[n].GetSizeOnStackDWords();
  442. }
  443. }
  444. #endif
  445. // Store the returned value in our stack
  446. if( descr->returnType.IsObject() && !descr->returnType.IsReference() )
  447. {
  448. if( descr->returnType.IsObjectHandle() )
  449. {
  450. #if defined(AS_BIG_ENDIAN) && AS_PTR_SIZE == 1
  451. // Since we're treating the system function as if it is returning a QWORD we are
  452. // actually receiving the value in the high DWORD of retQW.
  453. retQW >>= 32;
  454. #endif
  455. context->regs.objectRegister = (void*)(size_t)retQW;
  456. if( sysFunc->returnAutoHandle && context->regs.objectRegister )
  457. engine->CallObjectMethod(context->regs.objectRegister, descr->returnType.GetObjectType()->beh.addref);
  458. }
  459. else
  460. {
  461. if( !sysFunc->hostReturnInMemory )
  462. {
  463. // Copy the returned value to the pointer sent by the script engine
  464. if( sysFunc->hostReturnSize == 1 )
  465. {
  466. #if defined(AS_BIG_ENDIAN) && AS_PTR_SIZE == 1
  467. // Since we're treating the system function as if it is returning a QWORD we are
  468. // actually receiving the value in the high DWORD of retQW.
  469. retQW >>= 32;
  470. #endif
  471. *(asDWORD*)retPointer = (asDWORD)retQW;
  472. }
  473. else if( sysFunc->hostReturnSize == 2 )
  474. *(asQWORD*)retPointer = retQW;
  475. else if( sysFunc->hostReturnSize == 3 )
  476. {
  477. *(asQWORD*)retPointer = retQW;
  478. *(((asDWORD*)retPointer) + 2) = (asDWORD)retQW2;
  479. }
  480. else // if( sysFunc->hostReturnSize == 4 )
  481. {
  482. *(asQWORD*)retPointer = retQW;
  483. *(((asQWORD*)retPointer) + 1) = retQW2;
  484. }
  485. }
  486. // Store the object in the register
  487. context->regs.objectRegister = retPointer;
  488. #ifndef AS_OLD
  489. // If the value is returned on the stack we shouldn't update the object register
  490. if( descr->DoesReturnOnStack() )
  491. {
  492. context->regs.objectRegister = 0;
  493. if( context->status == asEXECUTION_EXCEPTION )
  494. {
  495. // If the function raised a script exception it really shouldn't have
  496. // initialized the object. However, as it is a soft exception there is
  497. // no way for the application to not return a value, so instead we simply
  498. // destroy it here, to pretend it was never created.
  499. if( descr->returnType.GetObjectType()->beh.destruct )
  500. engine->CallObjectMethod(retPointer, descr->returnType.GetObjectType()->beh.destruct);
  501. }
  502. }
  503. #endif
  504. }
  505. }
  506. else
  507. {
  508. // Store value in value register
  509. if( sysFunc->hostReturnSize == 1 )
  510. {
  511. #if defined(AS_BIG_ENDIAN)
  512. // Since we're treating the system function as if it is returning a QWORD we are
  513. // actually receiving the value in the high DWORD of retQW.
  514. retQW >>= 32;
  515. // Due to endian issues we need to handle return values that are
  516. // less than a DWORD (32 bits) in size specially
  517. int numBytes = descr->returnType.GetSizeInMemoryBytes();
  518. if( descr->returnType.IsReference() ) numBytes = 4;
  519. switch( numBytes )
  520. {
  521. case 1:
  522. {
  523. // 8 bits
  524. asBYTE *val = (asBYTE*)&context->regs.valueRegister;
  525. val[0] = (asBYTE)retQW;
  526. val[1] = 0;
  527. val[2] = 0;
  528. val[3] = 0;
  529. val[4] = 0;
  530. val[5] = 0;
  531. val[6] = 0;
  532. val[7] = 0;
  533. }
  534. break;
  535. case 2:
  536. {
  537. // 16 bits
  538. asWORD *val = (asWORD*)&context->regs.valueRegister;
  539. val[0] = (asWORD)retQW;
  540. val[1] = 0;
  541. val[2] = 0;
  542. val[3] = 0;
  543. }
  544. break;
  545. default:
  546. {
  547. // 32 bits
  548. asDWORD *val = (asDWORD*)&context->regs.valueRegister;
  549. val[0] = (asDWORD)retQW;
  550. val[1] = 0;
  551. }
  552. break;
  553. }
  554. #else
  555. *(asDWORD*)&context->regs.valueRegister = (asDWORD)retQW;
  556. #endif
  557. }
  558. else
  559. context->regs.valueRegister = retQW;
  560. }
  561. // Release autohandles in the arguments
  562. if( sysFunc->hasAutoHandles )
  563. {
  564. args = context->regs.stackPointer;
  565. if( callConv >= ICC_THISCALL && !objectPointer )
  566. args += AS_PTR_SIZE;
  567. int spos = 0;
  568. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  569. {
  570. if( sysFunc->paramAutoHandles[n] && *(size_t*)&args[spos] != 0 )
  571. {
  572. // Call the release method on the type
  573. engine->CallObjectMethod((void*)*(size_t*)&args[spos], descr->parameterTypes[n].GetObjectType()->beh.release);
  574. *(size_t*)&args[spos] = 0;
  575. }
  576. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  577. spos += AS_PTR_SIZE;
  578. else
  579. spos += descr->parameterTypes[n].GetSizeOnStackDWords();
  580. }
  581. }
  582. return popSize;
  583. }
  584. #endif // AS_MAX_PORTABILITY
  585. END_AS_NAMESPACE