as_callfunc_mips.cpp 12 KB

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