/xbmc/visualizations/Vortex/angelscript/angelscript/source/as_callfunc_arm.cpp

http://github.com/xbmc/xbmc · C++ · 344 lines · 233 code · 36 blank · 75 comment · 50 complexity · 9a1568b6b283860a160adf76109446d5 MD5 · raw file

  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 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_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. // TODO: CallSystemFunction should be split in two layers. The top layer
  43. // implements the parts that are common to all system functions,
  44. // e.g. the check for generic calling convention, the extraction of the
  45. // object pointer, the processing of auto handles, and the pop size.
  46. // Remember that the proper handling of auto handles is implemented in
  47. // as_callfunc_x64_gcc.cpp as that code can handle both 32bit and 64bit pointers.
  48. //
  49. // The lower layer implemented in CallNativeSystemFunction will then
  50. // be responsible for just transforming the parameters to the native
  51. // calling convention.
  52. //
  53. // This should be done for all supported platforms.
  54. int CallSystemFunction(int id, asCContext *context, void *objectPointer)
  55. {
  56. asCScriptEngine *engine = context->engine;
  57. asCScriptFunction *descr = engine->scriptFunctions[id];
  58. asSSystemFunctionInterface *sysFunc = descr->sysFuncIntf;
  59. int callConv = sysFunc->callConv;
  60. if( callConv == ICC_GENERIC_FUNC || callConv == ICC_GENERIC_METHOD )
  61. return context->CallGeneric(id, objectPointer);
  62. asQWORD retQW = 0;
  63. void *func = (void*)sysFunc->func;
  64. int paramSize = sysFunc->paramSize;
  65. asDWORD *args = context->regs.stackPointer;
  66. void *retPointer = 0;
  67. void *obj = 0;
  68. asDWORD *vftable;
  69. int popSize = paramSize;
  70. context->regs.objectType = descr->returnType.GetObjectType();
  71. if( descr->returnType.IsObject() && !descr->returnType.IsReference() && !descr->returnType.IsObjectHandle() )
  72. {
  73. // Allocate the memory for the object
  74. retPointer = engine->CallAlloc(descr->returnType.GetObjectType());
  75. if( sysFunc->hostReturnInMemory )
  76. {
  77. // The return is made in memory
  78. callConv++;
  79. }
  80. }
  81. if( callConv >= ICC_THISCALL )
  82. {
  83. if( objectPointer )
  84. {
  85. obj = objectPointer;
  86. }
  87. else
  88. {
  89. // The object pointer should be popped from the context stack
  90. popSize++;
  91. // Check for null pointer
  92. obj = (void*)*(size_t*)(args);
  93. if( obj == 0 )
  94. {
  95. context->SetInternalException(TXT_NULL_POINTER_ACCESS);
  96. if( retPointer )
  97. engine->CallFree(retPointer);
  98. return 0;
  99. }
  100. // Add the base offset for multiple inheritance
  101. #ifdef __GNUC__
  102. // On GNUC + ARM the lsb of the offset is used to indicate a virtual function
  103. // and the whole offset is thus shifted one bit left to keep the original
  104. // offset resolution
  105. obj = (void*)(size_t(obj) + (sysFunc->baseOffset>>1));
  106. #else
  107. obj = (void*)(size_t(obj) + sysFunc->baseOffset);
  108. #endif
  109. // Skip the object pointer
  110. args++;
  111. }
  112. }
  113. asDWORD paramBuffer[64];
  114. if( sysFunc->takesObjByVal )
  115. {
  116. paramSize = 0;
  117. int spos = 0;
  118. int dpos = 1;
  119. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  120. {
  121. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  122. {
  123. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  124. if( descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK )
  125. {
  126. paramBuffer[dpos++] = args[spos++];
  127. paramSize++;
  128. }
  129. else
  130. #endif
  131. {
  132. // Copy the object's memory to the buffer
  133. memcpy(&paramBuffer[dpos], *(void**)(args+spos), descr->parameterTypes[n].GetSizeInMemoryBytes());
  134. // Delete the original memory
  135. engine->CallFree(*(char**)(args+spos));
  136. spos++;
  137. dpos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  138. paramSize += descr->parameterTypes[n].GetSizeInMemoryDWords();
  139. }
  140. }
  141. else
  142. {
  143. // Copy the value directly
  144. paramBuffer[dpos++] = args[spos++];
  145. if( descr->parameterTypes[n].GetSizeOnStackDWords() > 1 )
  146. paramBuffer[dpos++] = args[spos++];
  147. paramSize += descr->parameterTypes[n].GetSizeOnStackDWords();
  148. }
  149. }
  150. // Keep a free location at the beginning
  151. args = &paramBuffer[1];
  152. }
  153. context->isCallingSystemFunction = true;
  154. switch( callConv )
  155. {
  156. case ICC_CDECL_RETURNINMEM: // fall through
  157. case ICC_STDCALL_RETURNINMEM:
  158. retQW = armFuncR0(args, paramSize<<2, (asDWORD)func, (asDWORD) retPointer);
  159. break;
  160. case ICC_CDECL: // fall through
  161. case ICC_STDCALL:
  162. retQW = armFunc(args, paramSize<<2, (asDWORD)func);
  163. break;
  164. case ICC_THISCALL: // fall through
  165. case ICC_CDECL_OBJFIRST:
  166. retQW = armFuncR0(args, paramSize<<2, (asDWORD)func, (asDWORD) obj);
  167. break;
  168. case ICC_THISCALL_RETURNINMEM:
  169. #ifndef __GNUC__
  170. retQW = armFuncR0R1(args, paramSize<<2, (asDWORD)func, (asDWORD) obj, (asDWORD) retPointer);
  171. break;
  172. #endif
  173. case ICC_CDECL_OBJFIRST_RETURNINMEM:
  174. retQW = armFuncR0R1(args, paramSize<<2, (asDWORD)func, (asDWORD) retPointer, (asDWORD) obj);
  175. break;
  176. case ICC_VIRTUAL_THISCALL:
  177. // Get virtual function table from the object pointer
  178. vftable = *(asDWORD**)obj;
  179. retQW = armFuncR0(args, paramSize<<2, vftable[asDWORD(func)>>2], (asDWORD) obj);
  180. break;
  181. case ICC_VIRTUAL_THISCALL_RETURNINMEM:
  182. // Get virtual function table from the object pointer
  183. vftable = *(asDWORD**)obj;
  184. #ifndef __GNUC__
  185. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[asDWORD(func)>>2], (asDWORD) retPointer, (asDWORD) obj);
  186. #else
  187. retQW = armFuncR0R1(args, (paramSize+1)<<2, vftable[asDWORD(func)>>2], (asDWORD) obj, (asDWORD) retPointer);
  188. #endif
  189. break;
  190. case ICC_CDECL_OBJLAST:
  191. retQW = armFuncObjLast(args, paramSize<<2, (asDWORD)func, (asDWORD) obj);
  192. break;
  193. case ICC_CDECL_OBJLAST_RETURNINMEM:
  194. retQW = armFuncR0ObjLast(args, paramSize<<2, (asDWORD)func, (asDWORD) retPointer, (asDWORD) obj);
  195. break;
  196. default:
  197. context->SetInternalException(TXT_INVALID_CALLING_CONVENTION);
  198. }
  199. context->isCallingSystemFunction = false;
  200. #ifdef COMPLEX_OBJS_PASSED_BY_REF
  201. if( sysFunc->takesObjByVal )
  202. {
  203. // Need to free the complex objects passed by value
  204. args = context->regs.stackPointer;
  205. if( callConv >= ICC_THISCALL && !objectPointer )
  206. args++;
  207. int spos = 0;
  208. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  209. {
  210. if( descr->parameterTypes[n].IsObject() &&
  211. !descr->parameterTypes[n].IsReference() &&
  212. !descr->parameterTypes[n].IsObjectHandle()
  213. )
  214. {
  215. if (descr->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK)
  216. {
  217. void *obj = (void*)args[spos++];
  218. // Running the destructor here results in the destructor being
  219. // run twice as it has already been called in the native function.
  220. // TODO: Should this be an ifdef or removed completely?
  221. // asSTypeBehaviour *beh = &descr->parameterTypes[n].GetObjectType()->beh;
  222. // if( beh->destruct )
  223. // engine->CallObjectMethod(obj, beh->destruct);
  224. engine->CallFree(obj);
  225. }
  226. else
  227. {
  228. // The original data was already freed earlier
  229. spos += descr->parameterTypes[n].GetSizeInMemoryDWords();
  230. }
  231. }
  232. else
  233. {
  234. spos += descr->parameterTypes[n].GetSizeOnStackDWords();
  235. }
  236. }
  237. }
  238. #endif
  239. // Store the returned value in our stack
  240. if( descr->returnType.IsObject() && !descr->returnType.IsReference() )
  241. {
  242. if( descr->returnType.IsObjectHandle() )
  243. {
  244. context->regs.objectRegister = (void*)(size_t)retQW;
  245. if( sysFunc->returnAutoHandle && context->regs.objectRegister )
  246. engine->CallObjectMethod(context->regs.objectRegister, descr->returnType.GetObjectType()->beh.addref);
  247. }
  248. else
  249. {
  250. if( !sysFunc->hostReturnInMemory )
  251. {
  252. // Copy the returned value to the pointer sent by the script engine
  253. if( sysFunc->hostReturnSize == 1 )
  254. *(asDWORD*)retPointer = (asDWORD)retQW;
  255. else
  256. *(asQWORD*)retPointer = retQW;
  257. }
  258. // Store the object in the register
  259. context->regs.objectRegister = retPointer;
  260. }
  261. }
  262. else
  263. {
  264. // Store value in valueRegister
  265. if( sysFunc->hostReturnFloat )
  266. {
  267. if( sysFunc->hostReturnSize == 1 )
  268. *(asDWORD*)&context->regs.valueRegister = (asDWORD) retQW;
  269. else
  270. context->regs.valueRegister = retQW;
  271. }
  272. else if( sysFunc->hostReturnSize == 1 )
  273. *(asDWORD*)&context->regs.valueRegister = (asDWORD)retQW;
  274. else
  275. context->regs.valueRegister = retQW;
  276. }
  277. if( sysFunc->hasAutoHandles )
  278. {
  279. args = context->regs.stackPointer;
  280. if( callConv >= ICC_THISCALL && !objectPointer )
  281. args++;
  282. int spos = 0;
  283. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  284. {
  285. if( sysFunc->paramAutoHandles[n] && args[spos] )
  286. {
  287. // Call the release method on the type
  288. engine->CallObjectMethod((void*)*(size_t*)&args[spos], descr->parameterTypes[n].GetObjectType()->beh.release);
  289. args[spos] = 0;
  290. }
  291. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() && !descr->parameterTypes[n].IsReference() )
  292. spos++;
  293. else
  294. spos += descr->parameterTypes[n].GetSizeOnStackDWords();
  295. }
  296. }
  297. return popSize;
  298. }
  299. END_AS_NAMESPACE
  300. #endif // AS_ARM
  301. #endif // AS_MAX_PORTABILITY