as_callfunc_mips.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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_mips.cpp
  25. //
  26. // These functions handle the actual calling of system functions
  27. //
  28. // This version is MIPS specific and was originally written
  29. // by Manu Evans in April, 2006 for Playstation Portable (PSP)
  30. //
  31. // Support for Linux with MIPS was added by Andreas Jonsson in April, 2015
  32. //
  33. #include "as_config.h"
  34. #ifndef AS_MAX_PORTABILITY
  35. #ifdef AS_MIPS
  36. #include "as_callfunc.h"
  37. #include "as_scriptengine.h"
  38. #include "as_texts.h"
  39. #include "as_tokendef.h"
  40. #include "as_context.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #if !defined(AS_ANDROID)
  44. #include <regdef.h>
  45. #endif
  46. BEGIN_AS_NAMESPACE
  47. #if defined(__linux__) && defined(_ABIO32)
  48. // The MIPS ABI used by Linux is implemented here
  49. // (Tested on CI20 MIPS Creator with Debian Linux)
  50. //
  51. // ref: SYSTEM V
  52. // APPLICATION BINARY INTERFACE
  53. // MIPS RISC Processor
  54. // http://math-atlas.sourceforge.net/devel/assembly/mipsabi32.pdf
  55. //
  56. // ref: MIPS Instruction Reference
  57. // http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html
  58. union SFloatRegs
  59. {
  60. union { double d0; struct { float f0; asDWORD dummy0; };};
  61. union { double d1; struct { float f1; asDWORD dummy1; };};
  62. } ;
  63. extern "C" asQWORD mipsFunc(asUINT argSize, asDWORD *argBuffer, void *func, SFloatRegs &floatRegs);
  64. asDWORD GetReturnedFloat();
  65. asQWORD GetReturnedDouble();
  66. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/, void *secondObject)
  67. {
  68. asCScriptEngine *engine = context->m_engine;
  69. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  70. int callConv = sysFunc->callConv;
  71. asQWORD retQW = 0;
  72. void *func = (void*)sysFunc->func;
  73. void **vftable;
  74. asDWORD argBuffer[128]; // Ought to be big enough
  75. asASSERT( sysFunc->paramSize < 128 );
  76. asDWORD argOffset = 0;
  77. SFloatRegs floatRegs;
  78. asDWORD floatOffset = 0;
  79. // If the application function returns the value in memory then
  80. // the first argument must be the pointer to that memory
  81. if( sysFunc->hostReturnInMemory )
  82. {
  83. asASSERT( retPointer );
  84. argBuffer[argOffset++] = (asPWORD)retPointer;
  85. }
  86. if( callConv == ICC_CDECL_OBJFIRST || callConv == ICC_CDECL_OBJFIRST_RETURNINMEM ||
  87. callConv == ICC_THISCALL || callConv == ICC_THISCALL_RETURNINMEM ||
  88. callConv == ICC_VIRTUAL_THISCALL || callConv == ICC_VIRTUAL_THISCALL_RETURNINMEM ||
  89. callConv == ICC_THISCALL_OBJFIRST || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
  90. callConv == ICC_THISCALL_OBJFIRST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM ||
  91. callConv == ICC_THISCALL_OBJLAST || callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
  92. callConv == ICC_THISCALL_OBJLAST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
  93. {
  94. // Add the object pointer as the first argument
  95. argBuffer[argOffset++] = (asPWORD)obj;
  96. }
  97. if( callConv == ICC_THISCALL_OBJFIRST || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST ||
  98. callConv == ICC_THISCALL_OBJFIRST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM )
  99. {
  100. // Add the second object pointer
  101. argBuffer[argOffset++] = (asPWORD)secondObject;
  102. }
  103. int spos = 0;
  104. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  105. {
  106. asCDataType &paramType = descr->parameterTypes[n];
  107. if( paramType.IsObject() && !paramType.IsObjectHandle() && !paramType.IsReference() )
  108. {
  109. if( paramType.GetTypeInfo()->flags & COMPLEX_MASK )
  110. {
  111. // The object is passed by reference
  112. argBuffer[argOffset++] = args[spos++];
  113. }
  114. else
  115. {
  116. // Ensure 8byte alignment for classes that need it
  117. if( (paramType.GetTypeInfo()->flags & asOBJ_APP_CLASS_ALIGN8) && (argOffset & 1) )
  118. argOffset++;
  119. // Copy the object's memory to the buffer
  120. memcpy(&argBuffer[argOffset], *(void**)(args+spos), paramType.GetSizeInMemoryBytes());
  121. // Delete the original memory
  122. engine->CallFree(*(char**)(args+spos));
  123. spos++;
  124. argOffset += paramType.GetSizeInMemoryDWords();
  125. }
  126. }
  127. else if( paramType.GetTokenType() == ttQuestion )
  128. {
  129. // Copy both pointer and type id
  130. argBuffer[argOffset++] = args[spos++];
  131. argBuffer[argOffset++] = args[spos++];
  132. }
  133. else
  134. {
  135. // The first 2 floats or doubles are loaded into the float registers.
  136. // Actually this is only done if they are the first arguments to the function,
  137. // but it doesn't cause any harm to load them into the registers even if they
  138. // won't be used so we don't need to check if they really are the first args.
  139. if( floatOffset == 0 )
  140. {
  141. if( paramType.GetTokenType() == ttFloat )
  142. floatRegs.f0 = *reinterpret_cast<float*>(&args[spos]);
  143. else if( paramType.GetTokenType() == ttDouble )
  144. floatRegs.d0 = *reinterpret_cast<double*>(&args[spos]);
  145. floatOffset++;
  146. }
  147. else if( floatOffset == 1 )
  148. {
  149. if( paramType.GetTokenType() == ttFloat )
  150. floatRegs.f1 = *reinterpret_cast<float*>(&args[spos]);
  151. else if( paramType.GetTokenType() == ttDouble )
  152. floatRegs.d1 = *reinterpret_cast<double*>(&args[spos]);
  153. floatOffset++;
  154. }
  155. // Copy the value directly
  156. if( paramType.GetSizeOnStackDWords() > 1 )
  157. {
  158. // Make sure the argument is 8byte aligned
  159. if( argOffset & 1 )
  160. argOffset++;
  161. *reinterpret_cast<asQWORD*>(&argBuffer[argOffset]) = *reinterpret_cast<asQWORD*>(&args[spos]);
  162. argOffset += 2;
  163. spos += 2;
  164. }
  165. else
  166. argBuffer[argOffset++] = args[spos++];
  167. }
  168. }
  169. if( callConv == ICC_CDECL_OBJLAST || callConv == ICC_CDECL_OBJLAST_RETURNINMEM )
  170. {
  171. // Add the object pointer as the last argument
  172. argBuffer[argOffset++] = (asPWORD)obj;
  173. }
  174. if( callConv == ICC_THISCALL_OBJLAST || callConv == ICC_VIRTUAL_THISCALL_OBJLAST ||
  175. callConv == ICC_THISCALL_OBJLAST_RETURNINMEM || callConv == ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM )
  176. {
  177. // Add the second object pointer
  178. argBuffer[argOffset++] = (asPWORD)secondObject;
  179. }
  180. switch( callConv )
  181. {
  182. case ICC_CDECL:
  183. case ICC_CDECL_RETURNINMEM:
  184. case ICC_STDCALL:
  185. case ICC_STDCALL_RETURNINMEM:
  186. case ICC_CDECL_OBJLAST:
  187. case ICC_CDECL_OBJLAST_RETURNINMEM:
  188. case ICC_CDECL_OBJFIRST:
  189. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  190. case ICC_THISCALL:
  191. case ICC_THISCALL_RETURNINMEM:
  192. case ICC_THISCALL_OBJFIRST:
  193. case ICC_THISCALL_OBJFIRST_RETURNINMEM:
  194. case ICC_THISCALL_OBJLAST:
  195. case ICC_THISCALL_OBJLAST_RETURNINMEM:
  196. retQW = mipsFunc(argOffset*4, argBuffer, func, floatRegs);
  197. break;
  198. case ICC_VIRTUAL_THISCALL:
  199. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  200. case ICC_VIRTUAL_THISCALL_OBJFIRST:
  201. case ICC_VIRTUAL_THISCALL_OBJFIRST_RETURNINMEM:
  202. case ICC_VIRTUAL_THISCALL_OBJLAST:
  203. case ICC_VIRTUAL_THISCALL_OBJLAST_RETURNINMEM:
  204. // Get virtual function table from the object pointer
  205. vftable = *(void***)obj;
  206. retQW = mipsFunc(argOffset*4, argBuffer, vftable[asPWORD(func)>>2], floatRegs);
  207. break;
  208. default:
  209. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  210. }
  211. // If the return is a float value we need to get the value from the FP register
  212. if( sysFunc->hostReturnFloat )
  213. {
  214. if( sysFunc->hostReturnSize == 1 )
  215. *(asDWORD*)&retQW = GetReturnedFloat();
  216. else
  217. retQW = GetReturnedDouble();
  218. }
  219. return retQW;
  220. }
  221. asDWORD GetReturnedFloat()
  222. {
  223. asDWORD f;
  224. asm("swc1 $f0, %0\n" : "=m"(f));
  225. return f;
  226. }
  227. asQWORD GetReturnedDouble()
  228. {
  229. asQWORD d = 0;
  230. asm("sdc1 $f0, %0\n" : "=m"(d));
  231. return d;
  232. }
  233. // asQWORD mipsFunc(asUINT argSize, asDWORD *argBuffer, void *func, SFloatRegs &floatRegs);
  234. // $2,$3 $4 $5 $6 $7
  235. asm(
  236. " .text\n"
  237. //" .align 2\n"
  238. " .cfi_startproc\n"
  239. " .global mipsFunc\n"
  240. " .ent mipsFunc\n"
  241. "mipsFunc:\n"
  242. //" .frame $fp,64,$31 # vars= 0, regs= 0/0, args= 0, gp= 0\n"
  243. //" .mask 0x00000000,0\n"
  244. //" .fmask 0x00000000,0\n"
  245. " .set noreorder\n"
  246. " .set nomacro\n"
  247. // align the stack frame to 8 bytes
  248. " addiu $12, $4, 7\n" // t4 ($12) = argSize ($4) + 7
  249. " li $13, -8\n" // t5 ($13) = 0xfffffffffffffff8
  250. " and $12, $12, $13\n" // t4 ($12) &= t5 ($13). t4 holds the size of the argument block
  251. // It is required that the caller reserves space for at least 16 bytes even if there are less than 4 arguments
  252. // and add 8 bytes for the return pointer and s0 ($16) backup
  253. " addiu $13, $12, 24\n" // t5 = t4 + 24. t5 ($13) holds the total size of the stack frame (including return pointer)
  254. // save the s0 register (so we can use it to remember where our return pointer is lives)
  255. " sw $16, -4($sp)\n" // store the s0 register (so we can use it to remember how big our stack frame is)
  256. " .cfi_offset 16, -4\n"
  257. // store the return pointer
  258. " sw $31, -8($sp)\n"
  259. " .cfi_offset 31, -8\n"
  260. // keep original stack pointer
  261. " move $16, $sp\n"
  262. " .cfi_def_cfa_register 16\n"
  263. // push the stack
  264. " subu $sp, $sp, $13\n"
  265. // store the argument in temporary registers
  266. " addiu $25, $6, 0\n" // t9 ($25) holds the function pointer (must be t9 for position independent code)
  267. " addiu $3, $4, 0\n" // v1 ($3) holds the size of the argument buffer
  268. " move $15, $5\n" // t7 ($15) holds the pointer to the argBuffer
  269. " move $14, $7\n" // t6 ($14) holds the values for the float registers
  270. // load integer registers
  271. " lw $4, 0($15)\n" // a0 ($4)
  272. " lw $5, 4($15)\n" // a1 ($5)
  273. " lw $6, 8($15)\n" // a2 ($6)
  274. " lw $7, 12($15)\n" // a3 ($7)
  275. // load float registers
  276. " ldc1 $f12, 8($14)\n"
  277. " ldc1 $f14, 0($14)\n"
  278. // skip stack parameters if there are 4 or less as they are moved into the registers
  279. " addi $14, $3, -16\n" // The first 4 args were already loaded into registers
  280. " blez $14, andCall\n"
  281. " nop\n"
  282. // push stack parameters
  283. "pushArgs:\n"
  284. " addi $3, -4\n"
  285. // load from $15 + stack bytes ($3)
  286. " addu $14, $15, $3\n"
  287. " lw $14, 0($14)\n"
  288. // store to $sp + stack bytes ($3)
  289. " addu $13, $sp, $3\n"
  290. " sw $14, 0($13)\n"
  291. // if there are more, loop...
  292. " bne $3, $0, pushArgs\n"
  293. " nop\n"
  294. // and call the function
  295. "andCall:\n"
  296. " jalr $25\n"
  297. " nop\n"
  298. // restore original stack pointer
  299. " move $sp, $16\n"
  300. // restore the return pointer
  301. " lw $31, -8($sp)\n"
  302. // restore the original value of $16
  303. " lw $16, -4($sp)\n"
  304. // and return from the function
  305. " jr $31\n"
  306. " nop\n"
  307. " .set macro\n"
  308. " .set reorder\n"
  309. " .end mipsFunc\n"
  310. " .cfi_endproc\n"
  311. " .size mipsFunc, .-mipsFunc\n"
  312. );
  313. #else // !(defined(__linux__) && defined(_ABIO32))
  314. // The MIPS ABI used by PSP and PS2 is implemented here
  315. #define AS_MIPS_MAX_ARGS 32
  316. #define AS_NUM_REG_FLOATS 8
  317. #define AS_NUM_REG_INTS 8
  318. // The array used to send values to the correct places.
  319. // first 0-8 regular values to load into the a0-a3, t0-t3 registers
  320. // then 0-8 float values to load into the f12-f19 registers
  321. // then (AS_MIPS_MAX_ARGS - 16) values to load onto the stack
  322. // the +1 is for when CallThis (object methods) is used
  323. // extra +1 when returning in memory
  324. extern "C" {
  325. // TODO: This array shouldn't be global. It should be a local array in CallSystemFunctionNative
  326. asDWORD mipsArgs[AS_MIPS_MAX_ARGS + 1 + 1];
  327. }
  328. // Loads all data into the correct places and calls the function.
  329. // intArgSize is the size in bytes for how much data to put in int registers
  330. // floatArgSize is the size in bytes for how much data to put in float registers
  331. // stackArgSize is the size in bytes for how much data to put on the callstack
  332. extern "C" asQWORD mipsFunc(int intArgSize, int floatArgSize, int stackArgSize, asDWORD func);
  333. // puts the arguments in the correct place in the mipsArgs-array. See comments above.
  334. // This could be done better.
  335. inline void splitArgs(const asDWORD *args, int argNum, int &numRegIntArgs, int &numRegFloatArgs, int &numRestArgs, int hostFlags)
  336. {
  337. int i;
  338. int argBit = 1;
  339. for (i = 0; i < argNum; i++)
  340. {
  341. if (hostFlags & argBit)
  342. {
  343. if (numRegFloatArgs < AS_NUM_REG_FLOATS)
  344. {
  345. // put in float register
  346. mipsArgs[AS_NUM_REG_INTS + numRegFloatArgs] = args[i];
  347. numRegFloatArgs++;
  348. }
  349. else
  350. {
  351. // put in stack
  352. mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + numRestArgs] = args[i];
  353. numRestArgs++;
  354. }
  355. }
  356. else
  357. {
  358. if (numRegIntArgs < AS_NUM_REG_INTS)
  359. {
  360. // put in int register
  361. mipsArgs[numRegIntArgs] = args[i];
  362. numRegIntArgs++;
  363. }
  364. else
  365. {
  366. // put in stack
  367. mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + numRestArgs] = args[i];
  368. numRestArgs++;
  369. }
  370. }
  371. argBit <<= 1;
  372. }
  373. }
  374. asQWORD CallCDeclFunction(const asDWORD *args, int argSize, asDWORD func, int flags)
  375. {
  376. int argNum = argSize >> 2;
  377. int intArgs = 0;
  378. int floatArgs = 0;
  379. int restArgs = 0;
  380. // put the arguments in the correct places in the mipsArgs array
  381. if(argNum > 0)
  382. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  383. return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  384. }
  385. // This function is identical to CallCDeclFunction, with the only difference that
  386. // the value in the first parameter is the object
  387. asQWORD CallThisCallFunction(const void *obj, const asDWORD *args, int argSize, asDWORD func, int flags)
  388. {
  389. int argNum = argSize >> 2;
  390. int intArgs = 1;
  391. int floatArgs = 0;
  392. int restArgs = 0;
  393. mipsArgs[0] = (asDWORD) obj;
  394. // put the arguments in the correct places in the mipsArgs array
  395. if (argNum > 0)
  396. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  397. return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  398. }
  399. // This function is identical to CallCDeclFunction, with the only difference that
  400. // the value in the last parameter is the object
  401. asQWORD CallThisCallFunction_objLast(const void *obj, const asDWORD *args, int argSize, asDWORD func, int flags)
  402. {
  403. int argNum = argSize >> 2;
  404. int intArgs = 0;
  405. int floatArgs = 0;
  406. int restArgs = 0;
  407. // put the arguments in the correct places in the mipsArgs array
  408. if(argNum > 0)
  409. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  410. if(intArgs < AS_NUM_REG_INTS)
  411. {
  412. mipsArgs[intArgs] = (asDWORD) obj;
  413. intArgs++;
  414. }
  415. else
  416. {
  417. mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + restArgs] = (asDWORD) obj;
  418. restArgs++;
  419. }
  420. return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  421. }
  422. asDWORD GetReturnedFloat()
  423. {
  424. asDWORD f;
  425. asm("swc1 $f0, %0\n" : "=m"(f));
  426. return f;
  427. }
  428. asQWORD GetReturnedDouble()
  429. {
  430. asQWORD d = 0;
  431. asm("sdc1 $f0, %0\n" : "=m"(d));
  432. return d;
  433. }
  434. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/, void */*secondObject*/)
  435. {
  436. asCScriptEngine *engine = context->m_engine;
  437. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  438. int callConv = sysFunc->callConv;
  439. // TODO: Mips does not yet support THISCALL_OBJFIRST/LAST
  440. asQWORD retQW = 0;
  441. void *func = (void*)sysFunc->func;
  442. int paramSize = sysFunc->paramSize;
  443. asDWORD *vftable;
  444. if( descr->returnType.IsObject() && !descr->returnType.IsReference() && !descr->returnType.IsObjectHandle() )
  445. {
  446. mipsArgs[AS_MIPS_MAX_ARGS+1] = (asDWORD) retPointer;
  447. }
  448. asASSERT(descr->parameterTypes.GetLength() <= AS_MIPS_MAX_ARGS);
  449. // mark all float arguments
  450. int argBit = 1;
  451. int hostFlags = 0;
  452. int intArgs = 0;
  453. for( size_t a = 0; a < descr->parameterTypes.GetLength(); a++ )
  454. {
  455. if (descr->parameterTypes[a].IsFloatType())
  456. hostFlags |= argBit;
  457. else
  458. intArgs++;
  459. argBit <<= 1;
  460. }
  461. asDWORD paramBuffer[64];
  462. if( sysFunc->takesObjByVal )
  463. {
  464. paramSize = 0;
  465. int spos = 0;
  466. int dpos = 1;
  467. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  468. {
  469. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  470. {
  471. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  472. if( descr->parameterTypes[n].GetTypeInfo()->flags & COMPLEX_MASK )
  473. {
  474. paramBuffer[dpos++] = args[spos++];
  475. paramSize++;
  476. }
  477. else
  478. #endif
  479. {
  480. // Copy the object's memory to the buffer
  481. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  482. // Delete the original memory
  483. engine->CallFree(*(char**)(args+spos));
  484. spos++;
  485. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  486. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  487. }
  488. }
  489. else
  490. {
  491. // Copy the value directly
  492. paramBuffer[dpos++] = args[spos++];
  493. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  494. paramBuffer[dpos++] = args[spos++];
  495. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  496. }
  497. }
  498. // Keep a free location at the beginning
  499. args = &paramBuffer[1];
  500. }
  501. switch( callConv )
  502. {
  503. case ICC_CDECL:
  504. case ICC_CDECL_RETURNINMEM:
  505. case ICC_STDCALL:
  506. case ICC_STDCALL_RETURNINMEM:
  507. retQW = CallCDeclFunction(args, paramSize<<2, (asDWORD)func, hostFlags);
  508. break;
  509. case ICC_THISCALL:
  510. case ICC_THISCALL_RETURNINMEM:
  511. retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  512. break;
  513. case ICC_VIRTUAL_THISCALL:
  514. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  515. // Get virtual function table from the object pointer
  516. vftable = *(asDWORD**)obj;
  517. retQW = CallThisCallFunction(obj, args, paramSize<<2, vftable[asDWORD(func)>>2], hostFlags);
  518. break;
  519. case ICC_CDECL_OBJLAST:
  520. case ICC_CDECL_OBJLAST_RETURNINMEM:
  521. retQW = CallThisCallFunction_objLast(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  522. break;
  523. case ICC_CDECL_OBJFIRST:
  524. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  525. retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  526. break;
  527. default:
  528. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  529. }
  530. // If the return is a float value we need to get the value from the FP register
  531. if( sysFunc->hostReturnFloat )
  532. {
  533. if( sysFunc->hostReturnSize == 1 )
  534. *(asDWORD*)&retQW = GetReturnedFloat();
  535. else
  536. retQW = GetReturnedDouble();
  537. }
  538. return retQW;
  539. }
  540. asm(
  541. " .text\n"
  542. //" .align 2\n"
  543. " .global mipsFunc\n"
  544. " .ent mipsFunc\n"
  545. "mipsFunc:\n"
  546. //" .frame $fp,64,$31 # vars= 0, regs= 0/0, args= 0, gp= 0\n"
  547. //" .mask 0x00000000,0\n"
  548. //" .fmask 0x00000000,0\n"
  549. " .set noreorder\n"
  550. " .set nomacro\n"
  551. // align the stack frame to 8 bytes
  552. " addiu $12, $6, 7\n"
  553. " li $13, -8\n" // 0xfffffffffffffff8
  554. " and $12, $12, $13\n" // t4 holds the size of the argument block
  555. // and add 8 bytes for the return pointer and s0 backup
  556. " addiu $13, $12, 8\n" // t5 holds the total size of the stack frame (including return pointer)
  557. // save the s0 register (so we can use it to remember where our return pointer is lives)
  558. " sw $16, -4($sp)\n" // store the s0 register (so we can use it to remember how big our stack frame is)
  559. // push the stack
  560. " subu $sp, $sp, $13\n"
  561. // find the return address, place in s0
  562. " addu $16, $sp, $12\n"
  563. // store the return pointer
  564. " sw $31, 0($16)\n"
  565. // backup our function params
  566. " addiu $2, $7, 0\n"
  567. " addiu $3, $6, 0\n"
  568. // get global mipsArgs[] array pointer
  569. //" lui $15, %hi(mipsArgs)\n"
  570. //" addiu $15, $15, %lo(mipsArgs)\n"
  571. // we'll use the macro instead because SN Systems doesnt like %hi/%lo
  572. ".set macro\n"
  573. " la $15, mipsArgs\n"
  574. ".set nomacro\n"
  575. // load register params
  576. " lw $4, 0($15)\n"
  577. " lw $5, 4($15)\n"
  578. " lw $6, 8($15)\n"
  579. " lw $7, 12($15)\n"
  580. " lw $8, 16($15)\n"
  581. " lw $9, 20($15)\n"
  582. " lw $10, 24($15)\n"
  583. " lw $11, 28($15)\n"
  584. // load float params
  585. " lwc1 $f12, 32($15)\n"
  586. " lwc1 $f13, 36($15)\n"
  587. " lwc1 $f14, 40($15)\n"
  588. " lwc1 $f15, 44($15)\n"
  589. " lwc1 $f16, 48($15)\n"
  590. " lwc1 $f17, 52($15)\n"
  591. " lwc1 $f18, 56($15)\n"
  592. " lwc1 $f19, 60($15)\n"
  593. // skip stack paramaters if there are none
  594. " beq $3, $0, andCall\n"
  595. // push stack paramaters
  596. " addiu $15, $15, 64\n"
  597. "pushArgs:\n"
  598. " addiu $3, -4\n"
  599. // load from $15 + stack bytes ($3)
  600. " addu $14, $15, $3\n"
  601. " lw $14, 0($14)\n"
  602. // store to $sp + stack bytes ($3)
  603. " addu $13, $sp, $3\n"
  604. " sw $14, 0($13)\n"
  605. // if there are more, loop...
  606. " bne $3, $0, pushArgs\n"
  607. " nop\n"
  608. // and call the function
  609. "andCall:\n"
  610. " jal $2\n"
  611. " nop\n"
  612. // restore the return pointer
  613. " lw $31, 0($16)\n"
  614. // pop the stack pointer (remembering the return pointer was 8 bytes below the top)
  615. " addiu $sp, $16, 8\n"
  616. // and return from the function
  617. " jr $31\n"
  618. // restore the s0 register (in the branch delay slot)
  619. " lw $16, -4($sp)\n"
  620. " .set macro\n"
  621. " .set reorder\n"
  622. " .end mipsFunc\n"
  623. " .size mipsFunc, .-mipsFunc\n"
  624. );
  625. #endif // PSP and PS2 MIPS ABI
  626. END_AS_NAMESPACE
  627. #endif // AS_MIPS
  628. #endif // AS_MAX_PORTABILITY