Encoder.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. class Encoder
  17. {
  18. friend class EncoderMD;
  19. public:
  20. Encoder(Func * func) : m_func(func), m_encoderMD(func), m_inlineeFrameMap(nullptr) {}
  21. void Encode();
  22. void RecordInlineeFrame(Func* inlinee, uint32 currentOffset);
  23. void RecordBailout(IR::Instr* instr, uint32 currentOffset);
  24. private:
  25. bool DoTrackAllStatementBoundary() const;
  26. Func * m_func;
  27. EncoderMD m_encoderMD;
  28. BYTE * m_encodeBuffer;
  29. BYTE * m_pc;
  30. uint32 m_encodeBufferSize;
  31. ArenaAllocator *m_tempAlloc;
  32. ArenaInlineeFrameMap* m_inlineeFrameMap;
  33. uint32 m_inlineeFrameMapDataOffset;
  34. uint32 m_inlineeFrameMapRecordCount;
  35. typedef JsUtil::List<LazyBailOutRecord, ArenaAllocator> ArenaBailoutRecordMap;
  36. ArenaBailoutRecordMap* m_bailoutRecordMap;
  37. #if DBG_DUMP
  38. void DumpInlineeFrameMap(size_t baseAddress);
  39. uint32 * m_offsetBuffer;
  40. uint32 m_instrNumber;
  41. #endif
  42. PragmaInstrList *m_pragmaInstrToRecordOffset;
  43. PragmaInstrList *m_pragmaInstrToRecordMap;
  44. #if defined(_M_IX86) || defined(_M_X64)
  45. InlineeFrameRecords *m_inlineeFrameRecords;
  46. BOOL ShortenBranchesAndLabelAlign(BYTE **codeStart, ptrdiff_t *codeSize, uint * brShortenedBufferCRC, uint bufferCrcToValidate, size_t jumpTableSize);
  47. void revertRelocList();
  48. template <bool restore> void CopyMaps(OffsetList **m_origInlineeFrameRecords, OffsetList **m_origInlineeFrameMap, OffsetList **m_origPragmaInstrToRecordOffset, OffsetList **m_origOffsetBuffer);
  49. #endif
  50. void InsertNopsForLabelAlignment(int nopCount, BYTE ** pDstBuffer);
  51. void CopyPartialBufferAndCalculateCRC(BYTE ** ptrDstBuffer, size_t &dstSize, BYTE * srcStart, BYTE * srcEnd, uint* pBufferCRC, size_t jumpTableSize = 0);
  52. BYTE FindNopCountFor16byteAlignment(size_t address);
  53. uint32 GetCurrentOffset() const;
  54. void TryCopyAndAddRelocRecordsForSwitchJumpTableEntries(BYTE *codeStart, size_t codeSize, JmpTableList * jumpTableListForSwitchStatement, size_t totalJmpTableSizeInBytes);
  55. void ValidateCRC(uint bufferCRC, uint initialCRCSeed, _In_reads_bytes_(count) void* buffer, size_t count);
  56. static void EnsureRelocEntryIntegrity(size_t newBufferStartAddress, size_t codeSize, size_t oldBufferAddress, size_t relocAddress, uint offsetBytes, ptrdiff_t opndData, bool isRelativeAddr = true);
  57. #if defined(_M_IX86) || defined(_M_X64)
  58. 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);
  59. #endif
  60. };