BackEndAPI.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #pragma once
  6. #if DYNAMIC_INTERPRETER_THUNK
  7. #define DefaultEntryThunk Js::InterpreterStackFrame::DelayDynamicInterpreterThunk
  8. #if _M_X64
  9. #define AsmJsDefaultEntryThunk Js::InterpreterStackFrame::AsmJsDelayDynamicInterpreterThunk
  10. #elif _M_IX86
  11. #define AsmJsDefaultEntryThunk Js::InterpreterStackFrame::DelayDynamicInterpreterThunk
  12. #endif
  13. #else
  14. #define DefaultEntryThunk Js::InterpreterStackFrame::InterpreterThunk
  15. #endif
  16. #define ProfileEntryThunk Js::ScriptContext::DebugProfileProbeThunk
  17. #define DefaultDeferredParsingThunk Js::JavascriptFunction::DeferredParsingThunk
  18. #define ProfileDeferredParsingThunk Js::ScriptContext::ProfileModeDeferredParsingThunk
  19. #define DefaultDeferredDeserializeThunk Js::JavascriptFunction::DeferredDeserializeThunk
  20. #define ProfileDeferredDeserializeThunk Js::ScriptContext::ProfileModeDeferredDeserializeThunk
  21. #if ENABLE_NATIVE_CODEGEN
  22. class NativeCodeGenerator;
  23. class ThreadContext;
  24. struct CodeGenWorkItem;
  25. class NativeCodeData;
  26. class StackSym;
  27. class Func;
  28. struct InlinedFrameLayout;
  29. typedef intptr IntConstType;
  30. typedef uintptr UIntConstType;
  31. typedef IntMath<intptr>::Type IntConstMath;
  32. typedef double FloatConstType;
  33. #include "..\Backend\EmitBuffer.h"
  34. #include "..\Backend\InterpreterThunkEmitter.h"
  35. #include "..\Runtime\Bytecode\BackEndOpCodeAttr.h"
  36. #include "..\Backend\BackEndOpCodeAttrAsmJs.h"
  37. #include "..\Backend\CodeGenNumberAllocator.h"
  38. #include "..\Backend\NativeCodeData.h"
  39. #include "..\Backend\JnHelperMethod.h"
  40. #include "..\Backend\IRType.h"
  41. #include "..\Backend\InlineeFrameInfo.h"
  42. NativeCodeGenerator * NewNativeCodeGenerator(Js::ScriptContext * nativeCodeGen);
  43. void DeleteNativeCodeGenerator(NativeCodeGenerator * nativeCodeGen);
  44. void CloseNativeCodeGenerator(NativeCodeGenerator* nativeCodeGen);
  45. bool IsClosedNativeCodeGenerator(NativeCodeGenerator* nativeCodeGen);
  46. void SetProfileModeNativeCodeGen(NativeCodeGenerator *pNativeCodeGen, BOOL fSet);
  47. void UpdateNativeCodeGeneratorForDebugMode(NativeCodeGenerator* nativeCodeGen);
  48. CriticalSection *GetNativeCodeGenCriticalSection(NativeCodeGenerator *pNativeCodeGen);
  49. bool TryReleaseNonHiPriWorkItem(Js::ScriptContext* scriptContext, CodeGenWorkItem* workItem);
  50. void NativeCodeGenEnterScriptStart(NativeCodeGenerator * nativeCodeGen);
  51. bool IsNativeFunctionAddr(Js::ScriptContext *scriptContext, void * address);
  52. void FreeNativeCodeGenAllocation(Js::ScriptContext* scriptContext, void* address);
  53. CodeGenAllocators* GetForegroundAllocator(NativeCodeGenerator * nativeCodeGen, PageAllocator* pageallocator);
  54. void GenerateFunction(NativeCodeGenerator * nativeCodeGen, Js::FunctionBody * functionBody, Js::ScriptFunction * function = NULL);
  55. void GenerateLoopBody(NativeCodeGenerator * nativeCodeGen, Js::FunctionBody * functionBody, Js::LoopHeader * loopHeader, Js::EntryPointInfo* entryPointInfo, uint localCount, Js::Var localSlots[]);
  56. #ifdef ENABLE_PREJIT
  57. void GenerateAllFunctions(NativeCodeGenerator * nativeCodeGen, Js::FunctionBody * fn);
  58. #endif
  59. #ifdef IR_VIEWER
  60. Js::Var RejitIRViewerFunction(NativeCodeGenerator *nativeCodeGen, Js::FunctionBody *fn, Js::ScriptContext *scriptContext);
  61. #endif
  62. BOOL IsIntermediateCodeGenThunk(Js::JavascriptMethod codeAddress);
  63. BOOL IsAsmJsCodeGenThunk(Js::JavascriptMethod codeAddress);
  64. typedef Js::JavascriptMethod (*CheckCodeGenFunction)(Js::ScriptFunction * function);
  65. CheckCodeGenFunction GetCheckCodeGenFunction(Js::JavascriptMethod codeAddress);
  66. uint GetBailOutRegisterSaveSlotCount();
  67. uint GetBailOutReserveSlotCount();
  68. #if DBG
  69. void CheckIsExecutable(Js::RecyclableObject * function, Js::JavascriptMethod entryPoint);
  70. #endif
  71. #ifdef PROFILE_EXEC
  72. namespace Js
  73. {
  74. class ScriptContextProfiler;
  75. };
  76. void CreateProfilerNativeCodeGen(NativeCodeGenerator * nativeCodeGen, Js::ScriptContextProfiler * profiler);
  77. void ProfilePrintNativeCodeGen(NativeCodeGenerator * nativeCodeGen);
  78. void SetProfilerFromNativeCodeGen(NativeCodeGenerator * toNativeCodeGen, NativeCodeGenerator * fromNativeCodeGen);
  79. #endif
  80. void DeleteNativeCodeData(NativeCodeData * data);
  81. #else
  82. inline BOOL IsIntermediateCodeGenThunk(Js::JavascriptMethod codeAddress) { return false; }
  83. inline BOOL IsAsmJsCodeGenThunk(Js::JavascriptMethod codeAddress) { return false; }
  84. #endif
  85. #if _M_X64
  86. extern "C" void * amd64_ReturnFromCallWithFakeFrame();
  87. #endif
  88. struct InlinedFrameLayout
  89. {
  90. Js::InlineeCallInfo callInfo;
  91. Js::JavascriptFunction *function;
  92. Js::Var arguments; // The arguments object.
  93. //Js::Var argv[0]; // Here it would be embedded arguments array (callInfo.count elements)
  94. // but can't have 0-size arr in base class, so we define it in derived class.
  95. Js::Var* GetArguments()
  96. {
  97. return (Js::Var*)(this + 1);
  98. }
  99. template<class Fn>
  100. void MapArgs(Fn callback)
  101. {
  102. Js::Var* arguments = this->GetArguments();
  103. for (uint i = 0; i < callInfo.Count; i++)
  104. {
  105. callback(i, (Js::Var*)((uintptr_t*)arguments + i));
  106. }
  107. }
  108. InlinedFrameLayout* Next()
  109. {
  110. InlinedFrameLayout *next = (InlinedFrameLayout *)(this->GetArguments() + this->callInfo.Count);
  111. return next;
  112. }
  113. };
  114. class BailOutRecord;
  115. struct LazyBailOutRecord
  116. {
  117. uint32 offset;
  118. BYTE* instructionPointer; // Instruction pointer of the bailout code
  119. BailOutRecord* bailoutRecord;
  120. LazyBailOutRecord() : offset(0), instructionPointer(nullptr), bailoutRecord(nullptr) {}
  121. LazyBailOutRecord(uint32 offset, BYTE* address, BailOutRecord* record) :
  122. offset(offset), instructionPointer(address),
  123. bailoutRecord(record)
  124. {}
  125. void SetBailOutKind();
  126. #if DBG
  127. void Dump(Js::FunctionBody* functionBody);
  128. #endif
  129. };
  130. struct StackFrameConstants
  131. {
  132. #if defined(_M_IX86)
  133. static const size_t StackCheckCodeHeightThreadBound = 35;
  134. static const size_t StackCheckCodeHeightNotThreadBound = 47;
  135. static const size_t StackCheckCodeHeightWithInterruptProbe = 53;
  136. #elif defined(_M_X64)
  137. static const size_t StackCheckCodeHeightThreadBound = 57;
  138. static const size_t StackCheckCodeHeightNotThreadBound = 62;
  139. static const size_t StackCheckCodeHeightWithInterruptProbe = 68;
  140. #elif defined(_M_ARM)
  141. static const size_t StackCheckCodeHeight = 64;
  142. static const size_t StackCheckCodeHeightThreadBound = StackFrameConstants::StackCheckCodeHeight;
  143. static const size_t StackCheckCodeHeightNotThreadBound = StackFrameConstants::StackCheckCodeHeight;
  144. static const size_t StackCheckCodeHeightWithInterruptProbe = StackFrameConstants::StackCheckCodeHeight;
  145. #elif defined(_M_ARM64)
  146. static const size_t StackCheckCodeHeight = 58*2;
  147. static const size_t StackCheckCodeHeightThreadBound = StackFrameConstants::StackCheckCodeHeight;
  148. static const size_t StackCheckCodeHeightNotThreadBound = StackFrameConstants::StackCheckCodeHeight;
  149. static const size_t StackCheckCodeHeightWithInterruptProbe = StackFrameConstants::StackCheckCodeHeight;
  150. #endif
  151. };
  152. struct NativeResourceIds
  153. {
  154. static const short SourceCodeResourceNameId = 0x64;
  155. static const short ByteCodeResourceNameId = 0x65;
  156. static const short NativeMapResourceNameId = 0x66;
  157. static const short NativeThrowMapResourceNameId = 0x67;
  158. };
  159. #if defined(_M_IX86)
  160. struct ThunkConstants
  161. {
  162. static const size_t ThunkInstructionSize = 2;
  163. static const size_t ThunkSize = 6;
  164. };
  165. #endif
  166. enum LibraryValue {
  167. ValueInvalid,
  168. ValueUndeclBlockVar,
  169. ValueEmptyString,
  170. ValueUndefined,
  171. ValueNull,
  172. ValueTrue,
  173. ValueFalse,
  174. ValueNegativeZero,
  175. ValueNumberTypeStatic,
  176. ValueStringTypeStatic,
  177. ValueObjectType,
  178. ValueObjectHeaderInlinedType,
  179. ValueRegexType,
  180. ValueArrayConstructor,
  181. ValuePositiveInfinity,
  182. ValueNaN,
  183. ValueJavascriptArrayType,
  184. ValueNativeIntArrayType,
  185. ValueNativeFloatArrayType,
  186. ValueConstructorCacheDefaultInstance,
  187. ValueAbsDoubleCst,
  188. ValueUintConvertConst,
  189. ValueBuiltinFunctions,
  190. ValueJnHelperMethods,
  191. ValueCharStringCache
  192. };
  193. enum VTableValue {
  194. #if !_M_X64
  195. VtableJavascriptNumber,
  196. #endif
  197. VtableDynamicObject,
  198. VtableInvalid,
  199. VtablePropertyString,
  200. VtableJavascriptBoolean,
  201. VtableSmallDynamicObjectSnapshotEnumeratorWPCache,
  202. VtableJavascriptArray,
  203. VtableInt8Array,
  204. VtableUint8Array,
  205. VtableUint8ClampedArray,
  206. VtableInt16Array,
  207. VtableUint16Array,
  208. VtableInt32Array,
  209. VtableUint32Array,
  210. VtableFloat32Array,
  211. VtableFloat64Array,
  212. VtableJavascriptPixelArray,
  213. VtableInt64Array,
  214. VtableUint64Array,
  215. VtableBoolArray,
  216. VtableCharArray,
  217. VtableInt8VirtualArray,
  218. VtableUint8VirtualArray,
  219. VtableUint8ClampedVirtualArray,
  220. VtableInt16VirtualArray,
  221. VtableUint16VirtualArray,
  222. VtableInt32VirtualArray,
  223. VtableUint32VirtualArray,
  224. VtableFloat32VirtualArray,
  225. VtableFloat64VirtualArray,
  226. VtableNativeIntArray,
  227. #if ENABLE_COPYONACCESS_ARRAY
  228. VtableCopyOnAccessNativeIntArray,
  229. #endif
  230. VtableNativeFloatArray,
  231. VtableJavascriptNativeIntArray,
  232. VtableJavascriptRegExp,
  233. VtableStackScriptFunction,
  234. VtableConcatStringMulti,
  235. VtableCompoundString,
  236. // SIMD_JS
  237. VtableSimd128F4,
  238. VtableSimd128I4,
  239. Count
  240. };
  241. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  242. const wchar_t *GetVtableName(VTableValue value);
  243. #endif
  244. enum AuxArrayValue {
  245. AuxPropertyIdArray,
  246. AuxIntArray,
  247. AuxFloatArray,
  248. AuxVarsArray,
  249. AuxVarArrayVarCount,
  250. AuxFuncInfoArray
  251. };
  252. enum OptimizationOverridesValue {
  253. OptimizationOverridesArraySetElementFastPathVtable,
  254. OptimizationOverridesIntArraySetElementFastPathVtable,
  255. OptimizationOverridesFloatArraySetElementFastPathVtable,
  256. OptimizationOverridesSideEffects
  257. };
  258. enum FunctionBodyValue {
  259. FunctionBodyConstantVar,
  260. FunctionBodyNestedFuncReference,
  261. FunctionBodyReferencedPropertyId,
  262. FunctionBodyPropertyIdFromCacheId,
  263. FunctionBodyLiteralRegex,
  264. FunctionBodyStringTemplateCallsiteRef
  265. };
  266. enum ScriptContextValue {
  267. ScriptContextNumberAllocator,
  268. ScriptContextCharStringCache,
  269. ScriptContextRecycler,
  270. ScriptContextOptimizationOverrides
  271. };
  272. enum NumberAllocatorValue {
  273. NumberAllocatorEndAddress,
  274. NumberAllocatorFreeObjectList
  275. };