as_callfunc_arm.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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. #include "as_config.h"
  30. #ifndef AS_MAX_PORTABILITY
  31. #ifdef AS_ARM
  32. #include "as_callfunc.h"
  33. #include "as_scriptengine.h"
  34. #include "as_texts.h"
  35. #include "as_tokendef.h"
  36. BEGIN_AS_NAMESPACE
  37. extern "C" asQWORD armFunc(const asDWORD *, int, size_t);
  38. extern "C" asQWORD armFuncR0(const asDWORD *, int, size_t, asDWORD r0);
  39. extern "C" asQWORD armFuncR0R1(const asDWORD *, int, size_t, asDWORD r0, asDWORD r1);
  40. extern "C" asQWORD armFuncObjLast(const asDWORD *, int, size_t, asDWORD obj);
  41. extern "C" asQWORD armFuncR0ObjLast(const asDWORD *, int, size_t, asDWORD r0, asDWORD obj);
  42. asQWORD CallSystemFunctionNative(asCContext *context, asCScriptFunction *descr, void *obj, asDWORD *args, void *retPointer, asQWORD &/*retQW2*/)
  43. {
  44. asCScriptEngine *engine = context->engine;
  45. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  46. int callConv = sysFunc->callConv;
  47. asQWORD retQW = 0;
  48. void *func = (void*)sysFunc->func;
  49. int paramSize = sysFunc->paramSize;
  50. asDWORD *vftable;
  51. if( sysFunc->hostReturnInMemory )
  52. {
  53. // The return is made in memory
  54. callConv++;
  55. }
  56. asDWORD paramBuffer[64];
  57. if( sysFunc->takesObjByVal )
  58. {
  59. paramSize = 0;
  60. int spos = 0;
  61. int dpos = 1;
  62. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  63. {
  64. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  65. {
  66. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  67. if( descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK )
  68. {
  69. paramBuffer[dpos++] = args[spos++];
  70. paramSize++;
  71. }
  72. else
  73. #endif
  74. {
  75. // Copy the object's memory to the buffer
  76. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  77. // Delete the original memory
  78. engine->CallFree(*(char**)(args+spos));
  79. spos++;
  80. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  81. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  82. }
  83. }
  84. else
  85. {
  86. // Copy the value directly
  87. paramBuffer[dpos++] = args[spos++];
  88. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  89. paramBuffer[dpos++] = args[spos++];
  90. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  91. }
  92. }
  93. // Keep a free location at the beginning
  94. args = &paramBuffer[1];
  95. }
  96. context->isCallingSystemFunction = true;
  97. switch( callConv )
  98. {
  99. case ICC_CDECL_RETURNINMEM: // fall through
  100. case ICC_STDCALL_RETURNINMEM:
  101. retQW = armFuncR0(args, paramSize<<2, (asDWORD)func, (asDWORD) retPointer);
  102. break;
  103. case ICC_CDECL: // fall through
  104. case ICC_STDCALL:
  105. retQW = armFunc(args, paramSize<<2, (asDWORD)func);
  106. break;
  107. case ICC_THISCALL: // fall through
  108. case ICC_CDECL_OBJFIRST:
  109. retQW = armFuncR0(args, paramSize<<2, (asDWORD)func, (asDWORD) obj);
  110. break;
  111. case ICC_THISCALL_RETURNINMEM:
  112. #ifndef __GNUC__
  113. retQW = armFuncR0R1(args, paramSize<<2, (asDWORD)func, (asDWORD) obj, (asDWORD) retPointer);
  114. break;
  115. #endif
  116. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  117. retQW = armFuncR0R1(args, paramSize<<2, (asDWORD)func, (asDWORD) retPointer, (asDWORD) obj);
  118. break;
  119. case ICC_VIRTUAL_THISCALL:
  120. // Get virtual function table from the object pointer
  121. vftable = *(asDWORD**)obj;
  122. retQW = armFuncR0(args, paramSize<<2, vftable[asDWORD(func)>>2], (asDWORD) obj);
  123. break;
  124. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  125. // Get virtual function table from the object pointer
  126. vftable = *(asDWORD**)obj;
  127. #ifndef __GNUC__
  128. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[asDWORD(func)>>2], (asDWORD) retPointer, (asDWORD) obj);
  129. #else
  130. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[asDWORD(func)>>2], (asDWORD) obj, (asDWORD) retPointer);
  131. #endif
  132. break;
  133. case ICC_CDECL_OBJLAST:
  134. retQW = armFuncObjLast(args, paramSize<<2, (asDWORD)func, (asDWORD) obj);
  135. break;
  136. case ICC_CDECL_OBJLAST_RETURNINMEM:
  137. retQW = armFuncR0ObjLast(args, paramSize<<2, (asDWORD)func, (asDWORD) retPointer, (asDWORD) obj);
  138. break;
  139. default:
  140. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  141. }
  142. context->isCallingSystemFunction = false;
  143. return retQW;
  144. }
  145. END_AS_NAMESPACE
  146. #endif // AS_ARM
  147. #endif // AS_MAX_PORTABILITY