BackendApi.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "Backend.h"
  6. NativeCodeGenerator *
  7. NewNativeCodeGenerator(Js::ScriptContext * scriptContext)
  8. {
  9. return HeapNew(NativeCodeGenerator, scriptContext);
  10. }
  11. void
  12. DeleteNativeCodeGenerator(NativeCodeGenerator * nativeCodeGen)
  13. {
  14. HeapDelete(nativeCodeGen);
  15. }
  16. void
  17. CloseNativeCodeGenerator(NativeCodeGenerator * nativeCodeGen)
  18. {
  19. nativeCodeGen->Close();
  20. }
  21. bool
  22. IsClosedNativeCodeGenerator(NativeCodeGenerator * nativeCodeGen)
  23. {
  24. return nativeCodeGen->IsClosed();
  25. }
  26. void SetProfileModeNativeCodeGen(NativeCodeGenerator *pNativeCodeGen, BOOL fSet)
  27. {
  28. pNativeCodeGen->SetProfileMode(fSet);
  29. }
  30. void UpdateNativeCodeGeneratorForDebugMode(NativeCodeGenerator* nativeCodeGen)
  31. {
  32. nativeCodeGen->UpdateQueueForDebugMode();
  33. }
  34. CriticalSection *GetNativeCodeGenCriticalSection(NativeCodeGenerator *pNativeCodeGen)
  35. {
  36. return pNativeCodeGen->Processor()->GetCriticalSection();
  37. }
  38. ///----------------------------------------------------------------------------
  39. ///
  40. /// GenerateFunction
  41. ///
  42. /// This is the main entry point for the runtime to call the native code
  43. /// generator for js function.
  44. ///
  45. ///----------------------------------------------------------------------------
  46. void
  47. GenerateFunction(NativeCodeGenerator * nativeCodeGen, Js::FunctionBody * fn, Js::ScriptFunction * function)
  48. {
  49. nativeCodeGen->GenerateFunction(fn, function);
  50. }
  51. InProcCodeGenAllocators* GetForegroundAllocator(NativeCodeGenerator * nativeCodeGen, PageAllocator* pageallocator)
  52. {
  53. return nativeCodeGen->GetCodeGenAllocator(pageallocator);
  54. }
  55. #ifdef ENABLE_PREJIT
  56. void
  57. GenerateAllFunctions(NativeCodeGenerator * nativeCodeGen, Js::FunctionBody *fn)
  58. {
  59. nativeCodeGen->GenerateAllFunctions(fn);
  60. }
  61. #endif
  62. #ifdef IR_VIEWER
  63. Js::Var
  64. RejitIRViewerFunction(NativeCodeGenerator *nativeCodeGen, Js::FunctionBody *fn, Js::ScriptContext *scriptContext)
  65. {
  66. return nativeCodeGen->RejitIRViewerFunction(fn, scriptContext);
  67. }
  68. #endif
  69. void
  70. GenerateLoopBody(NativeCodeGenerator *nativeCodeGen, Js::FunctionBody *fn, Js::LoopHeader * loopHeader, Js::EntryPointInfo* info, uint localCount, Js::Var localSlots[])
  71. {
  72. nativeCodeGen->GenerateLoopBody(fn, loopHeader, info, localCount, localSlots);
  73. }
  74. void
  75. NativeCodeGenEnterScriptStart(NativeCodeGenerator * nativeCodeGen)
  76. {
  77. if (nativeCodeGen)
  78. {
  79. nativeCodeGen->EnterScriptStart();
  80. }
  81. }
  82. BOOL IsIntermediateCodeGenThunk(Js::JavascriptMethod codeAddress)
  83. {
  84. return NativeCodeGenerator::IsThunk(codeAddress);
  85. }
  86. BOOL IsAsmJsCodeGenThunk(Js::JavascriptMethod codeAddress)
  87. {
  88. return NativeCodeGenerator::IsAsmJsCodeGenThunk(codeAddress);
  89. }
  90. CheckCodeGenFunction GetCheckCodeGenFunction(Js::JavascriptMethod codeAddress)
  91. {
  92. return NativeCodeGenerator::GetCheckCodeGenFunction(codeAddress);
  93. }
  94. Js::JavascriptMethod GetCheckCodeGenThunk()
  95. {
  96. return NativeCodeGenerator::CheckCodeGenThunk;
  97. }
  98. #ifdef ASMJS_PLAT
  99. Js::JavascriptMethod GetCheckAsmJsCodeGenThunk()
  100. {
  101. return NativeCodeGenerator::CheckAsmJsCodeGenThunk;
  102. }
  103. #endif
  104. uint GetBailOutRegisterSaveSlotCount()
  105. {
  106. // REVIEW: not all registers are used, we are allocating more space then necessary.
  107. return LinearScanMD::GetRegisterSaveSlotCount();
  108. }
  109. uint
  110. GetBailOutReserveSlotCount()
  111. {
  112. return 1; //For arguments id
  113. }
  114. #if DBG
  115. void CheckIsExecutable(Js::RecyclableObject * function, Js::JavascriptMethod entrypoint)
  116. {
  117. Js::ScriptContext * scriptContext = function->GetScriptContext();
  118. // it's easy to call the default entry point from RecyclableObject.
  119. AssertMsg((Js::JavascriptFunction::Is(function) && Js::JavascriptFunction::FromVar(function)->IsExternalFunction())
  120. || Js::CrossSite::IsThunk(entrypoint) || !scriptContext->IsActuallyClosed() ||
  121. (scriptContext->GetThreadContext()->IsScriptActive() && !Js::JavascriptConversion::IsCallable(function)),
  122. "Can't call function when the script context is closed");
  123. if (scriptContext->GetThreadContext()->IsScriptActive())
  124. {
  125. return;
  126. }
  127. if (function->IsExternal())
  128. {
  129. return;
  130. }
  131. if (Js::JavascriptOperators::GetTypeId(function) == Js::TypeIds_HostDispatch)
  132. {
  133. AssertMsg(false, "Has to go through CallRootFunction to start calling Javascript function");
  134. }
  135. else if (Js::JavascriptFunction::Is(function))
  136. {
  137. if (((Js::JavascriptFunction*)function)->IsExternalFunction())
  138. {
  139. return;
  140. }
  141. else if (((Js::JavascriptFunction*)function)->IsWinRTFunction())
  142. {
  143. return;
  144. }
  145. else
  146. {
  147. AssertMsg(false, "Has to go through CallRootFunction to start calling Javascript function");
  148. }
  149. }
  150. else
  151. {
  152. AssertMsg(false, "Has to go through CallRootFunction to start calling Javascript function");
  153. }
  154. }
  155. #endif
  156. #ifdef PROFILE_EXEC
  157. void
  158. CreateProfilerNativeCodeGen(NativeCodeGenerator * nativeCodeGen, Js::ScriptContextProfiler * profiler)
  159. {
  160. nativeCodeGen->CreateProfiler(profiler);
  161. }
  162. void
  163. ProfilePrintNativeCodeGen(NativeCodeGenerator * nativeCodeGen)
  164. {
  165. nativeCodeGen->ProfilePrint();
  166. }
  167. void
  168. SetProfilerFromNativeCodeGen(NativeCodeGenerator * toNativeCodeGen, NativeCodeGenerator * fromNativeCodeGen)
  169. {
  170. toNativeCodeGen->SetProfilerFromNativeCodeGen(fromNativeCodeGen);
  171. }
  172. #endif
  173. void DeleteNativeCodeData(NativeCodeData * data)
  174. {
  175. if (data)
  176. {
  177. HeapDelete(data);
  178. }
  179. }