Encoder.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "EncoderMD.h"
  6. #include "Backend.h"
  7. ///---------------------------------------------------------------------------
  8. ///
  9. /// class Encoder
  10. ///
  11. ///---------------------------------------------------------------------------
  12. typedef JsUtil::List<NativeOffsetInlineeFramePair, ArenaAllocator> ArenaInlineeFrameMap;
  13. typedef JsUtil::List<IR::PragmaInstr*, ArenaAllocator> PragmaInstrList;
  14. typedef JsUtil::List<uint32, ArenaAllocator> OffsetList;
  15. typedef JsUtil::List<BranchJumpTableWrapper*, ArenaAllocator> JmpTableList;
  16. typedef JsUtil::List<LazyBailOutRecord, ArenaAllocator> ArenaLazyBailoutRecordList;
  17. struct FixUpMapIndex
  18. {
  19. uint32 offsetBuffIndex = 0;
  20. uint32 pragmaInstToRecordOffsetIndex = 0;
  21. uint32 inlineeFrameRecordsIndex = 0;
  22. uint32 inlineeFrameMapIndex = 0;
  23. uint32 lazyBailOutRecordListIndex = 0;
  24. };
  25. class Encoder
  26. {
  27. friend class EncoderMD;
  28. public:
  29. Encoder(Func * func) :
  30. m_func(func), m_encoderMD(func), m_inlineeFrameMap(nullptr),
  31. m_lazyBailOutThunkOffset(0), m_sortedLazyBailoutRecordList(nullptr)
  32. {}
  33. void Encode();
  34. void RecordInlineeFrame(Func* inlinee, uint32 currentOffset);
  35. private:
  36. bool DoTrackAllStatementBoundary() const;
  37. Func * m_func;
  38. EncoderMD m_encoderMD;
  39. BYTE * m_encodeBuffer;
  40. BYTE * m_pc;
  41. uint32 m_encodeBufferSize;
  42. ArenaAllocator *m_tempAlloc;
  43. ArenaInlineeFrameMap* m_inlineeFrameMap;
  44. uint32 m_inlineeFrameMapDataOffset;
  45. uint32 m_inlineeFrameMapRecordCount;
  46. uint32 m_lazyBailOutThunkOffset;
  47. ArenaLazyBailoutRecordList* m_sortedLazyBailoutRecordList;
  48. #if DBG_DUMP
  49. void DumpInlineeFrameMap(size_t baseAddress);
  50. uint32 * m_offsetBuffer;
  51. uint32 m_instrNumber;
  52. #endif
  53. PragmaInstrList *m_pragmaInstrToRecordOffset;
  54. PragmaInstrList *m_pragmaInstrToRecordMap;
  55. #if defined(_M_IX86) || defined(_M_X64)
  56. InlineeFrameRecords *m_inlineeFrameRecords;
  57. BOOL ShortenBranchesAndLabelAlign(BYTE **codeStart, ptrdiff_t *codeSize, uint * brShortenedBufferCRC, uint bufferCrcToValidate, size_t jumpTableSize);
  58. void revertRelocList();
  59. template <bool restore> void CopyMaps(OffsetList **m_origInlineeFrameRecords, OffsetList **m_origInlineeFrameMap, OffsetList **m_origPragmaInstrToRecordOffset, OffsetList **m_origOffsetBuffer);
  60. #endif
  61. void InsertNopsForLabelAlignment(int nopCount, BYTE ** pDstBuffer);
  62. void CopyPartialBufferAndCalculateCRC(BYTE ** ptrDstBuffer, size_t &dstSize, BYTE * srcStart, BYTE * srcEnd, uint* pBufferCRC, size_t jumpTableSize = 0);
  63. BYTE FindNopCountFor16byteAlignment(size_t address);
  64. uint32 GetCurrentOffset() const;
  65. void TryCopyAndAddRelocRecordsForSwitchJumpTableEntries(BYTE *codeStart, size_t codeSize, JmpTableList * jumpTableListForSwitchStatement, size_t totalJmpTableSizeInBytes);
  66. void ValidateCRC(uint bufferCRC, uint initialCRCSeed, _In_reads_bytes_(count) void* buffer, size_t count);
  67. static void EnsureRelocEntryIntegrity(size_t newBufferStartAddress, size_t codeSize, size_t oldBufferAddress, size_t relocAddress, uint offsetBytes, ptrdiff_t opndData, bool isRelativeAddr = true);
  68. #if defined(_M_IX86) || defined(_M_X64)
  69. void ValidateCRCOnFinalBuffer(_In_reads_bytes_(finalCodeSize) BYTE * finalCodeBufferStart, size_t finalCodeSize, size_t jumpTableSize, _In_reads_bytes_(finalCodeSize) BYTE * oldCodeBufferStart, uint initialCrcSeed, uint bufferCrcToValidate, BOOL isSuccessBrShortAndLoopAlign);
  70. #endif
  71. void FixLazyBailOutThunkOffset(uint32 bytesSaved);
  72. void SaveLazyBailOutJitTransferData();
  73. void SaveLazyBailOutThunkOffset(uint32 currentOffset);
  74. void SaveToLazyBailOutRecordList(IR::Instr* instr, uint32 currentOffset);
  75. };