as_callfunc_xenon.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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_xenon.cpp
  25. //
  26. // These functions handle the actual calling of system functions
  27. //
  28. // This version is Xenon specific
  29. // Modified from as_callfunc_ppc.cpp by Laszlo Perneky February 2007
  30. //
  31. // Modified by Cyril Tissier March 2010:
  32. // various fixes in 'float' args passing / function return
  33. // properly handling 'double' type
  34. // various fixes in asm ppcFunc
  35. // fix for variable arguments
  36. //
  37. // Modified by Anthony Clark May 2015
  38. // Fixed the issue where int64 and uint64 could not be passed nativly
  39. // few minor fixes within asm ppcFunc to handle int64 and uint64
  40. // XBox 360 calling convention
  41. // ===========================
  42. // I've yet to find an official document with the ABI for XBox 360,
  43. // but I'll describe what I've gathered from the code and tests
  44. // performed by the AngelScript community.
  45. //
  46. // Arguments are passed in the following registers:
  47. // r3 - r10 : integer/pointer arguments (each register is 64bit)
  48. // fr1 - fr13 : float/double arguments (each register is 64bit)
  49. //
  50. // Arguments that don't fit in the registers will be pushed on the stack.
  51. //
  52. // When a float or double is passed as argument, its value will be placed
  53. // in the next available float register, but it will also reserve general
  54. // purpose register.
  55. //
  56. // Example: void foo(float a, int b). a will be passed in fr1 and b in r4.
  57. //
  58. // For each argument passed to a function an 8byte slot is reserved on the
  59. // stack, so that the function can offload the value there if needed. The
  60. // first slot is at r1+20, the next at r1+28, etc.
  61. //
  62. // If the function is a class method, the this pointer is passed as hidden
  63. // first argument. If the function returns an object in memory, the address
  64. // for that memory is passed as hidden first argument.
  65. //
  66. // Return value are placed in the following registers:
  67. // r3 : integer/pointer values
  68. // fr1 : float/double values
  69. //
  70. // Rules for registers
  71. // r1 : stack pointer
  72. // r14-r31 : nonvolatile, i.e. their values must be preserved
  73. // fr14-fr31 : nonvolatile, i.e. their values must be preserved
  74. // r0, r2, r13 : dedicated. I'm not sure what it means, but it is probably best not to use them
  75. //
  76. // The stack pointer must always be aligned at 8 bytes.
  77. //
  78. // References:
  79. // https://www-01.ibm.com/chips/techlib/techlib.nsf/techdocs/852569B20050FF77852569970071B0D6/$file/eabi_app.pdf
  80. //
  81. // TODO: The code doesn't handle objects passed by value (unless they are max 4 bytes in size)
  82. #include "as_config.h"
  83. #ifndef AS_MAX_PORTABILITY
  84. #if defined(AS_XENON)
  85. #include "as_callfunc.h"
  86. #include "as_scriptengine.h"
  87. #include "as_texts.h"
  88. #include "as_tokendef.h"
  89. #include "as_context.h"
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <xtl.h>
  93. BEGIN_AS_NAMESPACE
  94. #define AS_PPC_MAX_ARGS 32
  95. #define AS_PPC_THISCALL_REG 1
  96. #define AS_PPC_RETURNINMEM_REG 1
  97. #define AS_PPC_ENDOFARGS 1
  98. // The array used to send values to the correct places.
  99. // Contains a byte of argTypes to indicate the register type to load, or zero if end of arguments
  100. enum argTypes
  101. {
  102. ppcENDARG = 0,
  103. ppcINTARG = 1,
  104. ppcFLOATARG = 2,
  105. ppcDOUBLEARG = 3
  106. };
  107. // Loads all data into the correct places and calls the function.
  108. // pArgs is the array of the argument values
  109. // pArgTypes is an array containing a byte indicating the type (enum argTypes) for each argument.
  110. // dwFunc is the address of the function that will be called
  111. asQWORD __declspec( naked ) ppcFunc(const asQWORD* pArgs, asDWORD dwFunc, const asBYTE* pArgTypes)
  112. {
  113. __asm
  114. {
  115. _ppcFunc:
  116. // Prologue
  117. // Read and stack the link register (return address)
  118. mflr r12
  119. stw r12,-8(r1)
  120. // Backup all non-volatile registers we use in this function
  121. std r31,-10h(r1) // stack pointer for pushing arguments
  122. std r27,-18h(r1) // dwFunc
  123. std r26,-20h(r1) // pArgs
  124. std r25,-28h(r1) // pArgTypes
  125. std r24,-30h(r1) // current arg type
  126. std r23,-38h(r1) // counter for used GPRs
  127. std r22,-40h(r1) // counter for used float registers
  128. // Setup the stack frame to make room for the backup of registers
  129. // and the arguments that will be passed to the application function.
  130. // 512 bytes is enough for about 50 arguments plus backup of 8
  131. // TODO: Should perhaps make this dynamic based on number of arguments
  132. stwu r1,-200h(r1)
  133. //////////////////////////////////////////////////////////////////////////
  134. // Initialize local variables
  135. //////////////////////////////////////////////////////////////////////////
  136. // r31 is our pointer into the stack where the arguments will be place
  137. // The MSVC optimizer seems to rely on nobody copying the r1 register directly
  138. // so we can't just do a simple 'addi r31, r1, 14h' as the optimizer may
  139. // end up moving this instruction to before the update of r1 above.
  140. // Instead we'll read the previous stack pointer from the stack, and then
  141. // subtract to get the correct offset.
  142. lwz r31, 0(r1)
  143. subi r31, r31, 1ECh // prev r1 - 512 + 20 = curr r1 + 20
  144. mr r26, r3 // pArgs
  145. mr r27, r4 // dwFunc
  146. mr r25, r5 // pArgTypes
  147. // Counting of used/assigned GPR's
  148. sub r23, r23, r23
  149. // Counting of used/assigned Float Registers
  150. sub r22, r22, r22
  151. // Begin loading and stacking registers
  152. subi r25, r25, 1
  153. //////////////////////////////////////////////////////////////////////////
  154. // Fetch the next argument
  155. //////////////////////////////////////////////////////////////////////////
  156. ppcNextArg:
  157. // Increment rArgTypePtr
  158. addi r25, r25, 1
  159. // Get data type
  160. lbz r24, 0(r25)
  161. // r24 holds the data type
  162. cmplwi cr6, r24, 0
  163. beq cr6, ppcArgsEnd
  164. cmplwi cr6, r24, 1
  165. beq cr6, ppcArgIsInteger
  166. cmplwi cr6, r24, 2
  167. beq cr6, ppcArgIsFloat
  168. cmplwi cr6, r24, 3
  169. beq cr6, ppcArgIsDouble
  170. //////////////////////////////////////////////////////////////////////////
  171. // Load and stack integer arguments
  172. //////////////////////////////////////////////////////////////////////////
  173. ppcArgIsInteger:
  174. // Get the arg from the stack
  175. ld r12, 0(r26)
  176. // r23 holds the integer arg count so far
  177. cmplwi cr6, r23, 0
  178. beq cr6, ppcLoadIntReg0
  179. cmplwi cr6, r23, 1
  180. beq cr6, ppcLoadIntReg1
  181. cmplwi cr6, r23, 2
  182. beq cr6, ppcLoadIntReg2
  183. cmplwi cr6, r23, 3
  184. beq cr6, ppcLoadIntReg3
  185. cmplwi cr6, r23, 4
  186. beq cr6, ppcLoadIntReg4
  187. cmplwi cr6, r23, 5
  188. beq cr6, ppcLoadIntReg5
  189. cmplwi cr6, r23, 6
  190. beq cr6, ppcLoadIntReg6
  191. cmplwi cr6, r23, 7
  192. beq cr6, ppcLoadIntReg7
  193. // no more than 8 parameters
  194. b ppcLoadIntRegUpd
  195. ppcLoadIntReg0:
  196. mr r3, r12
  197. b ppcLoadIntRegUpd
  198. ppcLoadIntReg1:
  199. mr r4, r12
  200. b ppcLoadIntRegUpd
  201. ppcLoadIntReg2:
  202. mr r5, r12
  203. b ppcLoadIntRegUpd
  204. ppcLoadIntReg3:
  205. mr r6, r12
  206. b ppcLoadIntRegUpd
  207. ppcLoadIntReg4:
  208. mr r7, r12
  209. b ppcLoadIntRegUpd
  210. ppcLoadIntReg5:
  211. mr r8, r12
  212. b ppcLoadIntRegUpd
  213. ppcLoadIntReg6:
  214. mr r9, r12
  215. b ppcLoadIntRegUpd
  216. ppcLoadIntReg7:
  217. mr r10, r12
  218. b ppcLoadIntRegUpd
  219. ppcLoadIntRegUpd:
  220. std r12, 0(r31) // push on the stack
  221. addi r31, r31, 8 // inc stack by 1 reg
  222. addi r23, r23, 1 // Increment used int register count
  223. addi r26, r26, 8 // Increment pArgs
  224. b ppcNextArg // Call next arg
  225. //////////////////////////////////////////////////////////////////////////
  226. // Load and stack float arguments
  227. //////////////////////////////////////////////////////////////////////////
  228. ppcArgIsFloat:
  229. // Get the arg from the stack
  230. lfs fr0, 0(r26)
  231. // r22 holds the float arg count so far
  232. cmplwi cr6, r22, 0
  233. beq cr6, ppcLoadFloatReg0
  234. cmplwi cr6, r22, 1
  235. beq cr6, ppcLoadFloatReg1
  236. cmplwi cr6, r22, 2
  237. beq cr6, ppcLoadFloatReg2
  238. cmplwi cr6, r22, 3
  239. beq cr6, ppcLoadFloatReg3
  240. cmplwi cr6, r22, 4
  241. beq cr6, ppcLoadFloatReg4
  242. cmplwi cr6, r22, 5
  243. beq cr6, ppcLoadFloatReg5
  244. cmplwi cr6, r22, 6
  245. beq cr6, ppcLoadFloatReg6
  246. cmplwi cr6, r22, 7
  247. beq cr6, ppcLoadFloatReg7
  248. cmplwi cr6, r22, 8
  249. beq cr6, ppcLoadFloatReg8
  250. cmplwi cr6, r22, 9
  251. beq cr6, ppcLoadFloatReg9
  252. cmplwi cr6, r22, 10
  253. beq cr6, ppcLoadFloatReg10
  254. cmplwi cr6, r22, 11
  255. beq cr6, ppcLoadFloatReg11
  256. cmplwi cr6, r22, 12
  257. beq cr6, ppcLoadFloatReg12
  258. // no more than 12 parameters
  259. b ppcLoadFloatRegUpd
  260. ppcLoadFloatReg0:
  261. fmr fr1, fr0
  262. b ppcLoadFloatRegUpd
  263. ppcLoadFloatReg1:
  264. fmr fr2, fr0
  265. b ppcLoadFloatRegUpd
  266. ppcLoadFloatReg2:
  267. fmr fr3, fr0
  268. b ppcLoadFloatRegUpd
  269. ppcLoadFloatReg3:
  270. fmr fr4, fr0
  271. b ppcLoadFloatRegUpd
  272. ppcLoadFloatReg4:
  273. fmr fr5, fr0
  274. b ppcLoadFloatRegUpd
  275. ppcLoadFloatReg5:
  276. fmr fr6, fr0
  277. b ppcLoadFloatRegUpd
  278. ppcLoadFloatReg6:
  279. fmr fr7, fr0
  280. b ppcLoadFloatRegUpd
  281. ppcLoadFloatReg7:
  282. fmr fr8, fr0
  283. b ppcLoadFloatRegUpd
  284. ppcLoadFloatReg8:
  285. fmr fr9, fr0
  286. b ppcLoadFloatRegUpd
  287. ppcLoadFloatReg9:
  288. fmr fr10, fr0
  289. b ppcLoadFloatRegUpd
  290. ppcLoadFloatReg10:
  291. fmr fr11, fr0
  292. b ppcLoadFloatRegUpd
  293. ppcLoadFloatReg11:
  294. fmr fr12, fr0
  295. b ppcLoadFloatRegUpd
  296. ppcLoadFloatReg12:
  297. fmr fr13, fr0
  298. b ppcLoadFloatRegUpd
  299. ppcLoadFloatRegUpd:
  300. stfs fr0, 0(r31) // push on the stack
  301. addi r31, r31, 8 // inc stack by 1 reg
  302. addi r22, r22, 1 // Increment used float register count
  303. addi r23, r23, 1 // Increment used int register count - a float reg eats up a GPR
  304. addi r26, r26, 4 // Increment pArgs
  305. b ppcNextArg // Call next arg
  306. //////////////////////////////////////////////////////////////////////////
  307. // Load and stack double float arguments
  308. //////////////////////////////////////////////////////////////////////////
  309. ppcArgIsDouble:
  310. // Get the arg from the stack
  311. lfd fr0, 0(r26)
  312. // r22 holds the float arg count so far
  313. cmplwi cr6, r22, 0
  314. beq cr6, ppcLoadDoubleReg0
  315. cmplwi cr6, r22, 1
  316. beq cr6, ppcLoadDoubleReg1
  317. cmplwi cr6, r22, 2
  318. beq cr6, ppcLoadDoubleReg2
  319. cmplwi cr6, r22, 3
  320. beq cr6, ppcLoadDoubleReg3
  321. cmplwi cr6, r22, 4
  322. beq cr6, ppcLoadDoubleReg4
  323. cmplwi cr6, r22, 5
  324. beq cr6, ppcLoadDoubleReg5
  325. cmplwi cr6, r22, 6
  326. beq cr6, ppcLoadDoubleReg6
  327. cmplwi cr6, r22, 7
  328. beq cr6, ppcLoadDoubleReg7
  329. cmplwi cr6, r22, 8
  330. beq cr6, ppcLoadDoubleReg8
  331. cmplwi cr6, r22, 9
  332. beq cr6, ppcLoadDoubleReg9
  333. cmplwi cr6, r22, 10
  334. beq cr6, ppcLoadDoubleReg10
  335. cmplwi cr6, r22, 11
  336. beq cr6, ppcLoadDoubleReg11
  337. cmplwi cr6, r22, 12
  338. beq cr6, ppcLoadDoubleReg12
  339. // no more than 12 parameters
  340. b ppcLoadDoubleRegUpd
  341. ppcLoadDoubleReg0:
  342. fmr fr1, fr0
  343. b ppcLoadDoubleRegUpd
  344. ppcLoadDoubleReg1:
  345. fmr fr2, fr0
  346. b ppcLoadDoubleRegUpd
  347. ppcLoadDoubleReg2:
  348. fmr fr3, fr0
  349. b ppcLoadDoubleRegUpd
  350. ppcLoadDoubleReg3:
  351. fmr fr4, fr0
  352. b ppcLoadDoubleRegUpd
  353. ppcLoadDoubleReg4:
  354. fmr fr5, fr0
  355. b ppcLoadDoubleRegUpd
  356. ppcLoadDoubleReg5:
  357. fmr fr6, fr0
  358. b ppcLoadDoubleRegUpd
  359. ppcLoadDoubleReg6:
  360. fmr fr7, fr0
  361. b ppcLoadDoubleRegUpd
  362. ppcLoadDoubleReg7:
  363. fmr fr8, fr0
  364. b ppcLoadDoubleRegUpd
  365. ppcLoadDoubleReg8:
  366. fmr fr9, fr0
  367. b ppcLoadDoubleRegUpd
  368. ppcLoadDoubleReg9:
  369. fmr fr10, fr0
  370. b ppcLoadDoubleRegUpd
  371. ppcLoadDoubleReg10:
  372. fmr fr11, fr0
  373. b ppcLoadDoubleRegUpd
  374. ppcLoadDoubleReg11:
  375. fmr fr12, fr0
  376. b ppcLoadDoubleRegUpd
  377. ppcLoadDoubleReg12:
  378. fmr fr13, fr0
  379. b ppcLoadDoubleRegUpd
  380. ppcLoadDoubleRegUpd:
  381. stfd fr0, 0(r31) // push on the stack
  382. addi r31, r31, 8 // inc stack by 1 reg
  383. addi r22, r22, 1 // Increment used float register count
  384. addi r23, r23, 1 // Increment used int register count
  385. addi r26, r26, 8 // Increment pArgs
  386. b ppcNextArg
  387. //////////////////////////////////////////////////////////////////////////
  388. // Finished
  389. //////////////////////////////////////////////////////////////////////////
  390. ppcArgsEnd:
  391. // Call the function
  392. mtctr r27
  393. bctrl
  394. // Epilogue
  395. // Restore callers stack
  396. addi r1, r1, 200h
  397. // restore all registers we used in this fct
  398. ld r22,-40h(r1)
  399. ld r23,-38h(r1)
  400. ld r24,-30h(r1)
  401. ld r25,-28h(r1)
  402. ld r26,-20h(r1)
  403. ld r27,-18h(r1)
  404. ld r31,-10h(r1)
  405. // Fetch return link to caller
  406. lwz r12,-8(r1)
  407. mtlr r12
  408. blr
  409. }
  410. }
  411. asDWORD GetReturnedFloat()
  412. {
  413. // This variable must be declared volatile so that the
  414. // compiler optimizations do not remove its initialization
  415. // with the fr1 register due to believing the fr1 register
  416. // isn't initialized.
  417. volatile asDWORD f;
  418. __asm
  419. {
  420. stfs fr1, f
  421. }
  422. return f;
  423. }
  424. asQWORD GetReturnedDouble()
  425. {
  426. // This variable must be declared volatile so that the
  427. // compiler optimizations do not remove its initialization
  428. // with the fr1 register due to believing the fr1 register
  429. // isn't initialized.
  430. volatile asQWORD f;
  431. __asm
  432. {
  433. stfd fr1, f
  434. }
  435. return f;
  436. }
  437. // returns true if the given parameter is a 'variable argument'
  438. inline bool IsVariableArgument( asCDataType type )
  439. {
  440. return (type.GetTokenType() == ttQuestion) ? true : false;
  441. }
  442. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/, void */*secondObject*/)
  443. {
  444. // TODO: Xenon does not yet support THISCALL_OBJFIRST/LAST
  445. asCScriptEngine *engine = context->m_engine;
  446. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  447. int callConv = sysFunc->callConv;
  448. asQWORD retQW = 0;
  449. void *func = (void*)sysFunc->func;
  450. asDWORD *vftable;
  451. // Pack the arguments into an array that ppcFunc() can use to load each CPU register properly
  452. asBYTE ppcArgsType[AS_PPC_MAX_ARGS + AS_PPC_RETURNINMEM_REG + AS_PPC_THISCALL_REG + AS_PPC_ENDOFARGS];
  453. asQWORD ppcArgs[AS_PPC_MAX_ARGS + AS_PPC_RETURNINMEM_REG + AS_PPC_THISCALL_REG];
  454. int argsCnt = 0;
  455. // If the function returns an object in memory, we allocate the memory and put the ptr to the front (will go to r3)
  456. if( sysFunc->hostReturnInMemory )
  457. {
  458. ppcArgs[argsCnt] = (asDWORD)retPointer;
  459. ppcArgsType[argsCnt] = ppcINTARG;
  460. argsCnt++;
  461. }
  462. // If we have an object and it's not objectlast, then we put it as the first arg
  463. if ( obj &&
  464. callConv != ICC_CDECL_OBJLAST &&
  465. callConv != ICC_CDECL_OBJLAST_RETURNINMEM )
  466. {
  467. ppcArgs[argsCnt] = (asDWORD)obj;
  468. ppcArgsType[argsCnt] = ppcINTARG;
  469. argsCnt++;
  470. }
  471. // If the function takes any objects by value, they must be copied
  472. // to the stack, shifting the other arguments as necessary. paramBuffer
  473. // will then replace the args pointer that was received from the VM.
  474. // TODO: Is this really how XBox 360 passes objects by value?
  475. asDWORD paramBuffer[AS_PPC_MAX_ARGS];
  476. if( sysFunc->takesObjByVal )
  477. {
  478. int paramSize = 0;
  479. int spos = 0;
  480. int dpos = 1;
  481. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  482. {
  483. // Parameter object by value
  484. if( descr->parameterTypes[n].IsObject() &&
  485. !descr->parameterTypes[n].IsObjectHandle() &&
  486. !descr->parameterTypes[n].IsReference() )
  487. {
  488. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  489. if( descr->parameterTypes[n].GetTypeInfo()->flags & COMPLEX_MASK )
  490. {
  491. paramBuffer[dpos++] = args[spos++];
  492. paramSize++;
  493. }
  494. else
  495. #endif
  496. {
  497. // Copy the object's memory to the buffer
  498. memcpy( &paramBuffer[dpos], *(void**)(args + spos), descr->parameterTypes[n].GetSizeInMemoryBytes() );
  499. // Delete the original memory
  500. engine->CallFree(*(char**)(args + spos));
  501. spos++;
  502. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  503. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  504. }
  505. }
  506. else
  507. {
  508. // Copy the value directly
  509. paramBuffer[dpos++] = args[spos++];
  510. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  511. paramBuffer[dpos++] = args[spos++];
  512. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  513. }
  514. // If this was a variable argument parameter, then account for the implicit typeId
  515. if( IsVariableArgument( descr->parameterTypes[n] ) )
  516. {
  517. // the TypeId is just a DWORD
  518. paramBuffer[dpos++] = args[spos++];
  519. ++paramSize;
  520. }
  521. }
  522. // Keep a free location at the beginning
  523. args = &paramBuffer[1];
  524. asASSERT( paramSize <= AS_PPC_MAX_ARGS );
  525. }
  526. const asUINT paramCount = (asUINT)descr->parameterTypes.GetLength();
  527. asBYTE * pCurArgType = (asBYTE*)&ppcArgsType[argsCnt];
  528. asBYTE * pCurFixedArgValue = (asBYTE*)&ppcArgs[argsCnt];
  529. asBYTE * pCurStackArgValue = (asBYTE*)args;
  530. for( asUINT n = 0; n < paramCount; n++ )
  531. {
  532. argsCnt++;
  533. if (descr->parameterTypes[n].IsFloatType() && !descr->parameterTypes[n].IsReference())
  534. {
  535. *pCurArgType++ = ppcFLOATARG;
  536. *((float*) pCurFixedArgValue) = *((float*) pCurStackArgValue);
  537. pCurFixedArgValue += 4;
  538. pCurStackArgValue += 4;
  539. }
  540. else if (descr->parameterTypes[n].IsDoubleType() && !descr->parameterTypes[n].IsReference())
  541. {
  542. *pCurArgType++ = ppcDOUBLEARG;
  543. *((double*) pCurFixedArgValue) = *((double*) pCurStackArgValue);
  544. pCurFixedArgValue += 8;
  545. pCurStackArgValue += 8;
  546. }
  547. else
  548. {
  549. // TODO: The code also ignore the fact that large objects
  550. // passed by value has been copied to the stack
  551. // in the above loop.
  552. *pCurArgType++ = ppcINTARG;
  553. *((asQWORD*) pCurFixedArgValue) = *((asUINT*) pCurStackArgValue);
  554. if( !descr->parameterTypes[n].IsReference() )
  555. {
  556. // If the arg is not 4 bytes which we coppied, lets do it again the right way
  557. asUINT numBytes = descr->parameterTypes[n].GetSizeInMemoryBytes();
  558. if( numBytes == 1 )
  559. {
  560. *((asQWORD*) pCurFixedArgValue) = *((asBYTE*) pCurStackArgValue);
  561. }
  562. else if( numBytes == 2 )
  563. {
  564. *((asQWORD*) pCurFixedArgValue) = *((asWORD*) pCurStackArgValue);
  565. }
  566. else if( numBytes == 8 )
  567. {
  568. *((asQWORD*) pCurFixedArgValue) = *((asQWORD*) pCurStackArgValue);
  569. pCurStackArgValue += 4; // Increase our cur stack arg value by 4 bytes to = 8 total later
  570. }
  571. }
  572. pCurFixedArgValue += 8;
  573. pCurStackArgValue += 4;
  574. // if it is a variable argument, account for the typeId
  575. // implicitly add another parameter (AFTER the parameter above) for the typeId
  576. if( IsVariableArgument(descr->parameterTypes[n]) )
  577. {
  578. argsCnt++;
  579. *pCurArgType++ = ppcINTARG;
  580. *((int*) pCurFixedArgValue) = *((int*) pCurStackArgValue);
  581. pCurFixedArgValue += 4;
  582. pCurStackArgValue += 4;
  583. }
  584. }
  585. }
  586. // Add the arg list end indicator
  587. ppcArgsType[argsCnt] = ppcENDARG;
  588. switch( callConv )
  589. {
  590. case ICC_CDECL:
  591. case ICC_CDECL_RETURNINMEM:
  592. case ICC_STDCALL:
  593. case ICC_STDCALL_RETURNINMEM:
  594. case ICC_THISCALL:
  595. case ICC_THISCALL_RETURNINMEM:
  596. case ICC_CDECL_OBJFIRST:
  597. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  598. {
  599. retQW = ppcFunc( ppcArgs, (asDWORD)func, ppcArgsType );
  600. break;
  601. }
  602. case ICC_VIRTUAL_THISCALL:
  603. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  604. {
  605. // Get virtual function table from the object pointer
  606. vftable = *(asDWORD**)obj;
  607. retQW = ppcFunc( ppcArgs, vftable[asDWORD(func)>>2], ppcArgsType );
  608. break;
  609. }
  610. case ICC_CDECL_OBJLAST:
  611. case ICC_CDECL_OBJLAST_RETURNINMEM:
  612. {
  613. // Add the object pointer as the last argument
  614. ppcArgsType[argsCnt++] = ppcINTARG;
  615. ppcArgsType[argsCnt] = ppcENDARG;
  616. *((asQWORD*)pCurFixedArgValue) = (asPWORD)obj;
  617. retQW = ppcFunc( ppcArgs, (asDWORD)func, ppcArgsType );
  618. break;
  619. }
  620. default:
  621. context->SetInternalException( TXT_INVALID_CALLING_CONVENTION );
  622. }
  623. // If the return is a float value we need to get the value from the FP register
  624. if( sysFunc->hostReturnFloat )
  625. {
  626. if( sysFunc->hostReturnSize == 1 )
  627. *(asDWORD*)&retQW = GetReturnedFloat();
  628. else
  629. retQW = GetReturnedDouble();
  630. }
  631. else if( sysFunc->hostReturnSize == 1 )
  632. {
  633. // Move the bits to the higher value to compensate for the adjustment that the caller does
  634. retQW <<= 32;
  635. }
  636. return retQW;
  637. }
  638. END_AS_NAMESPACE
  639. #endif // AS_XENON
  640. #endif // AS_MAX_PORTABILITY