as_callfunc_sh4.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_sh4.cpp
  25. //
  26. // These functions handle the actual calling of system functions
  27. //
  28. // This version is SH4 specific and was originally written
  29. // by Fredrik Ehnbom in May, 2004
  30. // Later updated for angelscript 2.0.0 by Fredrik Ehnbom in Jan, 2005
  31. // References:
  32. // * http://www.renesas.com/avs/resource/japan/eng/pdf/mpumcu/e602156_sh4.pdf
  33. // * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcechp40/html/_callsh4_SH_4_Calling_Standard.asp
  34. #include "as_config.h"
  35. #ifndef AS_MAX_PORTABILITY
  36. #ifdef AS_SH4
  37. #include "as_callfunc.h"
  38. #include "as_scriptengine.h"
  39. #include "as_texts.h"
  40. #include "as_tokendef.h"
  41. #include "as_context.h"
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. BEGIN_AS_NAMESPACE
  45. #define AS_SH4_MAX_ARGS 32
  46. // The array used to send values to the correct places.
  47. // first 0-4 regular values to load into the r4-r7 registers
  48. // then 0-8 float values to load into the fr4-fr11 registers
  49. // then (AS_SH4_MAX_ARGS - 12) 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. static asDWORD sh4Args[AS_SH4_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 sh4Func(int intArgSize, int floatArgSize, int stackArgSize, asDWORD func);
  60. asm(""
  61. " .align 4\n"
  62. " .global _sh4Func\n"
  63. "_sh4Func:\n"
  64. " mov.l r14,@-r15\n"
  65. " mov.l r13,@-r15\n"
  66. " mov.l r12,@-r15\n"
  67. " sts.l pr,@-r15\n" // must be saved since we call a subroutine
  68. " mov r7, r14\n" // func
  69. " mov r6, r13\n" // stackArgSize
  70. " mov.l r5,@-r15\n" // floatArgSize
  71. " mov.l sh4Args,r0\n"
  72. " pref @r0\n"
  73. " mov r4, r1\n" // intArgsize
  74. " mov #33*4,r2\n"
  75. " extu.b r2,r2\n" // make unsigned (33*4 = 132 => 128)
  76. " mov.l @(r0,r2), r2\n" // r2 has adress for when returning in memory
  77. "_sh4f_intarguments:\n" // copy all the int arguments to the respective registers
  78. " mov #4*2*2,r3\n" // calculate how many bytes to skip
  79. " sub r1,r3\n"
  80. " braf r3\n"
  81. " add #-4,r1\n" // we are indexing the array backwards, so subtract one (delayed slot)
  82. " mov.l @(r0,r1),r7\n" // 4 arguments
  83. " add #-4,r1\n"
  84. " mov.l @(r0,r1),r6\n" // 3 arguments
  85. " add #-4,r1\n"
  86. " mov.l @(r0,r1),r5\n" // 2 arguments
  87. " add #-4,r1\n"
  88. " mov.l @(r0,r1),r4\n" // 1 argument
  89. " nop\n"
  90. "_sh4f_floatarguments:\n" // copy all the float arguments to the respective registers
  91. " add #4*4, r0\n"
  92. " mov.l @r15+,r1\n" // floatArgSize
  93. " mov #8*2*2,r3\n" // calculate how many bytes to skip
  94. " sub r1,r3\n"
  95. " braf r3\n"
  96. " add #-4,r1\n" // we are indexing the array backwards, so subtract one (delayed slot)
  97. " fmov.s @(r0,r1),fr11\n" // 8 arguments
  98. " add #-4,r1\n"
  99. " fmov.s @(r0,r1),fr10\n" // 7 arguments
  100. " add #-4,r1\n"
  101. " fmov.s @(r0,r1),fr9\n" // 6 arguments
  102. " add #-4,r1\n"
  103. " fmov.s @(r0,r1),fr8\n" // 5 arguments
  104. " add #-4,r1\n"
  105. " fmov.s @(r0,r1),fr7\n" // 4 arguments
  106. " add #-4,r1\n"
  107. " fmov.s @(r0,r1),fr6\n" // 3 arguments
  108. " add #-4,r1\n"
  109. " fmov.s @(r0,r1),fr5\n" // 2 arguments
  110. " add #-4,r1\n"
  111. " fmov.s @(r0,r1),fr4\n" // 1 argument
  112. " nop\n"
  113. "_sh4f_stackarguments:\n" // copy all the stack argument onto the stack
  114. " add #8*4, r0\n"
  115. " mov r0, r1\n"
  116. " mov #0, r0\n" // init position counter (also used as a 0-check on the line after)
  117. " cmp/eq r0, r13\n"
  118. " bt _sh4f_functioncall\n" // no arguments to push onto the stack
  119. " mov r13, r3\n" // stackArgSize
  120. " sub r3,r15\n" // "allocate" space on the stack
  121. " shlr2 r3\n" // make into a counter
  122. "_sh4f_stackloop:\n"
  123. " mov.l @r1+, r12\n"
  124. " mov.l r12, @(r0, r15)\n"
  125. " add #4, r0\n"
  126. " dt r3\n"
  127. " bf _sh4f_stackloop\n"
  128. "_sh4f_functioncall:\n"
  129. " jsr @r14\n" // no arguments
  130. " nop\n"
  131. " add r13, r15\n" // restore stack position
  132. " lds.l @r15+,pr\n"
  133. " mov.l @r15+, r12\n"
  134. " mov.l @r15+, r13\n"
  135. " rts\n"
  136. " mov.l @r15+, r14\n" // delayed slot
  137. "\n"
  138. " .align 4\n"
  139. "sh4Args:\n"
  140. " .long _sh4Args\n"
  141. );
  142. // puts the arguments in the correct place in the sh4Args-array. See comments above.
  143. // This could be done better.
  144. inline void splitArgs(const asDWORD *args, int argNum, int &numRegIntArgs, int &numRegFloatArgs, int &numRestArgs, int hostFlags) {
  145. int i;
  146. int argBit = 1;
  147. for (i = 0; i < argNum; i++) {
  148. if (hostFlags & argBit) {
  149. if (numRegFloatArgs < 12 - 4) {
  150. // put in float register
  151. sh4Args[4 + numRegFloatArgs] = args[i];
  152. numRegFloatArgs++;
  153. } else {
  154. // put in stack
  155. sh4Args[4 + 8 + numRestArgs] = args[i];
  156. numRestArgs++;
  157. }
  158. } else {
  159. if (numRegIntArgs < 8 - 4) {
  160. // put in int register
  161. sh4Args[numRegIntArgs] = args[i];
  162. numRegIntArgs++;
  163. } else {
  164. // put in stack
  165. sh4Args[4 + 8 + numRestArgs] = args[i];
  166. numRestArgs++;
  167. }
  168. }
  169. argBit <<= 1;
  170. }
  171. }
  172. asQWORD CallCDeclFunction(const asDWORD *args, int argSize, asDWORD func, int flags)
  173. {
  174. int argNum = argSize >> 2;
  175. int intArgs = 0;
  176. int floatArgs = 0;
  177. int restArgs = 0;
  178. // put the arguments in the correct places in the sh4Args array
  179. if (argNum > 0)
  180. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  181. return sh4Func(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  182. }
  183. // This function is identical to CallCDeclFunction, with the only difference that
  184. // the value in the first parameter is the object
  185. asQWORD CallThisCallFunction(const void *obj, const asDWORD *args, int argSize, asDWORD func, int flags)
  186. {
  187. int argNum = argSize >> 2;
  188. int intArgs = 1;
  189. int floatArgs = 0;
  190. int restArgs = 0;
  191. sh4Args[0] = (asDWORD) obj;
  192. // put the arguments in the correct places in the sh4Args array
  193. if (argNum >= 1)
  194. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  195. return sh4Func(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  196. }
  197. // This function is identical to CallCDeclFunction, with the only difference that
  198. // the value in the last parameter is the object
  199. asQWORD CallThisCallFunction_objLast(const void *obj, const asDWORD *args, int argSize, asDWORD func, int flags)
  200. {
  201. int argNum = argSize >> 2;
  202. int intArgs = 0;
  203. int floatArgs = 0;
  204. int restArgs = 0;
  205. // put the arguments in the correct places in the sh4Args array
  206. if (argNum >= 1)
  207. splitArgs(args, argNum, intArgs, floatArgs, restArgs, flags);
  208. if (intArgs < 4) {
  209. sh4Args[intArgs] = (asDWORD) obj;
  210. intArgs++;
  211. } else {
  212. sh4Args[4 + 8 + restArgs] = (asDWORD) obj;
  213. restArgs++;
  214. }
  215. return sh4Func(intArgs << 2, floatArgs << 2, restArgs << 2, func);
  216. }
  217. asDWORD GetReturnedFloat()
  218. {
  219. asDWORD f;
  220. asm("fmov.s fr0, %0\n" : "=m"(f));
  221. return f;
  222. }
  223. // sizeof(double) == 4 with sh-elf-gcc (3.4.0) -m4
  224. // so this isn't really used...
  225. asQWORD GetReturnedDouble()
  226. {
  227. asQWORD d;
  228. asm("fmov dr0, %0\n" : "=m"(d));
  229. return d;
  230. }
  231. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/, void */*secondObject*/)
  232. {
  233. // TODO: SH4 does not yet support THISCALL_OBJFIRST/LAST
  234. asCScriptEngine *engine = context->m_engine;
  235. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  236. int callConv = sysFunc->callConv;
  237. asQWORD retQW = 0;
  238. void *func = (void*)sysFunc->func;
  239. int paramSize = sysFunc->paramSize;
  240. asDWORD *vftable;
  241. if( descr->returnType.IsObject() && !descr->returnType.IsReference() && !descr->returnType.IsObjectHandle() )
  242. {
  243. sh4Args[AS_SH4_MAX_ARGS+1] = (asDWORD) retPointer;
  244. }
  245. asASSERT(descr->parameterTypes.GetLength() <= 32);
  246. // mark all float arguments
  247. int argBit = 1;
  248. int hostFlags = 0;
  249. int intArgs = 0;
  250. for( asUINT a = 0; a < descr->parameterTypes.GetLength(); a++ ) {
  251. if (descr->parameterTypes[a].IsFloatType()) {
  252. hostFlags |= argBit;
  253. } else intArgs++;
  254. argBit <<= 1;
  255. }
  256. asDWORD paramBuffer[64];
  257. if( sysFunc->takesObjByVal )
  258. {
  259. paramSize = 0;
  260. int spos = 0;
  261. int dpos = 1;
  262. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  263. {
  264. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  265. {
  266. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  267. if( descr->parameterTypes[n].GetTypeInfo()->flags & COMPLEX_MASK )
  268. {
  269. paramBuffer[dpos++] = args[spos++];
  270. paramSize++;
  271. }
  272. else
  273. #endif
  274. {
  275. // Copy the object's memory to the buffer
  276. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  277. // Delete the original memory
  278. engine->CallFree(*(char**)(args+spos));
  279. spos++;
  280. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  281. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  282. }
  283. }
  284. else
  285. {
  286. // Copy the value directly
  287. paramBuffer[dpos++] = args[spos++];
  288. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  289. paramBuffer[dpos++] = args[spos++];
  290. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  291. }
  292. }
  293. // Keep a free location at the beginning
  294. args = &paramBuffer[1];
  295. }
  296. switch( callConv )
  297. {
  298. case ICC_CDECL:
  299. case ICC_CDECL_RETURNINMEM:
  300. case ICC_STDCALL:
  301. case ICC_STDCALL_RETURNINMEM:
  302. retQW = CallCDeclFunction(args, paramSize<<2, (asDWORD)func, hostFlags);
  303. break;
  304. case ICC_THISCALL:
  305. case ICC_THISCALL_RETURNINMEM:
  306. retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  307. break;
  308. case ICC_VIRTUAL_THISCALL:
  309. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  310. // Get virtual function table from the object pointer
  311. vftable = *(asDWORD**)obj;
  312. retQW = CallThisCallFunction(obj, args, paramSize<<2, vftable[asDWORD(func)>>2], hostFlags);
  313. break;
  314. case ICC_CDECL_OBJLAST:
  315. case ICC_CDECL_OBJLAST_RETURNINMEM:
  316. retQW = CallThisCallFunction_objLast(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  317. break;
  318. case ICC_CDECL_OBJFIRST:
  319. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  320. retQW = CallThisCallFunction(obj, args, paramSize<<2, (asDWORD)func, hostFlags);
  321. break;
  322. default:
  323. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  324. }
  325. // If the return is a float value we need to get the value from the FP register
  326. if( sysFunc->hostReturnFloat )
  327. {
  328. if( sysFunc->hostReturnSize == 1 )
  329. *(asDWORD*)&retQW = GetReturnedFloat();
  330. else
  331. retQW = GetReturnedDouble();
  332. }
  333. return retQW;
  334. }
  335. END_AS_NAMESPACE
  336. #endif // AS_SH4
  337. #endif // AS_MAX_PORTABILITY