2
0

WAsmjsUtils.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 defined(ASMJS_PLAT) || defined(ENABLE_WASM)
  7. namespace WAsmJs
  8. {
  9. static const int DOUBLE_SLOTS_SPACE = (sizeof(double) / sizeof(Js::Var)); // 2 in x86 and 1 in x64
  10. static const double FLOAT_SLOTS_SPACE = (sizeof(float) / (double)sizeof(Js::Var)); // 1 in x86 and 0.5 in x64
  11. static const double INT_SLOTS_SPACE = (sizeof(int) / (double)sizeof(Js::Var)); // 1 in x86 and 0.5 in x64
  12. static const double SIMD_SLOTS_SPACE = (sizeof(SIMDValue) / sizeof(Js::Var)); // 4 in x86 and 2 in x64
  13. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  14. void TraceAsmJsArgsIn(Js::Var function, int n, ...);
  15. #endif
  16. typedef Js::RegSlot RegSlot;
  17. uint32 ConvertOffset(uint32 ptr, uint32 fromSize, uint32 toSize);
  18. template<typename ToType> uint32 ConvertOffset(uint32 ptr, uint32 fromSize)
  19. {
  20. return ConvertOffset(ptr, fromSize, sizeof(ToType));
  21. }
  22. template<typename FromType, typename ToType> uint32 ConvertOffset(uint32 ptr)
  23. {
  24. return ConvertOffset(ptr, sizeof(FromType), sizeof(ToType));
  25. }
  26. template<typename T> uint32 ConvertToJsVarOffset(uint32 ptr)
  27. {
  28. return ConvertOffset<T, Js::Var>(ptr);
  29. }
  30. template<typename T> uint32 ConvertFromJsVarOffset(uint32 ptr)
  31. {
  32. return ConvertOffset<Js::Var, T>(ptr);
  33. }
  34. struct EmitInfoBase
  35. {
  36. EmitInfoBase(RegSlot location_) : location(location_) {}
  37. EmitInfoBase() : location(Js::Constants::NoRegister) {}
  38. RegSlot location;
  39. };
  40. enum Types
  41. {
  42. INT32,
  43. INT64,
  44. FLOAT32,
  45. FLOAT64,
  46. SIMD,
  47. LIMIT
  48. };
  49. const Types FirstType = (Types)0;
  50. const Types LastType = (Types)(LIMIT - 1);
  51. uint32 GetTypeByteSize(Types type);
  52. Types FromIRType(IRType irType);
  53. /// Register space for const, parameters, variables and tmp values
  54. /// --------------------------------------------------------
  55. /// | Reserved | Consts | Parameters | Variables | Tmp
  56. /// --------------------------------------------------------
  57. /// Cannot allocate in any different order
  58. class RegisterSpace
  59. {
  60. // Total number of register allocated
  61. RegSlot mRegisterCount;
  62. // location of the first temporary register and last variable + 1
  63. RegSlot mFirstTmpReg;
  64. // Location of the next register to be allocated
  65. RegSlot mNextLocation;
  66. // number of const, includes the reserved slots
  67. RegSlot mNbConst;
  68. public:
  69. // Constructor
  70. RegisterSpace(RegSlot reservedSlotsCount = 0) :
  71. mRegisterCount( reservedSlotsCount )
  72. , mFirstTmpReg( reservedSlotsCount )
  73. , mNextLocation( reservedSlotsCount )
  74. , mNbConst( reservedSlotsCount )
  75. {
  76. }
  77. // Get the number of const allocated
  78. RegSlot GetConstCount() const { return mNbConst; }
  79. // Get the location of the first temporary register
  80. RegSlot GetFirstTmpRegister() const{ return mFirstTmpReg; }
  81. // Get the total number of temporary register allocated
  82. RegSlot GetTmpCount() const { return mRegisterCount-mFirstTmpReg; }
  83. // Get number of local variables
  84. RegSlot GetVarCount() const { return mFirstTmpReg - mNbConst; }
  85. // Get the total number of variable allocated ( including temporaries )
  86. RegSlot GetTotalVarCount() const { return mRegisterCount - mNbConst; }
  87. RegSlot GetRegisterCount() const { return mRegisterCount; }
  88. // Acquire a location for a register. Use only for arguments and Variables
  89. RegSlot AcquireRegister()
  90. {
  91. // Makes sure no temporary register have been allocated yet
  92. Assert(mFirstTmpReg == mRegisterCount && mNextLocation == mFirstTmpReg);
  93. ++mFirstTmpReg;
  94. ++mRegisterCount;
  95. return mNextLocation++;
  96. }
  97. // Acquire a location for a constant
  98. RegSlot AcquireConstRegister()
  99. {
  100. ++mNbConst;
  101. return AcquireRegister();
  102. }
  103. // Acquire a location for a temporary register
  104. RegSlot AcquireTmpRegister()
  105. {
  106. // Make sure this function is called correctly
  107. Assert(mNextLocation <= mRegisterCount && mNextLocation >= mFirstTmpReg);
  108. // Allocate a new temp pseudo-register, increasing the locals count if necessary.
  109. if(mNextLocation == mRegisterCount)
  110. {
  111. ++mRegisterCount;
  112. }
  113. #if DBG_DUMP
  114. PrintTmpRegisterAllocation(mNextLocation);
  115. #endif
  116. return mNextLocation++;
  117. }
  118. // Release a location for a temporary register, must be the last location acquired
  119. void ReleaseTmpRegister( RegSlot tmpReg )
  120. {
  121. // make sure the location released is valid
  122. Assert(tmpReg != Js::Constants::NoRegister);
  123. // Put this reg back on top of the temp stack (if it's a temp).
  124. if( this->IsTmpReg( tmpReg ) )
  125. {
  126. Assert( tmpReg == this->mNextLocation - 1 );
  127. #if DBG_DUMP
  128. PrintTmpRegisterAllocation(mNextLocation - 1, true);
  129. #endif
  130. mNextLocation--;
  131. }
  132. }
  133. // Checks if the register is a temporary register
  134. bool IsTmpReg(RegSlot tmpReg)
  135. {
  136. Assert(mFirstTmpReg != Js::Constants::NoRegister);
  137. return !IsConstReg(tmpReg) && tmpReg >= mFirstTmpReg;
  138. }
  139. // Checks if the register is a const register
  140. bool IsConstReg(RegSlot reg)
  141. {
  142. // a register is const if it is between the first register and the end of consts
  143. return reg < mNbConst && reg != 0;
  144. }
  145. // Checks if the register is a variable register
  146. bool IsVarReg(RegSlot reg)
  147. {
  148. // a register is a var if it is between the last const and the end
  149. // equivalent to reg>=mNbConst && reg<mRegisterCount
  150. // forcing unsigned, if reg < mNbConst then reg-mNbConst = 0xFFFFF..
  151. return (uint32)(reg - mNbConst) < (uint32)(mRegisterCount - mNbConst);
  152. }
  153. // Releases a location if its a temporary, safe to call with any expression
  154. void ReleaseLocation(const EmitInfoBase *pnode)
  155. {
  156. // Release the temp assigned to this expression so it can be re-used.
  157. if(pnode && pnode->location != Js::Constants::NoRegister)
  158. {
  159. ReleaseTmpRegister(pnode->location);
  160. }
  161. }
  162. // Checks if the location points to a temporary register
  163. bool IsTmpLocation(const EmitInfoBase* pnode)
  164. {
  165. if(pnode && pnode->location != Js::Constants::NoRegister)
  166. {
  167. return IsTmpReg(pnode->location);
  168. }
  169. return false;
  170. }
  171. // Checks if the location points to a constant register
  172. bool IsConstLocation(const EmitInfoBase* pnode)
  173. {
  174. if(pnode && pnode->location != Js::Constants::NoRegister)
  175. {
  176. return IsConstReg(pnode->location);
  177. }
  178. return false;
  179. }
  180. // Checks if the location points to a variable register
  181. bool IsVarLocation(const EmitInfoBase* pnode)
  182. {
  183. if(pnode && pnode->location != Js::Constants::NoRegister)
  184. {
  185. return IsVarReg(pnode->location);
  186. }
  187. return false;
  188. }
  189. // Checks if the location is valid (within bounds of already allocated registers)
  190. bool IsValidLocation(const EmitInfoBase* pnode)
  191. {
  192. if(pnode && pnode->location != Js::Constants::NoRegister)
  193. {
  194. return pnode->location < mRegisterCount;
  195. }
  196. return false;
  197. }
  198. template<typename T> static Types GetRegisterSpaceType();
  199. #if DBG_DUMP
  200. // Used for debugging
  201. Types mType;
  202. static void GetTypeDebugName(Types type, char16* buf, uint bufsize, bool shortName = false);
  203. void PrintTmpRegisterAllocation(RegSlot loc, bool deallocation = false);
  204. #endif
  205. };
  206. struct TypedConstSourcesInfo
  207. {
  208. uint32 srcByteOffsets[WAsmJs::LIMIT];
  209. uint32 bytesUsed;
  210. };
  211. struct TypedSlotInfo
  212. {
  213. TypedSlotInfo(): constCount(0), varCount(0), tmpCount(0), byteOffset(0), constSrcByteOffset(0) { }
  214. Field(uint32) constCount;
  215. Field(uint32) varCount;
  216. Field(uint32) tmpCount;
  217. // Offset in bytes from the start of InterpreterStack::m_localSlot
  218. Field(uint32) byteOffset;
  219. // Offset in bytes from the start of the const table before shuffling (InterpreterStackFrame::AlignMemoryForAsmJs())
  220. Field(uint32) constSrcByteOffset;
  221. };
  222. typedef RegisterSpace*(*AllocateRegisterSpaceFunc)(ArenaAllocator*, WAsmJs::Types);
  223. class TypedRegisterAllocator
  224. {
  225. uint32 mExcludedMask;
  226. RegisterSpace* mTypeSpaces[WAsmJs::LIMIT];
  227. public:
  228. TypedRegisterAllocator(ArenaAllocator* allocator, AllocateRegisterSpaceFunc allocateFunc, uint32 excludedMask = 0);
  229. void CommitToFunctionInfo(Js::AsmJsFunctionInfo* funcInfo, Js::FunctionBody* body) const;
  230. void CommitToFunctionBody(Js::FunctionBody* body);
  231. TypedConstSourcesInfo GetConstSourceInfos() const;
  232. bool IsTypeExcluded(Types type) const;
  233. #if DBG_DUMP
  234. void DumpLocalsInfo() const;
  235. // indexes' array size must be WAsmJs::RegisterSpace::LIMIT
  236. void GetArgumentStartIndex(uint32* indexes) const;
  237. #endif
  238. RegisterSpace* GetRegisterSpace(Types type) const;
  239. private:
  240. bool IsValidType(Types type) const;
  241. };
  242. };
  243. #endif