as_callfunc_mips.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 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_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
  30. //
  31. #include "as_config.h"
  32. #ifndef AS_MAX_PORTABILITY
  33. #ifdef AS_MIPS
  34. #include "as_callfunc.h"
  35. #include "as_scriptengine.h"
  36. #include "as_texts.h"
  37. #include "as_tokendef.h"
  38. #include "as_context.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <regdef.h>
  42. BEGIN_AS_NAMESPACE
  43. #define AS_MIPS_MAX_ARGS 32
  44. #define AS_NUM_REG_FLOATS 8
  45. #define AS_NUM_REG_INTS 8
  46. // The array used to send values to the correct places.
  47. // first 0-8 regular values to load into the a0-a3, t0-t3 registers
  48. // then 0-8 float values to load into the f12-f19 registers
  49. // then (AS_MIPS_MAX_ARGS - 16) values to load onto the stack
  50. // the +1 is for when CallThis (object methods) is used
  51. // extra +1 when returning in memory
  52. extern "C" {
  53. // TODO: This array shouldn't be global. It should be a local array in CallSystemFunctionNative
  54. asDWORD mipsArgs[AS_MIPS_MAX_ARGS + 1 + 1];
  55. }
  56. // Loads all data into the correct places and calls the function.
  57. // intArgSize is the size in bytes for how much data to put in int registers
  58. // floatArgSize is the size in bytes for how much data to put in float registers
  59. // stackArgSize is the size in bytes for how much data to put on the callstack
  60. extern "C" asQWORD mipsFunc(int intArgSize, int floatArgSize, int stackArgSize, asDWORD func);
  61. // puts the arguments in the correct place in the mipsArgs-array. See comments above.
  62. // This could be done better.
  63. inline void splitArgs(const asDWORD *args, int argNum, int &numRegIntArgs, int &numRegFloatArgs, int &numRestArgs, int hostFlags)
  64. {
  65. int i;
  66. int argBit = 1;
  67. for (i = 0; i < argNum; i++)
  68. {
  69. if (hostFlags & argBit)
  70. {
  71. if (numRegFloatArgs < AS_NUM_REG_FLOATS)
  72. {
  73. // put in float register
  74. mipsArgs[AS_NUM_REG_INTS + numRegFloatArgs] = args[i];
  75. numRegFloatArgs++;
  76. }
  77. else
  78. {
  79. // put in stack
  80. mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + numRestArgs] = args[i];
  81. numRestArgs++;
  82. }
  83. }
  84. else
  85. {
  86. if (numRegIntArgs < AS_NUM_REG_INTS)
  87. {
  88. // put in int register
  89. mipsArgs[numRegIntArgs] = args[i];
  90. numRegIntArgs++;
  91. }
  92. else
  93. {
  94. // put in stack
  95. mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + numRestArgs] = args[i];
  96. numRestArgs++;
  97. }
  98. }
  99. argBit <<= 1;
  100. }
  101. }
  102. asQWORD CallCDeclFunction(const asDWORD *args, int argSize, asDWORD func, int flags)
  103. {
  104. int argNum = argSize >> 2;
  105. int intArgs = 0;
  106. int floatArgs = 0;
  107. int restArgs = 0;
  108. // put the arguments in the correct places in the mipsArgs array
  109. if(argNum > 0)
  110. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  111. return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  112. }
  113. // This function is identical to CallCDeclFunction, with the only difference that
  114. // the value in the first parameter is the object
  115. asQWORD CallThisCallFunction(const void *obj, const asDWORD *args, int argSize, asDWORD func, int flags)
  116. {
  117. int argNum = argSize >> 2;
  118. int intArgs = 1;
  119. int floatArgs = 0;
  120. int restArgs = 0;
  121. mipsArgs[0] = (asDWORD) obj;
  122. // put the arguments in the correct places in the mipsArgs array
  123. if (argNum > 0)
  124. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  125. return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  126. }
  127. // This function is identical to CallCDeclFunction, with the only difference that
  128. // the value in the last parameter is the object
  129. asQWORD CallThisCallFunction_objLast(const void *obj, const asDWORD *args, int argSize, asDWORD func, int flags)
  130. {
  131. int argNum = argSize >> 2;
  132. int intArgs = 0;
  133. int floatArgs = 0;
  134. int restArgs = 0;
  135. // put the arguments in the correct places in the mipsArgs array
  136. if(argNum > 0)
  137. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  138. if(intArgs < AS_NUM_REG_INTS)
  139. {
  140. mipsArgs[intArgs] = (asDWORD) obj;
  141. intArgs++;
  142. }
  143. else
  144. {
  145. mipsArgs[AS_NUM_REG_INTS + AS_NUM_REG_FLOATS + restArgs] = (asDWORD) obj;
  146. restArgs++;
  147. }
  148. return mipsFunc(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  149. }
  150. asDWORD GetReturnedFloat()
  151. {
  152. asDWORD f;
  153. asm("swc1 $f0, %0\n" : "=m"(f));
  154. return f;
  155. }
  156. /*
  157. asDWORD GetReturnedFloat();
  158. asm(
  159. " .align 4\n"
  160. " .global GetReturnedFloat\n"
  161. "GetReturnedFloat:\n"
  162. " .set noreorder\n"
  163. " .set nomacro\n"
  164. " j $ra\n"
  165. " mfc1 $v0, $f0\n"
  166. " .set macro\n"
  167. " .set reorder\n"
  168. " .end Func\n"
  169. */
  170. // sizeof(double) == 4 with sh-elf-gcc (3.4.0) -m4
  171. // so this isn't really used...
  172. asQWORD GetReturnedDouble()
  173. {
  174. asQWORD d = 0;
  175. printf("Broken!!!");
  176. /*
  177. asm("sw $v0, %0\n" : "=m"(d));
  178. */
  179. return d;
  180. }
  181. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/, void */*secondObject*/)
  182. {
  183. asCScriptEngine *engine = context->m_engine;
  184. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  185. int callConv = sysFunc->callConv;
  186. // TODO: Mips does not yet support THISCALL_OBJFIRST/LAST
  187. asQWORD retQW = 0;
  188. void *func = (void*)sysFunc->func;
  189. int paramSize = sysFunc->paramSize;
  190. asDWORD *vftable;
  191. if( descr->returnType.IsObject() && !descr->returnType.IsReference() && !descr->returnType.IsObjectHandle() )
  192. {
  193. mipsArgs[AS_MIPS_MAX_ARGS+1] = (asDWORD) retPointer;
  194. }
  195. asASSERT(descr->parameterTypes.GetLength() <= AS_MIPS_MAX_ARGS);
  196. // mark all float arguments
  197. int argBit = 1;
  198. int hostFlags = 0;
  199. int intArgs = 0;
  200. for( size_t a = 0; a < descr->parameterTypes.GetLength(); a++ )
  201. {
  202. if (descr->parameterTypes[a].IsFloatType())
  203. hostFlags |= argBit;
  204. else
  205. intArgs++;
  206. argBit <<= 1;
  207. }
  208. asDWORD paramBuffer[64];
  209. if( sysFunc->takesObjByVal )
  210. {
  211. paramSize = 0;
  212. int spos = 0;
  213. int dpos = 1;
  214. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  215. {
  216. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  217. {
  218. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  219. if( descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK )
  220. {
  221. paramBuffer[dpos++] = args[spos++];
  222. paramSize++;
  223. }
  224. else
  225. #endif
  226. {
  227. // Copy the object's memory to the buffer
  228. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  229. // Delete the original memory
  230. engine->CallFree(*(char**)(args+spos));
  231. spos++;
  232. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  233. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  234. }
  235. }
  236. else
  237. {
  238. // Copy the value directly
  239. paramBuffer[dpos++] = args[spos++];
  240. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  241. paramBuffer[dpos++] = args[spos++];
  242. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  243. }
  244. }
  245. // Keep a free location at the beginning
  246. args = &paramBuffer[1];
  247. }
  248. switch( callConv )
  249. {
  250. case ICC_CDECL:
  251. case ICC_CDECL_RETURNINMEM:
  252. case ICC_STDCALL:
  253. case ICC_STDCALL_RETURNINMEM:
  254. retQW = CallCDeclFunction(args, paramSize<<2, (asDWORD)func, hostFlags);
  255. break;
  256. case ICC_THISCALL:
  257. case ICC_THISCALL_RETURNINMEM:
  258. retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  259. break;
  260. case ICC_VIRTUAL_THISCALL:
  261. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  262. // Get virtual function table from the object pointer
  263. vftable = *(asDWORD**)obj;
  264. retQW = CallThisCallFunction(obj, args, paramSize<<2, vftable[asDWORD(func)>>2], hostFlags);
  265. break;
  266. case ICC_CDECL_OBJLAST:
  267. case ICC_CDECL_OBJLAST_RETURNINMEM:
  268. retQW = CallThisCallFunction_objLast(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  269. break;
  270. case ICC_CDECL_OBJFIRST:
  271. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  272. retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  273. break;
  274. default:
  275. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  276. }
  277. // If the return is a float value we need to get the value from the FP register
  278. if( sysFunc->hostReturnFloat )
  279. {
  280. if( sysFunc->hostReturnSize == 1 )
  281. *(asDWORD*)&retQW = GetReturnedFloat();
  282. else
  283. retQW = GetReturnedDouble();
  284. }
  285. return retQW;
  286. }
  287. asm(
  288. " .text\n"
  289. //" .align 2\n"
  290. " .global mipsFunc\n"
  291. " .ent mipsFunc\n"
  292. "mipsFunc:\n"
  293. //" .frame $fp,64,$31 # vars= 0, regs= 0/0, args= 0, gp= 0\n"
  294. //" .mask 0x00000000,0\n"
  295. //" .fmask 0x00000000,0\n"
  296. " .set noreorder\n"
  297. " .set nomacro\n"
  298. // align the stack frame to 8 bytes
  299. " addiu $12, $6, 7\n"
  300. " li $13, -8\n" // 0xfffffffffffffffc
  301. " and $12, $12, $13\n" // t4 holds the size of the argument block
  302. // and add 8 bytes for the return pointer and s0 backup
  303. " addiu $13, $12, 8\n" // t5 holds the total size of the stack frame (including return pointer)
  304. // save the s0 register (so we can use it to remember where our return pointer is lives)
  305. " sw $16, -4($sp)\n" // store the s0 register (so we can use it to remember how big our stack frame is)
  306. // push the stack
  307. " subu $sp, $sp, $13\n"
  308. // find the return address, place in s0
  309. " addu $16, $sp, $12\n"
  310. // store the return pointer
  311. " sw $31, 0($16)\n"
  312. // backup our function params
  313. " addiu $2, $7, 0\n"
  314. " addiu $3, $6, 0\n"
  315. // get global mipsArgs[] array pointer
  316. //" lui $15, %hi(mipsArgs)\n"
  317. //" addiu $15, $15, %lo(mipsArgs)\n"
  318. // we'll use the macro instead because SN Systems doesnt like %hi/%lo
  319. ".set macro\n"
  320. " la $15, mipsArgs\n"
  321. ".set nomacro\n"
  322. // load register params
  323. " lw $4, 0($15)\n"
  324. " lw $5, 4($15)\n"
  325. " lw $6, 8($15)\n"
  326. " lw $7, 12($15)\n"
  327. " lw $8, 16($15)\n"
  328. " lw $9, 20($15)\n"
  329. " lw $10, 24($15)\n"
  330. " lw $11, 28($15)\n"
  331. // load float params
  332. " lwc1 $f12, 32($15)\n"
  333. " lwc1 $f13, 36($15)\n"
  334. " lwc1 $f14, 40($15)\n"
  335. " lwc1 $f15, 44($15)\n"
  336. " lwc1 $f16, 48($15)\n"
  337. " lwc1 $f17, 52($15)\n"
  338. " lwc1 $f18, 56($15)\n"
  339. " lwc1 $f19, 60($15)\n"
  340. // skip stack paramaters if there are none
  341. " beq $3, $0, andCall\n"
  342. // push stack paramaters
  343. " addiu $15, $15, 64\n"
  344. "pushArgs:\n"
  345. " addiu $3, -4\n"
  346. // load from $15 + stack bytes ($3)
  347. " addu $14, $15, $3\n"
  348. " lw $14, 0($14)\n"
  349. // store to $sp + stack bytes ($3)
  350. " addu $13, $sp, $3\n"
  351. " sw $14, 0($13)\n"
  352. // if there are more, loop...
  353. " bne $3, $0, pushArgs\n"
  354. " nop\n"
  355. // and call the function
  356. "andCall:\n"
  357. " jal $2\n"
  358. " nop\n"
  359. // restore the return pointer
  360. " lw $31, 0($16)\n"
  361. // pop the stack pointer (remembering the return pointer was 8 bytes below the top)
  362. " addiu $sp, $16, 8\n"
  363. // and return from the function
  364. " jr $31\n"
  365. // restore the s0 register (in the branch delay slot)
  366. " lw $16, -4($sp)\n"
  367. " .set macro\n"
  368. " .set reorder\n"
  369. " .end mipsFunc\n"
  370. " .size mipsFunc, .-mipsFunc\n"
  371. );
  372. END_AS_NAMESPACE
  373. #endif // AS_MIPS
  374. #endif // AS_MAX_PORTABILITY