IRBuilderAsmJs.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. 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. #ifdef ASMJS_PLAT
  7. namespace AsmJsRegSlots
  8. {
  9. enum ConstSlots
  10. {
  11. ReturnReg = 0,
  12. ModuleMemReg,
  13. ArrayReg,
  14. WasmMemoryReg,
  15. BufferReg,
  16. LengthReg,
  17. RegCount
  18. };
  19. };
  20. class IRBuilderAsmJs
  21. {
  22. friend struct IRBuilderAsmJsSwitchAdapter;
  23. public:
  24. IRBuilderAsmJs(Func * func)
  25. : m_func(func)
  26. , m_IsTJLoopBody(false)
  27. , m_switchAdapter(this)
  28. , m_switchBuilder(&m_switchAdapter)
  29. {
  30. func->m_workItem->InitializeReader(&m_jnReader, &m_statementReader, func->m_alloc);
  31. m_asmFuncInfo = m_func->GetJITFunctionBody()->GetAsmJsInfo();
  32. #if 0
  33. // templatized JIT loop body
  34. if (func->IsLoopBody())
  35. {
  36. Js::LoopEntryPointInfo* loopEntryPointInfo = (Js::LoopEntryPointInfo*)(func->m_workItem->GetEntryPoint());
  37. if (loopEntryPointInfo->GetIsTJMode())
  38. {
  39. m_IsTJLoopBody = true;
  40. func->isTJLoopBody = true;
  41. }
  42. }
  43. #endif
  44. }
  45. void Build();
  46. private:
  47. void LoadNativeCodeData();
  48. void AddInstr(IR::Instr * instr, uint32 offset);
  49. bool IsLoopBody()const;
  50. uint GetLoopBodyExitInstrOffset() const;
  51. IR::SymOpnd * BuildLoopBodySlotOpnd(SymID symId);
  52. IR::SymOpnd * BuildAsmJsLoopBodySlotOpnd(SymID symId, IRType opndType);
  53. void EnsureLoopBodyLoadSlot(SymID symId);
  54. void EnsureLoopBodyAsmJsLoadSlot(SymID symId, IRType type);
  55. bool IsLoopBodyOuterOffset(uint offset) const;
  56. bool IsLoopBodyReturnIPInstr(IR::Instr * instr) const;
  57. IR::Opnd * InsertLoopBodyReturnIPInstr(uint targetOffset, uint offset);
  58. IR::Instr * CreateLoopBodyReturnIPInstr(uint targetOffset, uint offset);
  59. IR::RegOpnd * BuildDstOpnd(Js::RegSlot dstRegSlot, IRType type);
  60. IR::RegOpnd * BuildSrcOpnd(Js::RegSlot srcRegSlot, IRType type);
  61. IR::RegOpnd * BuildIntConstOpnd(Js::RegSlot regSlot);
  62. SymID BuildSrcStackSymID(Js::RegSlot regSlot, IRType type = IRType::TyVar);
  63. IR::SymOpnd * BuildFieldOpnd(Js::RegSlot reg, Js::PropertyId propertyId, PropertyKind propertyKind, IRType type, bool scale = true);
  64. PropertySym * BuildFieldSym(Js::RegSlot reg, Js::PropertyId propertyId, PropertyKind propertyKind);
  65. uint AddStatementBoundary(uint statementIndex, uint offset);
  66. BranchReloc * AddBranchInstr(IR::BranchInstr *instr, uint32 offset, uint32 targetOffset);
  67. BranchReloc * CreateRelocRecord(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset);
  68. void BuildHeapBufferReload(uint32 offset);
  69. template<typename T, typename ConstOpnd, typename F>
  70. void CreateLoadConstInstrForType(byte* table, Js::RegSlot& regAllocated, uint32 constCount, uint32 offset, IRType irType, ValueType valueType, Js::OpCode opcode, F extraProcess);
  71. void BuildConstantLoads();
  72. void BuildImplicitArgIns();
  73. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  74. void BuildArgInTracing();
  75. #endif
  76. void InsertLabels();
  77. IR::LabelInstr * CreateLabel(IR::BranchInstr * branchInstr, uint& offset);
  78. #if DBG
  79. BVFixed * m_usedAsTemp;
  80. #endif
  81. Js::RegSlot GetTypedRegFromRegSlot(Js::RegSlot reg, WAsmJs::Types type);
  82. Js::RegSlot GetRegSlotFromTypedReg(Js::RegSlot srcReg, WAsmJs::Types type);
  83. Js::RegSlot GetRegSlotFromIntReg(Js::RegSlot srcIntReg) {return GetRegSlotFromTypedReg(srcIntReg, WAsmJs::INT32);}
  84. Js::RegSlot GetRegSlotFromInt64Reg(Js::RegSlot srcIntReg) {return GetRegSlotFromTypedReg(srcIntReg, WAsmJs::INT64);}
  85. Js::RegSlot GetRegSlotFromFloatReg(Js::RegSlot srcFloatReg) {return GetRegSlotFromTypedReg(srcFloatReg, WAsmJs::FLOAT32);}
  86. Js::RegSlot GetRegSlotFromDoubleReg(Js::RegSlot srcDoubleReg) {return GetRegSlotFromTypedReg(srcDoubleReg, WAsmJs::FLOAT64);}
  87. Js::RegSlot GetRegSlotFromSimd128Reg(Js::RegSlot srcSimd128Reg) {return GetRegSlotFromTypedReg(srcSimd128Reg, WAsmJs::SIMD);}
  88. Js::RegSlot GetRegSlotFromVarReg(Js::RegSlot srcVarReg);
  89. Js::OpCode GetSimdOpcode(Js::OpCodeAsmJs asmjsOpcode);
  90. void GetSimdTypesFromAsmType(Js::AsmJsType::Which asmType, IRType *pIRType, ValueType *pValueType = nullptr);
  91. IR::Instr * AddExtendedArg(IR::RegOpnd *src1, IR::RegOpnd *src2, uint32 offset);
  92. bool RegIsSimd128ReturnVar(Js::RegSlot reg);
  93. SymID GetMappedTemp(Js::RegSlot reg);
  94. void SetMappedTemp(Js::RegSlot reg, SymID tempId);
  95. BOOL GetTempUsed(Js::RegSlot reg);
  96. void SetTempUsed(Js::RegSlot reg, BOOL used);
  97. BOOL RegIsTemp(Js::RegSlot reg);
  98. BOOL RegIsConstant(Js::RegSlot reg);
  99. BOOL RegIsVar(Js::RegSlot reg);
  100. BOOL RegIsTypedVar(Js::RegSlot reg, WAsmJs::Types type);
  101. BOOL RegIsIntVar(Js::RegSlot reg) {return RegIsTypedVar(reg, WAsmJs::INT32);}
  102. BOOL RegIsInt64Var(Js::RegSlot reg) {return RegIsTypedVar(reg, WAsmJs::INT64);}
  103. BOOL RegIsFloatVar(Js::RegSlot reg) {return RegIsTypedVar(reg, WAsmJs::FLOAT32);}
  104. BOOL RegIsDoubleVar(Js::RegSlot reg) {return RegIsTypedVar(reg, WAsmJs::FLOAT64);}
  105. BOOL RegIsSimd128Var(Js::RegSlot reg) {return RegIsTypedVar(reg, WAsmJs::SIMD);}
  106. void BuildArgOut(IR::Opnd* srcOpnd, uint32 dstRegSlot, uint32 offset, IRType type, ValueType valueType = ValueType::Uninitialized);
  107. void BuildFromVar(uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot srcRegSlot, IRType irType, ValueType valueType);
  108. #define LAYOUT_TYPE(layout) \
  109. void Build##layout(Js::OpCodeAsmJs newOpcode, uint32 offset);
  110. #define LAYOUT_TYPE_WMS(layout) \
  111. template <typename SizePolicy> void Build##layout(Js::OpCodeAsmJs newOpcode, uint32 offset);
  112. #define EXCLUDE_FRONTEND_LAYOUT
  113. #include "ByteCode/LayoutTypesAsmJs.h"
  114. void BuildSimd_1Ints(Js::OpCodeAsmJs newOpcode, uint32 offset, IRType dstSimdType, Js::RegSlot* srcRegSlots, Js::RegSlot dstRegSlot, uint LANES);
  115. void BuildSimd_1Int1(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot, IRType simdType);
  116. void BuildSimd_2Int2(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot, Js::RegSlot src2RegSlot, Js::RegSlot src3RegSlot, IRType simdType);
  117. void BuildSimd_2(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot, IRType simdType);
  118. void BuildSimd_2Int1(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot, Js::RegSlot src2RegSlot, IRType simdType);
  119. void BuildSimd_3(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot, Js::RegSlot src2RegSlot, IRType simdType);
  120. void BuildSimd_3(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot, Js::RegSlot src2RegSlot, IRType dstSimdType, IRType srcSimdType);
  121. void BuildSimdConversion(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot srcRegSlot, IRType dstSimdType, IRType srcSimdType);
  122. ValueType GetSimdValueTypeFromIRType(IRType type);
  123. void BuildElementSlot(Js::OpCodeAsmJs newOpcode, uint32 offset, int32 slotIndex, Js::RegSlot value, Js::RegSlot instance);
  124. void BuildAsmUnsigned1(Js::OpCodeAsmJs newOpcode, uint value);
  125. void BuildAsmTypedArr(Js::OpCodeAsmJs newOpcode, uint32 offset, uint32 slotIndex, Js::RegSlot value, int8 viewType);
  126. void BuildAsmSimdTypedArr(Js::OpCodeAsmJs newOpcode, uint32 offset, uint32 slotIndex, Js::RegSlot value, int8 viewType, uint8 DataWidth);
  127. void BuildAsmCall(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::ArgSlot argCount, Js::RegSlot ret, Js::RegSlot function, int8 returnType);
  128. void BuildAsmReg1(Js::OpCodeAsmJs newOpcode, uint32 offset, Js::RegSlot dstReg);
  129. void BuildBrInt1(Js::OpCodeAsmJs newOpcode, uint32 offset, int32 relativeOffset, Js::RegSlot src);
  130. void BuildBrInt2(Js::OpCodeAsmJs newOpcode, uint32 offset, int32 relativeOffset, Js::RegSlot src1, Js::RegSlot src2);
  131. void BuildBrInt1Const1(Js::OpCodeAsmJs newOpcode, uint32 offset, int32 relativeOffset, Js::RegSlot src1, int32 src2);
  132. void BuildBrCmp(Js::OpCodeAsmJs newOpcode, uint32 offset, int32 relativeOffset, IR::RegOpnd* src1Opnd, IR::Opnd* src2Opnd);
  133. void GenerateLoopBodySlotAccesses(uint offset);
  134. void GenerateLoopBodyStSlots(SymID loopParamSymId, uint offset);
  135. Js::PropertyId CalculatePropertyOffset(SymID id, IRType type, bool isVar = true);
  136. IR::Instr* GenerateStSlotForReturn(IR::RegOpnd* srcOpnd, IRType type);
  137. IR::RegOpnd* BuildTrapIfZero(IR::RegOpnd* srcOpnd, uint32 offset);
  138. IR::RegOpnd* BuildTrapIfMinIntOverNegOne(IR::RegOpnd* src1Opnd, IR::RegOpnd* src2Opnd, uint32 offset);
  139. JitArenaAllocator * m_tempAlloc;
  140. JitArenaAllocator * m_funcAlloc;
  141. Func * m_func;
  142. IR::Instr * m_lastInstr;
  143. IR::Instr ** m_offsetToInstruction;
  144. Js::ByteCodeReader m_jnReader;
  145. Js::StatementReader<Js::FunctionBody::ArenaStatementMapList> m_statementReader;
  146. SList<IR::Instr *> * m_argStack;
  147. SList<IR::Instr *> * m_tempList;
  148. SList<int32> * m_argOffsetStack;
  149. SList<BranchReloc *> * m_branchRelocList;
  150. // 1 for const, 1 for var, 1 for temps for each type and 1 for last
  151. static const uint32 m_firstsTypeCount = WAsmJs::LIMIT * 3 + 1;
  152. Js::RegSlot m_firstsType[m_firstsTypeCount];
  153. Js::RegSlot m_firstVarConst;
  154. Js::RegSlot m_firstIRTemp;
  155. Js::OpCode * m_simdOpcodesMap;
  156. Js::RegSlot GetFirstConst(WAsmJs::Types type) { return m_firstsType[type]; }
  157. Js::RegSlot GetFirstVar(WAsmJs::Types type) { return m_firstsType[type + WAsmJs::LIMIT]; }
  158. Js::RegSlot GetFirstTmp(WAsmJs::Types type) { return m_firstsType[type + WAsmJs::LIMIT * 2]; }
  159. Js::RegSlot GetLastConst(WAsmJs::Types type) { return m_firstsType[type + 1]; }
  160. Js::RegSlot GetLastVar(WAsmJs::Types type) { return m_firstsType[type + WAsmJs::LIMIT + 1]; }
  161. Js::RegSlot GetLastTmp(WAsmJs::Types type) { return m_firstsType[type + WAsmJs::LIMIT * 2 + 1]; }
  162. SymID * m_tempMap;
  163. BVFixed * m_fbvTempUsed;
  164. uint32 m_functionStartOffset;
  165. const AsmJsJITInfo * m_asmFuncInfo;
  166. StackSym * m_loopBodyRetIPSym;
  167. BVFixed * m_ldSlots;
  168. BVFixed * m_stSlots;
  169. BOOL m_IsTJLoopBody;
  170. IRBuilderAsmJsSwitchAdapter m_switchAdapter;
  171. SwitchIRBuilder m_switchBuilder;
  172. IR::RegOpnd * m_funcOpnd;
  173. #if DBG
  174. uint32 m_offsetToInstructionCount;
  175. #endif
  176. #define BUILD_LAYOUT_DEF(layout, ...) void Build##layout (Js::OpCodeAsmJs, uint32, __VA_ARGS__);
  177. #define Reg_Type Js::RegSlot
  178. #define Int_Type Js::RegSlot
  179. #define Long_Type Js::RegSlot
  180. #define Float_Type Js::RegSlot
  181. #define Double_Type Js::RegSlot
  182. #define IntConst_Type int
  183. #define LongConst_Type int64
  184. #define FloatConst_Type float
  185. #define DoubleConst_Type double
  186. #define Float32x4_Type Js::RegSlot
  187. #define Bool32x4_Type Js::RegSlot
  188. #define Int32x4_Type Js::RegSlot
  189. #define Float64x2_Type Js::RegSlot
  190. #define Int16x8_Type Js::RegSlot
  191. #define Bool16x8_Type Js::RegSlot
  192. #define Int8x16_Type Js::RegSlot
  193. #define Bool8x16_Type Js::RegSlot
  194. #define Uint32x4_Type Js::RegSlot
  195. #define Uint16x8_Type Js::RegSlot
  196. #define Uint8x16_Type Js::RegSlot
  197. #define LAYOUT_TYPE_WMS_REG2(layout, t0, t1) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type)
  198. #define LAYOUT_TYPE_WMS_REG3(layout, t0, t1, t2) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type)
  199. #define LAYOUT_TYPE_WMS_REG4(layout, t0, t1, t2, t3) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type)
  200. #define LAYOUT_TYPE_WMS_REG5(layout, t0, t1, t2, t3, t4) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type)
  201. #define LAYOUT_TYPE_WMS_REG6(layout, t0, t1, t2, t3, t4, t5) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type, t5##_Type)
  202. #define LAYOUT_TYPE_WMS_REG7(layout, t0, t1, t2, t3, t4, t5, t6) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type, t5##_Type, t6##_Type)
  203. #define LAYOUT_TYPE_WMS_REG9(layout, t0, t1, t2, t3, t4, t5, t6, t7, t8) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type, t5##_Type, t6##_Type, t7##_Type, t8##_Type)
  204. #define LAYOUT_TYPE_WMS_REG10(layout, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type, t5##_Type, t6##_Type, t7##_Type, t8##_Type, t9##_Type)
  205. #define LAYOUT_TYPE_WMS_REG11(layout, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type, t5##_Type, t6##_Type, t7##_Type, t8##_Type, t9##_Type, t10##_Type)
  206. #define LAYOUT_TYPE_WMS_REG17(layout, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type, t5##_Type, t6##_Type, t7##_Type, t8##_Type, t9##_Type, t10##_Type, t11##_Type, t12##_Type, t13##_Type, t14##_Type, t15##_Type, t16##_Type)
  207. #define LAYOUT_TYPE_WMS_REG18(layout, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type, t5##_Type, t6##_Type, t7##_Type, t8##_Type, t9##_Type, t10##_Type, t11##_Type, t12##_Type, t13##_Type, t14##_Type, t15##_Type, t16##_Type, t17##_Type)
  208. #define LAYOUT_TYPE_WMS_REG19(layout, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18) BUILD_LAYOUT_DEF(layout, t0##_Type, t1##_Type, t2##_Type, t3##_Type, t4##_Type, t5##_Type, t6##_Type, t7##_Type, t8##_Type, t9##_Type, t10##_Type, t11##_Type, t12##_Type, t13##_Type, t14##_Type, t15##_Type, t16##_Type, t17##_Type, t18##_Type)
  209. #define EXCLUDE_FRONTEND_LAYOUT
  210. #include "LayoutTypesAsmJs.h"
  211. #undef BUILD_LAYOUT_DEF
  212. #undef RegType
  213. #undef IntType
  214. #undef LongType
  215. #undef FloatType
  216. #undef DoubleType
  217. #undef IntConstType
  218. #undef LongConstType
  219. #undef FloatConstType
  220. #undef DoubleConstType
  221. #undef Float32x4Type
  222. #undef Bool32x4Type
  223. #undef Int32x4Type
  224. #undef Float64x2Type
  225. #undef Int16x8Type
  226. #undef Bool16x8Type
  227. #undef Int8x16Type
  228. #undef Bool8x16Type
  229. #undef Uint32x4Type
  230. #undef Uint16x8Type
  231. #undef Uint8x16Type
  232. };
  233. #endif