as_callfunc_mips.cpp 12 KB

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