as_callfunc_xenon.cpp 21 KB

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