as_callfunc_xenon.cpp 21 KB

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