as_callfunc_arm.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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_arm.cpp
  25. //
  26. // These functions handle the actual calling of system functions on the arm platform
  27. //
  28. // Written by Fredrik Ehnbom in June 2009, based on as_callfunc_x86.cpp
  29. //
  30. // The code was complemented to support Linux with ARM by Carlos Luna in December, 2012.
  31. //
  32. // Added support for functor methods by Jordi Oliveras Rovira in April, 2014.
  33. // This code has to conform to both AAPCS and the modified ABI for iOS
  34. //
  35. // Reference:
  36. //
  37. // AAPCS: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042d/IHI0042D_aapcs.pdf
  38. // iOS: http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iPhoneOSABIReference/iPhoneOSABIReference.pdf
  39. #include "as_config.h"
  40. #ifndef AS_MAX_PORTABILITY
  41. #ifdef AS_ARM
  42. #include "as_callfunc.h"
  43. #include "as_scriptengine.h"
  44. #include "as_texts.h"
  45. #include "as_tokendef.h"
  46. #include "as_context.h"
  47. #if defined(AS_SOFTFP)
  48. // This code supports the soft-float ABI, i.e. g++ -mfloat-abi=softfp
  49. //
  50. // The code for iOS, Android, Marmalade and Windows Phone goes here
  51. BEGIN_AS_NAMESPACE
  52. extern "C" asQWORD armFunc (const asDWORD *, int, asFUNCTION_t);
  53. extern "C" asQWORD armFuncR0 (const asDWORD *, int, asFUNCTION_t, asDWORD r0);
  54. extern "C" asQWORD armFuncR0R1 (const asDWORD *, int, asFUNCTION_t, asDWORD r0, asDWORD r1);
  55. extern "C" asQWORD armFuncObjLast (const asDWORD *, int, asFUNCTION_t, asDWORD obj);
  56. extern "C" asQWORD armFuncR0ObjLast (const asDWORD *, int, asFUNCTION_t, asDWORD r0, asDWORD obj);
  57. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/)
  58. {
  59. asCScriptEngine *engine = context->m_engine;
  60. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  61. int callConv = sysFunc->callConv;
  62. asQWORD retQW = 0;
  63. asFUNCTION_t func = sysFunc->func;
  64. int paramSize = sysFunc->paramSize;
  65. asFUNCTION_t *vftable;
  66. if( sysFunc->hostReturnInMemory )
  67. {
  68. // The return is made in memory
  69. callConv++;
  70. }
  71. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  72. // Unpack the two object pointers
  73. void **objectsPtrs = (void**)obj;
  74. void *secondObject = objectsPtrs[1];
  75. obj = objectsPtrs[0];
  76. bool isThisCallMethod = callConv >= ICC_THISCALL_OBJLAST;
  77. #endif
  78. asDWORD paramBuffer[64+2];
  79. // Android & Linux needs to align 64bit types on even registers, but this isn't done on iOS or Windows Phone
  80. // TODO: optimize runtime: There should be a check for this in PrepareSystemFunction() so this
  81. // doesn't have to be done for functions that don't have any 64bit types
  82. #if !defined(AS_ANDROID) && !defined(AS_LINUX)
  83. #ifdef AS_NO_THISCALL_FUNCTOR_METHOD
  84. if( sysFunc->takesObjByVal )
  85. #else
  86. // In cases of thiscall methods, the callstack is configured as a standard thiscall
  87. // adding the secondObject as first or last element in callstack
  88. if( sysFunc->takesObjByVal || isThisCallMethod )
  89. #endif
  90. #endif
  91. {
  92. #if defined(AS_ANDROID) || defined(AS_LINUX)
  93. // mask is used as a toggler to skip uneven registers.
  94. int mask = 1;
  95. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  96. if( isThisCallMethod )
  97. {
  98. mask = 0;
  99. }
  100. else
  101. #endif
  102. {
  103. // Check for object pointer as first argument
  104. switch( callConv )
  105. {
  106. case ICC_THISCALL:
  107. case ICC_CDECL_OBJFIRST:
  108. case ICC_VIRTUAL_THISCALL:
  109. case ICC_THISCALL_RETURNINMEM:
  110. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  111. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  112. mask = 0;
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. // Check for hidden address in case of return by value
  119. if( sysFunc->hostReturnInMemory )
  120. mask = !mask;
  121. #endif
  122. paramSize = 0;
  123. int spos = 0;
  124. int dpos = 2;
  125. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  126. if( isThisCallMethod && (callConv >= ICC_THISCALL_OBJFIRST &&
  127. callConv <= ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM) )
  128. {
  129. // Add the object pointer as the first parameter
  130. paramBuffer[dpos++] = (asDWORD)secondObject;
  131. paramSize++;
  132. }
  133. #endif
  134. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  135. {
  136. // TODO: runtime optimize: Declare a reference to descr->parameterTypes[n] so the array doesn't have to be access all the time
  137. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  138. {
  139. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  140. if( descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK )
  141. {
  142. paramBuffer[dpos++] = args[spos++];
  143. paramSize++;
  144. }
  145. else
  146. #endif
  147. {
  148. #if defined(AS_ANDROID) || defined(AS_LINUX)
  149. if( (descr->parameterTypes[n].GetObjectType()->flags & asOBJ_APP_CLASS_ALIGN8) &&
  150. ((dpos & 1) == mask) )
  151. {
  152. // 64 bit value align
  153. dpos++;
  154. paramSize++;
  155. }
  156. #endif
  157. // Copy the object's memory to the buffer
  158. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  159. // Delete the original memory
  160. engine->CallFree(*(char**)(args+spos));
  161. spos++;
  162. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  163. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  164. }
  165. }
  166. else
  167. {
  168. #if defined(AS_ANDROID) || defined(AS_LINUX)
  169. // Should an alignment be performed?
  170. if( !descr->parameterTypes[n].IsObjectHandle() &&
  171. !descr->parameterTypes[n].IsReference() &&
  172. descr->parameterTypes[n].GetSizeOnStackDWords() == 2 &&
  173. ((dpos & 1) == mask) )
  174. {
  175. // 64 bit value align
  176. dpos++;
  177. paramSize++;
  178. }
  179. #endif
  180. // Copy the value directly
  181. paramBuffer[dpos++] = args[spos++];
  182. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  183. paramBuffer[dpos++] = args[spos++];
  184. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  185. }
  186. }
  187. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  188. if( isThisCallMethod && (callConv >= ICC_THISCALL_OBJLAST &&
  189. callConv <= ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM) )
  190. {
  191. // Add the object pointer as the last parameter
  192. paramBuffer[dpos++] = (asDWORD)secondObject;
  193. paramSize++;
  194. }
  195. #endif
  196. // Keep a free location at the beginning
  197. args = &paramBuffer[2];
  198. }
  199. switch( callConv )
  200. {
  201. case ICC_CDECL_RETURNINMEM: // fall through
  202. case ICC_STDCALL_RETURNINMEM:
  203. retQW = armFuncR0(args, paramSize<<2, func, (asDWORD)retPointer);
  204. break;
  205. case ICC_CDECL: // fall through
  206. case ICC_STDCALL:
  207. retQW = armFunc(args, paramSize<<2, func);
  208. break;
  209. case ICC_THISCALL: // fall through
  210. case ICC_CDECL_OBJFIRST:
  211. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  212. case ICC_THISCALL_OBJFIRST:
  213. case ICC_THISCALL_OBJLAST:
  214. #endif
  215. retQW = armFuncR0(args, paramSize<<2, func, (asDWORD)obj);
  216. break;
  217. case ICC_THISCALL_RETURNINMEM:
  218. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  219. case ICC_THISCALL_OBJFIRST_RETURNINMEM:
  220. case ICC_THISCALL_OBJLAST_RETURNINMEM:
  221. #endif
  222. #ifdef __GNUC__
  223. // On GNUC the address where the return value will be placed should be put in R0
  224. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  225. #else
  226. // On Windows the R0 should always hold the object pointer, and the address for the return value comes after
  227. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)obj, (asDWORD)retPointer);
  228. #endif
  229. break;
  230. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  231. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  232. break;
  233. case ICC_VIRTUAL_THISCALL:
  234. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  235. case ICC_VIRTUAL_THISCALL_OBJFIRST:
  236. case ICC_VIRTUAL_THISCALL_OBJLAST:
  237. #endif
  238. // Get virtual function table from the object pointer
  239. vftable = *(asFUNCTION_t**)obj;
  240. retQW = armFuncR0(args, paramSize<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)obj);
  241. break;
  242. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  243. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  244. case ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM:
  245. case ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM:
  246. #endif
  247. // Get virtual function table from the object pointer
  248. vftable = *(asFUNCTION_t**)obj;
  249. #ifdef __GNUC__
  250. // On GNUC the address where the return value will be placed should be put in R0
  251. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)retPointer, (asDWORD)obj);
  252. #else
  253. // On Windows the R0 should always hold the object pointer, and the address for the return value comes after
  254. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)obj, (asDWORD)retPointer);
  255. #endif
  256. break;
  257. case ICC_CDECL_OBJLAST:
  258. retQW = armFuncObjLast(args, paramSize<<2, func, (asDWORD)obj);
  259. break;
  260. case ICC_CDECL_OBJLAST_RETURNINMEM:
  261. retQW = armFuncR0ObjLast(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  262. break;
  263. default:
  264. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  265. }
  266. return retQW;
  267. }
  268. END_AS_NAMESPACE
  269. #elif !defined(AS_SOFTFP)
  270. // This code supports the hard-float ABI, i.e. g++ -mfloat-abi=hard
  271. // The main difference is that the floating point values are passed in the fpu registers
  272. #define VFP_OFFSET 70
  273. #define STACK_OFFSET 6
  274. #define PARAM_BUFFER_SIZE 104
  275. BEGIN_AS_NAMESPACE
  276. extern "C" asQWORD armFunc (const asDWORD *, int, asFUNCTION_t);
  277. extern "C" asQWORD armFuncR0 (const asDWORD *, int, asFUNCTION_t, asDWORD r0);
  278. extern "C" asQWORD armFuncR0R1 (const asDWORD *, int, asFUNCTION_t, asDWORD r0, asDWORD r1);
  279. extern "C" asQWORD armFuncObjLast (const asDWORD *, int, asFUNCTION_t, asDWORD obj);
  280. extern "C" asQWORD armFuncR0ObjLast (const asDWORD *, int, asFUNCTION_t, asDWORD r0, asDWORD obj);
  281. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/)
  282. {
  283. asCScriptEngine *engine = context->m_engine;
  284. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  285. int callConv = sysFunc->callConv;
  286. asQWORD retQW = 0;
  287. asFUNCTION_t func = sysFunc->func;
  288. int paramSize = sysFunc->paramSize;
  289. asFUNCTION_t *vftable;
  290. //---------------------------------------------------------------------------- RPi
  291. int freeFloatSlot = VFP_OFFSET;
  292. int freeDoubleSlot = VFP_OFFSET;
  293. int stackPos = STACK_OFFSET;
  294. int stackSize = 0;
  295. //----------------------------------------------------------------------------
  296. //---------------------------------------------------------------------------- RPi
  297. // We´ll divide paramBuffer into several segments:
  298. //
  299. // 0-1 Unused
  300. // 2-5 (+8 / +0 asm) values that should be placed in R0 - R3
  301. // 6-67 (+24 / +16 asm) values that should be placed on the stack
  302. // 68 (+272 / +264 asm) number of values stored in r registers (R0 - R3)
  303. // 69 (+276 / +268 asm) number of args stored on the stack
  304. // 70-85 (+280 / +272 asm) values that should be placed in VFP registers (16)
  305. // 86-87 (+344 / +336 asm) sp original value - sp final value - for debugging
  306. // 88-103 (+352 / +344 asm) Check area for free-used VFP registers
  307. //
  308. // Total number of elements: 104
  309. //
  310. // When passing the paramBuffer to the asm routines via the args pointer we are
  311. // offsetting the start of the array to being at element # 2. That´s why in asm
  312. // all addresses must have an offset of -2 words (-8 bytes).
  313. //---------------------------------------------------------------------------- RPi
  314. asDWORD paramBuffer[PARAM_BUFFER_SIZE];
  315. memset(paramBuffer, 0, sizeof(asDWORD) * PARAM_BUFFER_SIZE);
  316. if( sysFunc->hostReturnInMemory )
  317. {
  318. // TODO: runtime optimize: This check should be done in PrepareSystemFunction
  319. if ( !( descr->returnType.GetObjectType()->flags & COMPLEX_RETURN_MASK ) &&
  320. ( descr->returnType.GetObjectType()->flags & asOBJ_APP_CLASS_ALLFLOATS ) &&
  321. descr->returnType.GetSizeInMemoryBytes() <= 8 )
  322. callConv--;
  323. // The return is made in memory
  324. callConv++;
  325. }
  326. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  327. // Unpack the two object pointers
  328. void **objectsPtrs = (void**)obj;
  329. void *secondObject = objectsPtrs[1];
  330. obj = objectsPtrs[0];
  331. bool isThisCallMethod = callConv >= ICC_THISCALL_OBJLAST;
  332. #endif
  333. // Linux needs to align 64bit types on even registers, but this isn't done on iOS or Windows Phone
  334. // TODO: optimize runtime: There should be a check for this in PrepareSystemFunction() so this
  335. // doesn't have to be done for functions that don't have any 64bit types
  336. {
  337. // mask is used as a toggler to skip uneven registers.
  338. int mask = 1;
  339. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  340. if( isThisCallMethod )
  341. {
  342. mask = 0;
  343. }
  344. else
  345. #endif
  346. {
  347. // Check for object pointer as first argument
  348. switch( callConv )
  349. {
  350. case ICC_THISCALL:
  351. case ICC_CDECL_OBJFIRST:
  352. case ICC_VIRTUAL_THISCALL:
  353. case ICC_THISCALL_RETURNINMEM:
  354. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  355. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  356. mask = 0;
  357. break;
  358. default:
  359. break;
  360. }
  361. }
  362. // Check for hidden address in case of return by value
  363. if( sysFunc->hostReturnInMemory )
  364. mask = !mask;
  365. paramSize = 0;
  366. int spos = 0;
  367. int dpos = 2;
  368. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  369. if( isThisCallMethod && (callConv >= ICC_THISCALL_OBJFIRST &&
  370. callConv <= ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM) )
  371. {
  372. // Add the object pointer as the first parameter
  373. paramBuffer[dpos++] = (asDWORD)secondObject;
  374. paramSize++;
  375. }
  376. #endif
  377. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  378. {
  379. // TODO: runtime optimize: Declare a reference to descr->parameterTypes[n] so the array doesn't have to be access all the time
  380. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  381. {
  382. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  383. if( descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK )
  384. {
  385. paramBuffer[dpos++] = args[spos++];
  386. paramSize++;
  387. }
  388. else
  389. #endif
  390. {
  391. if( (descr->parameterTypes[n].GetObjectType()->flags & asOBJ_APP_CLASS_ALIGN8) )
  392. {
  393. if ( (dpos & 1) == mask )
  394. {
  395. // 64 bit value align
  396. dpos++;
  397. paramSize++;
  398. }
  399. if ( (stackPos & 1) == mask )
  400. {
  401. // 64 bit value align
  402. stackPos++;
  403. stackSize++;
  404. }
  405. }
  406. // Copy the object's memory to the buffer
  407. if (descr->parameterTypes[n].GetObjectType()->flags & asOBJ_APP_CLASS_ALLFLOATS)
  408. {
  409. int target = (freeFloatSlot > freeDoubleSlot) ? freeFloatSlot : freeDoubleSlot;
  410. if ( descr->parameterTypes[n].GetSizeInMemoryDWords() <= ( (VFP_OFFSET + 16) - target) )
  411. {
  412. memcpy(&paramBuffer[target], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  413. memset(&paramBuffer[target + 18], (asDWORD)1, descr->parameterTypes[n].GetSizeInMemoryDWords());
  414. target += descr->parameterTypes[n].GetSizeInMemoryDWords();
  415. freeFloatSlot = freeDoubleSlot = target;
  416. }
  417. else
  418. {
  419. memcpy(&paramBuffer[stackPos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  420. stackPos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  421. stackSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  422. }
  423. }
  424. else
  425. {
  426. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  427. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  428. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  429. }
  430. // Delete the original memory
  431. engine->CallFree(*(char**)(args+spos));
  432. spos++;
  433. }
  434. continue;
  435. }
  436. else if( descr->parameterTypes[n].IsFloatType() && !descr->parameterTypes[n].IsReference() )
  437. {
  438. // Are there any "s" registers available?
  439. if ( freeFloatSlot < (VFP_OFFSET + 16) )
  440. {
  441. if (freeFloatSlot == freeDoubleSlot)
  442. freeDoubleSlot += 2;
  443. paramBuffer[freeFloatSlot + 18] = (asDWORD)1;
  444. paramBuffer[freeFloatSlot++] = args[spos++];
  445. while(freeFloatSlot < (VFP_OFFSET + 16) && paramBuffer[freeFloatSlot + 18] != 0)
  446. freeFloatSlot++;
  447. }
  448. // If not, then store the float arg in the stack area
  449. else
  450. {
  451. paramBuffer[stackPos++] = args[spos++];
  452. stackSize++;
  453. }
  454. continue;
  455. }
  456. else if( descr->parameterTypes[n].IsDoubleType() && !descr->parameterTypes[n].IsReference() )
  457. {
  458. // Are there any "d" registers available?
  459. if ( freeDoubleSlot < (VFP_OFFSET + 15) )
  460. {
  461. if (freeFloatSlot == freeDoubleSlot)
  462. freeFloatSlot += 2;
  463. // Copy two dwords for the double
  464. paramBuffer[freeDoubleSlot + 18] = (asDWORD)1;
  465. paramBuffer[freeDoubleSlot + 19] = (asDWORD)1;
  466. paramBuffer[freeDoubleSlot++] = args[spos++];
  467. paramBuffer[freeDoubleSlot++] = args[spos++];
  468. while(freeDoubleSlot < (VFP_OFFSET + 15) && paramBuffer[freeDoubleSlot + 18] != 0)
  469. freeDoubleSlot += 2;
  470. }
  471. // If not, then store the double arg in the stack area
  472. else
  473. {
  474. if ( (stackPos & 1) == mask )
  475. {
  476. // 64 bit value align
  477. stackPos++;
  478. stackSize++;
  479. }
  480. paramBuffer[stackPos++] = args[spos++];
  481. paramBuffer[stackPos++] = args[spos++];
  482. stackSize += 2;
  483. }
  484. continue;
  485. }
  486. else
  487. {
  488. // Copy the value directly to "r" registers or the stack, checking for alignment
  489. if (paramSize < 4)
  490. {
  491. // Should an alignment be performed?
  492. if( (dpos & 1) == mask && descr->parameterTypes[n].GetSizeOnStackDWords() == 2 &&
  493. !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() &&
  494. !descr->parameterTypes[n].IsAnyType() )
  495. {
  496. // 64 bit value align
  497. dpos++;
  498. paramSize++;
  499. }
  500. paramBuffer[dpos++] = args[spos++];
  501. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  502. }
  503. else
  504. {
  505. // Should an alignment be performed?
  506. if( (stackPos & 1) == mask && descr->parameterTypes[n].GetSizeOnStackDWords() == 2 &&
  507. !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() &&
  508. !descr->parameterTypes[n].IsAnyType() )
  509. {
  510. // 64 bit value align
  511. stackPos++;
  512. stackSize++;
  513. }
  514. paramBuffer[stackPos++] = args[spos++];
  515. stackSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  516. }
  517. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  518. {
  519. if (paramSize < 5)
  520. paramBuffer[dpos++] = args[spos++];
  521. else
  522. paramBuffer[stackPos++] = args[spos++];
  523. }
  524. }// else...
  525. }// Loop
  526. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  527. if( isThisCallMethod && (callConv >= ICC_THISCALL_OBJLAST &&
  528. callConv <= ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM) )
  529. {
  530. if (paramSize < 4)
  531. {
  532. paramBuffer[dpos++] = (asDWORD)secondObject;
  533. paramSize++;
  534. }
  535. else
  536. {
  537. paramBuffer[stackPos++] = (asDWORD)secondObject;
  538. stackSize++;
  539. }
  540. }
  541. #endif
  542. // Keep a free location at the beginning
  543. args = &paramBuffer[2];
  544. }
  545. paramBuffer[69] = static_cast<asDWORD>(stackSize<<2);
  546. switch( callConv )
  547. {
  548. case ICC_CDECL_RETURNINMEM: // fall through
  549. case ICC_STDCALL_RETURNINMEM:
  550. retQW = armFuncR0(args, paramSize<<2, func, (asDWORD)retPointer);
  551. break;
  552. case ICC_CDECL: // fall through
  553. case ICC_STDCALL:
  554. retQW = armFunc(args, paramSize<<2, func);
  555. break;
  556. case ICC_THISCALL: // fall through
  557. case ICC_CDECL_OBJFIRST:
  558. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  559. case ICC_THISCALL_OBJFIRST:
  560. case ICC_THISCALL_OBJLAST:
  561. #endif
  562. retQW = armFuncR0(args, paramSize<<2, func, (asDWORD)obj);
  563. break;
  564. case ICC_THISCALL_RETURNINMEM:
  565. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  566. case ICC_THISCALL_OBJFIRST_RETURNINMEM:
  567. case ICC_THISCALL_OBJLAST_RETURNINMEM:
  568. #endif
  569. // On GNUC the address where the return value will be placed should be put in R0
  570. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  571. break;
  572. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  573. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  574. break;
  575. case ICC_VIRTUAL_THISCALL:
  576. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  577. case ICC_VIRTUAL_THISCALL_OBJFIRST:
  578. case ICC_VIRTUAL_THISCALL_OBJLAST:
  579. #endif
  580. // Get virtual function table from the object pointer
  581. vftable = *(asFUNCTION_t**)obj;
  582. retQW = armFuncR0(args, paramSize<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)obj);
  583. break;
  584. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  585. #ifndef AS_NO_THISCALL_FUNCTOR_METHOD
  586. case ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM:
  587. case ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM:
  588. #endif
  589. // Get virtual function table from the object pointer
  590. vftable = *(asFUNCTION_t**)obj;
  591. // On GNUC the address where the return value will be placed should be put in R0
  592. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)retPointer, (asDWORD)obj);
  593. break;
  594. case ICC_CDECL_OBJLAST:
  595. retQW = armFuncObjLast(args, paramSize<<2, func, (asDWORD)obj);
  596. break;
  597. case ICC_CDECL_OBJLAST_RETURNINMEM:
  598. retQW = armFuncR0ObjLast(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  599. break;
  600. default:
  601. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  602. }
  603. // On Linux with arm the float and double values are returns in the
  604. // floating point registers, s0 and s1. Objects that contain only
  605. // float types and are not considered complex are also returned in the
  606. // floating point registers.
  607. if( sysFunc->hostReturnFloat )
  608. {
  609. retQW = paramBuffer[VFP_OFFSET];
  610. if ( sysFunc->hostReturnSize > 1 )
  611. retQW = *( (asQWORD*)&paramBuffer[VFP_OFFSET] );
  612. }
  613. else if ( descr->returnType.IsObject() )
  614. {
  615. // TODO: runtime optimize: This should be identified with a flag determined in PrepareSystemFunction
  616. if ( !descr->returnType.IsObjectHandle() &&
  617. !descr->returnType.IsReference() &&
  618. !(descr->returnType.GetObjectType()->flags & COMPLEX_RETURN_MASK) &&
  619. (descr->returnType.GetObjectType()->flags & asOBJ_APP_CLASS_ALLFLOATS) )
  620. memcpy( retPointer, &paramBuffer[VFP_OFFSET], descr->returnType.GetSizeInMemoryBytes() );
  621. }
  622. return retQW;
  623. }
  624. END_AS_NAMESPACE
  625. #endif // AS_LINUX
  626. #endif // AS_ARM
  627. #endif // AS_MAX_PORTABILITY