2
0

as_callfunc_arm.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_arm.cpp
  25. //
  26. // These functions handle the actual calling of system functions on the arm platform
  27. //
  28. // Written by Fredrik Ehnbom in June 2009, based on as_callfunc_x86.cpp
  29. // This code has to conform to both AAPCS and the modified ABI for iOS
  30. //
  31. // Reference:
  32. //
  33. // AAPCS: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042d/IHI0042D_aapcs.pdf
  34. // iOS: http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iPhoneOSABIReference/iPhoneOSABIReference.pdf
  35. #include "as_config.h"
  36. #ifndef AS_MAX_PORTABILITY
  37. #ifdef AS_ARM
  38. #include "as_callfunc.h"
  39. #include "as_scriptengine.h"
  40. #include "as_texts.h"
  41. #include "as_tokendef.h"
  42. BEGIN_AS_NAMESPACE
  43. extern "C" asQWORD armFunc(const asDWORD *, int, asFUNCTION_t);
  44. extern "C" asQWORD armFuncR0(const asDWORD *, int, asFUNCTION_t, asDWORD r0);
  45. extern "C" asQWORD armFuncR0R1(const asDWORD *, int, asFUNCTION_t, asDWORD r0, asDWORD r1);
  46. extern "C" asQWORD armFuncObjLast(const asDWORD *, int, asFUNCTION_t, asDWORD obj);
  47. extern "C" asQWORD armFuncR0ObjLast(const asDWORD *, int, asFUNCTION_t, asDWORD r0, asDWORD obj);
  48. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/)
  49. {
  50. asCScriptEngine *engine = context->m_engine;
  51. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  52. int callConv = sysFunc->callConv;
  53. asQWORD retQW = 0;
  54. asFUNCTION_t func = sysFunc->func;
  55. int paramSize = sysFunc->paramSize;
  56. asFUNCTION_t *vftable;
  57. if( sysFunc->hostReturnInMemory )
  58. {
  59. // The return is made in memory
  60. callConv++;
  61. }
  62. asDWORD paramBuffer[64+2];
  63. // Android needs to align 64bit types on even registers, but this isn't done on iOS or Windows Phone
  64. // TODO: optimize runtime: There should be a check for this in PrepareSystemFunction() so this
  65. // doesn't have to be done for functions that don't have any 64bit types
  66. #ifndef AS_ANDROID
  67. if( sysFunc->takesObjByVal )
  68. #endif
  69. {
  70. #ifdef AS_ANDROID
  71. // mask is used as a toggler to skip uneven registers.
  72. int mask = 1;
  73. // Check for object pointer as first argument
  74. switch( callConv )
  75. {
  76. case ICC_THISCALL:
  77. case ICC_CDECL_OBJFIRST:
  78. case ICC_VIRTUAL_THISCALL:
  79. case ICC_THISCALL_RETURNINMEM:
  80. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  81. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  82. mask = 0;
  83. break;
  84. default:
  85. break;
  86. }
  87. // Check for hidden address in case of return by value
  88. if( sysFunc->hostReturnInMemory )
  89. mask = !mask;
  90. #endif
  91. paramSize = 0;
  92. int spos = 0;
  93. int dpos = 2;
  94. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  95. {
  96. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  97. {
  98. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  99. if( descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK )
  100. {
  101. paramBuffer[dpos++] = args[spos++];
  102. paramSize++;
  103. }
  104. else
  105. #endif
  106. {
  107. #ifdef AS_ANDROID
  108. if( (descr->parameterTypes[n].GetObjectType()->flags & asOBJ_APP_CLASS_ALIGN8) &&
  109. ((dpos & 1) == mask) )
  110. {
  111. // 64 bit value align
  112. dpos++;
  113. paramSize++;
  114. }
  115. #endif
  116. // Copy the object's memory to the buffer
  117. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  118. // Delete the original memory
  119. engine->CallFree(*(char**)(args+spos));
  120. spos++;
  121. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  122. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  123. }
  124. }
  125. else
  126. {
  127. #ifdef AS_ANDROID
  128. // Should an alignment be performed?
  129. if( !descr->parameterTypes[n].IsObjectHandle() &&
  130. !descr->parameterTypes[n].IsReference() &&
  131. descr->parameterTypes[n].GetSizeOnStackDWords() == 2 &&
  132. ((dpos & 1) == mask) )
  133. {
  134. // 64 bit value align
  135. dpos++;
  136. paramSize++;
  137. }
  138. #endif
  139. // Copy the value directly
  140. paramBuffer[dpos++] = args[spos++];
  141. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  142. paramBuffer[dpos++] = args[spos++];
  143. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  144. }
  145. }
  146. // Keep a free location at the beginning
  147. args = &paramBuffer[2];
  148. }
  149. switch( callConv )
  150. {
  151. case ICC_CDECL_RETURNINMEM: // fall through
  152. case ICC_STDCALL_RETURNINMEM:
  153. retQW = armFuncR0(args, paramSize<<2, func, (asDWORD)retPointer);
  154. break;
  155. case ICC_CDECL: // fall through
  156. case ICC_STDCALL:
  157. retQW = armFunc(args, paramSize<<2, func);
  158. break;
  159. case ICC_THISCALL: // fall through
  160. case ICC_CDECL_OBJFIRST:
  161. retQW = armFuncR0(args, paramSize<<2, func, (asDWORD)obj);
  162. break;
  163. case ICC_THISCALL_RETURNINMEM:
  164. #ifdef __GNUC__
  165. // On GNUC the address where the return value will be placed should be put in R0
  166. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  167. #else
  168. // On Windows the R0 should always hold the object pointer, and the address for the return value comes after
  169. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)obj, (asDWORD)retPointer);
  170. #endif
  171. break;
  172. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  173. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  174. break;
  175. case ICC_VIRTUAL_THISCALL:
  176. // Get virtual function table from the object pointer
  177. vftable = *(asFUNCTION_t**)obj;
  178. retQW = armFuncR0(args, paramSize<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)obj);
  179. break;
  180. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  181. // Get virtual function table from the object pointer
  182. vftable = *(asFUNCTION_t**)obj;
  183. #ifdef __GNUC__
  184. // On GNUC the address where the return value will be placed should be put in R0
  185. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)retPointer, (asDWORD)obj);
  186. #else
  187. // On Windows the R0 should always hold the object pointer, and the address for the return value comes after
  188. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)obj, (asDWORD)retPointer);
  189. #endif
  190. break;
  191. case ICC_CDECL_OBJLAST:
  192. retQW = armFuncObjLast(args, paramSize<<2, func, (asDWORD)obj);
  193. break;
  194. case ICC_CDECL_OBJLAST_RETURNINMEM:
  195. retQW = armFuncR0ObjLast(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  196. break;
  197. default:
  198. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  199. }
  200. return retQW;
  201. }
  202. END_AS_NAMESPACE
  203. #endif // AS_ARM
  204. #endif // AS_MAX_PORTABILITY