as_callfunc_mips.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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_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*/)
  182. {
  183. asCScriptEngine *engine = context->m_engine;
  184. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  185. int callConv = sysFunc->callConv;
  186. asQWORD retQW = 0;
  187. void *func = (void*)sysFunc->func;
  188. int paramSize = sysFunc->paramSize;
  189. asDWORD *vftable;
  190. if( descr->returnType.IsObject() && !descr->returnType.IsReference() && !descr->returnType.IsObjectHandle() )
  191. {
  192. mipsArgs[AS_MIPS_MAX_ARGS+1] = (asDWORD) retPointer;
  193. }
  194. asASSERT(descr->parameterTypes.GetLength() <= AS_MIPS_MAX_ARGS);
  195. // mark all float arguments
  196. int argBit = 1;
  197. int hostFlags = 0;
  198. int intArgs = 0;
  199. for( size_t a = 0; a < descr->parameterTypes.GetLength(); a++ )
  200. {
  201. if (descr->parameterTypes[a].IsFloatType())
  202. hostFlags |= argBit;
  203. else
  204. intArgs++;
  205. argBit <<= 1;
  206. }
  207. asDWORD paramBuffer[64];
  208. if( sysFunc->takesObjByVal )
  209. {
  210. paramSize = 0;
  211. int spos = 0;
  212. int dpos = 1;
  213. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  214. {
  215. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  216. {
  217. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  218. if( descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK )
  219. {
  220. paramBuffer[dpos++] = args[spos++];
  221. paramSize++;
  222. }
  223. else
  224. #endif
  225. {
  226. // Copy the object's memory to the buffer
  227. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  228. // Delete the original memory
  229. engine->CallFree(*(char**)(args+spos));
  230. spos++;
  231. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  232. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  233. }
  234. }
  235. else
  236. {
  237. // Copy the value directly
  238. paramBuffer[dpos++] = args[spos++];
  239. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  240. paramBuffer[dpos++] = args[spos++];
  241. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  242. }
  243. }
  244. // Keep a free location at the beginning
  245. args = &paramBuffer[1];
  246. }
  247. switch( callConv )
  248. {
  249. case ICC_CDECL:
  250. case ICC_CDECL_RETURNINMEM:
  251. case ICC_STDCALL:
  252. case ICC_STDCALL_RETURNINMEM:
  253. retQW = CallCDeclFunction(args, paramSize<<2, (asDWORD)func, hostFlags);
  254. break;
  255. case ICC_THISCALL:
  256. case ICC_THISCALL_RETURNINMEM:
  257. retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  258. break;
  259. case ICC_VIRTUAL_THISCALL:
  260. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  261. // Get virtual function table from the object pointer
  262. vftable = *(asDWORD**)obj;
  263. retQW = CallThisCallFunction(obj, args, paramSize<<2, vftable[asDWORD(func)>>2], hostFlags);
  264. break;
  265. case ICC_CDECL_OBJLAST:
  266. case ICC_CDECL_OBJLAST_RETURNINMEM:
  267. retQW = CallThisCallFunction_objLast(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  268. break;
  269. case ICC_CDECL_OBJFIRST:
  270. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  271. retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  272. break;
  273. default:
  274. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  275. }
  276. // If the return is a float value we need to get the value from the FP register
  277. if( sysFunc->hostReturnFloat )
  278. {
  279. if( sysFunc->hostReturnSize == 1 )
  280. *(asDWORD*)&retQW = GetReturnedFloat();
  281. else
  282. retQW = GetReturnedDouble();
  283. }
  284. return retQW;
  285. }
  286. asm(
  287. " .text\n"
  288. //" .align 2\n"
  289. " .global mipsFunc\n"
  290. " .ent mipsFunc\n"
  291. "mipsFunc:\n"
  292. //" .frame $fp,64,$31 # vars= 0, regs= 0/0, args= 0, gp= 0\n"
  293. //" .mask 0x00000000,0\n"
  294. //" .fmask 0x00000000,0\n"
  295. " .set noreorder\n"
  296. " .set nomacro\n"
  297. // align the stack frame to 8 bytes
  298. " addiu $12, $6, 7\n"
  299. " li $13, -8\n" // 0xfffffffffffffffc
  300. " and $12, $12, $13\n" // t4 holds the size of the argument block
  301. // and add 8 bytes for the return pointer and s0 backup
  302. " addiu $13, $12, 8\n" // t5 holds the total size of the stack frame (including return pointer)
  303. // save the s0 register (so we can use it to remember where our return pointer is lives)
  304. " sw $16, -4($sp)\n" // store the s0 register (so we can use it to remember how big our stack frame is)
  305. // push the stack
  306. " subu $sp, $sp, $13\n"
  307. // find the return address, place in s0
  308. " addu $16, $sp, $12\n"
  309. // store the return pointer
  310. " sw $31, 0($16)\n"
  311. // backup our function params
  312. " addiu $2, $7, 0\n"
  313. " addiu $3, $6, 0\n"
  314. // get global mipsArgs[] array pointer
  315. //" lui $15, %hi(mipsArgs)\n"
  316. //" addiu $15, $15, %lo(mipsArgs)\n"
  317. // we'll use the macro instead because SN Systems doesnt like %hi/%lo
  318. ".set macro\n"
  319. " la $15, mipsArgs\n"
  320. ".set nomacro\n"
  321. // load register params
  322. " lw $4, 0($15)\n"
  323. " lw $5, 4($15)\n"
  324. " lw $6, 8($15)\n"
  325. " lw $7, 12($15)\n"
  326. " lw $8, 16($15)\n"
  327. " lw $9, 20($15)\n"
  328. " lw $10, 24($15)\n"
  329. " lw $11, 28($15)\n"
  330. // load float params
  331. " lwc1 $f12, 32($15)\n"
  332. " lwc1 $f13, 36($15)\n"
  333. " lwc1 $f14, 40($15)\n"
  334. " lwc1 $f15, 44($15)\n"
  335. " lwc1 $f16, 48($15)\n"
  336. " lwc1 $f17, 52($15)\n"
  337. " lwc1 $f18, 56($15)\n"
  338. " lwc1 $f19, 60($15)\n"
  339. // skip stack paramaters if there are none
  340. " beq $3, $0, andCall\n"
  341. // push stack paramaters
  342. " addiu $15, $15, 64\n"
  343. "pushArgs:\n"
  344. " addiu $3, -4\n"
  345. // load from $15 + stack bytes ($3)
  346. " addu $14, $15, $3\n"
  347. " lw $14, 0($14)\n"
  348. // store to $sp + stack bytes ($3)
  349. " addu $13, $sp, $3\n"
  350. " sw $14, 0($13)\n"
  351. // if there are more, loop...
  352. " bne $3, $0, pushArgs\n"
  353. " nop\n"
  354. // and call the function
  355. "andCall:\n"
  356. " jal $2\n"
  357. " nop\n"
  358. // restore the return pointer
  359. " lw $31, 0($16)\n"
  360. // pop the stack pointer (remembering the return pointer was 8 bytes below the top)
  361. " addiu $sp, $16, 8\n"
  362. // and return from the function
  363. " jr $31\n"
  364. // restore the s0 register (in the branch delay slot)
  365. " lw $16, -4($sp)\n"
  366. " .set macro\n"
  367. " .set reorder\n"
  368. " .end mipsFunc\n"
  369. " .size mipsFunc, .-mipsFunc\n"
  370. );
  371. END_AS_NAMESPACE
  372. #endif // AS_MIPS
  373. #endif // AS_MAX_PORTABILITY