as_callfunc_arm.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 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];
  63. if( sysFunc->takesObjByVal )
  64. {
  65. paramSize = 0;
  66. int spos = 0;
  67. int dpos = 1;
  68. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  69. {
  70. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  71. {
  72. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  73. if( descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK )
  74. {
  75. paramBuffer[dpos++] = args[spos++];
  76. paramSize++;
  77. }
  78. else
  79. #endif
  80. {
  81. // Copy the object's memory to the buffer
  82. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  83. // Delete the original memory
  84. engine->CallFree(*(char**)(args+spos));
  85. spos++;
  86. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  87. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  88. }
  89. }
  90. else
  91. {
  92. // Copy the value directly
  93. paramBuffer[dpos++] = args[spos++];
  94. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  95. paramBuffer[dpos++] = args[spos++];
  96. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  97. }
  98. }
  99. // Keep a free location at the beginning
  100. args = &paramBuffer[1];
  101. }
  102. switch( callConv )
  103. {
  104. case ICC_CDECL_RETURNINMEM: // fall through
  105. case ICC_STDCALL_RETURNINMEM:
  106. retQW = armFuncR0(args, paramSize<<2, func, (asDWORD)retPointer);
  107. break;
  108. case ICC_CDECL: // fall through
  109. case ICC_STDCALL:
  110. retQW = armFunc(args, paramSize<<2, func);
  111. break;
  112. case ICC_THISCALL: // fall through
  113. case ICC_CDECL_OBJFIRST:
  114. retQW = armFuncR0(args, paramSize<<2, func, (asDWORD)obj);
  115. break;
  116. case ICC_THISCALL_RETURNINMEM:
  117. #ifndef __GNUC__
  118. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)obj, (asDWORD)retPointer);
  119. break;
  120. #endif
  121. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  122. retQW = armFuncR0R1(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  123. break;
  124. case ICC_VIRTUAL_THISCALL:
  125. // Get virtual function table from the object pointer
  126. vftable = *(asFUNCTION_t**)obj;
  127. retQW = armFuncR0(args, paramSize<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)obj);
  128. break;
  129. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  130. // Get virtual function table from the object pointer
  131. vftable = *(asFUNCTION_t**)obj;
  132. #ifndef __GNUC__
  133. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)retPointer, (asDWORD)obj);
  134. #else
  135. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[FuncPtrToUInt(func)>>2], (asDWORD)obj, (asDWORD)retPointer);
  136. #endif
  137. break;
  138. case ICC_CDECL_OBJLAST:
  139. retQW = armFuncObjLast(args, paramSize<<2, func, (asDWORD)obj);
  140. break;
  141. case ICC_CDECL_OBJLAST_RETURNINMEM:
  142. retQW = armFuncR0ObjLast(args, paramSize<<2, func, (asDWORD)retPointer, (asDWORD)obj);
  143. break;
  144. default:
  145. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  146. }
  147. return retQW;
  148. }
  149. END_AS_NAMESPACE
  150. #endif // AS_ARM
  151. #endif // AS_MAX_PORTABILITY