as_callfunc_arm.cpp 22 KB

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