ByteCodeReader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. namespace Js
  7. {
  8. struct ByteCodeReader
  9. {
  10. static uint32 GetStartLocationOffset() { return offsetof(ByteCodeReader, m_startLocation); }
  11. static uint32 GetCurrentLocationOffset() { return offsetof(ByteCodeReader, m_currentLocation); }
  12. private:
  13. const byte * m_startLocation;
  14. const byte * m_currentLocation;
  15. #if DBG
  16. const byte * m_endLocation;
  17. #endif
  18. public:
  19. void Create(FunctionBody* functionRead, uint startOffset = 0);
  20. void Create(FunctionBody* functionRead, uint startOffset, bool useOriginalByteCode);
  21. #if DBG
  22. void Create(const byte * byteCodeStart, uint startOffset, uint byteCodeLength);
  23. #else
  24. void Create(const byte * byteCodeStart, uint startOffset);
  25. #endif
  26. uint GetCurrentOffset() const;
  27. const byte * SetCurrentOffset(int byteOffset);
  28. const byte * SetCurrentRelativeOffset(const byte * ip, int byteOffset);
  29. template<typename LayoutType> inline const unaligned LayoutType * GetLayout();
  30. template<typename LayoutType> inline const unaligned LayoutType * GetLayout(const byte*& ip);
  31. // Read*Op advance the IP,
  32. // Peek*Op doesn't move the IP
  33. // *ByteOp only read one byte of the opcode,
  34. // *Op interprets and remove the large layout prefix
  35. private:
  36. OpCode ReadOp(const byte *&ip, LayoutSize& layoutSize) const;
  37. OpCode ReadPrefixedOp(const byte *&ip, LayoutSize& layoutSize, OpCode prefix) const;
  38. public:
  39. OpCode ReadOp(LayoutSize& layoutSize);
  40. OpCodeAsmJs ReadAsmJsOp(LayoutSize& layoutSize);
  41. OpCode ReadPrefixedOp(LayoutSize& layoutSize, OpCode prefix);
  42. OpCode PeekOp(LayoutSize& layoutSize) const;
  43. OpCode PeekOp() const { LayoutSize layoutSize; return PeekOp(layoutSize); }
  44. OpCode PeekOp(const byte * ip, LayoutSize& layoutSize);
  45. static OpCode ReadByteOp(const byte*& ip);
  46. static OpCode PeekByteOp(const byte * ip);
  47. // Declare reading functions
  48. #define LAYOUT_TYPE(layout) \
  49. const unaligned OpLayout##layout* layout(); \
  50. const unaligned OpLayout##layout* layout(const byte*& ip);
  51. #include "LayoutTypes.h"
  52. #ifndef TEMP_DISABLE_ASMJS
  53. #define LAYOUT_TYPE(layout) \
  54. const unaligned OpLayout##layout* layout(); \
  55. const unaligned OpLayout##layout* layout(const byte*& ip);
  56. #define EXCLUDE_DUP_LAYOUT
  57. #include "LayoutTypesAsmJs.h"
  58. #endif
  59. template <typename T>
  60. static AuxArray<T> const * ReadAuxArray(uint offset, FunctionBody * functionBody);
  61. template <typename T>
  62. static AuxArray<T> const * ReadAuxArrayWithLock(uint offset, FunctionBody * functionBody);
  63. static PropertyIdArray const * ReadPropertyIdArray(uint offset, FunctionBody * functionBody);
  64. static PropertyIdArray const * ReadPropertyIdArrayWithLock(uint offset, FunctionBody * functionBody);
  65. static VarArrayVarCount const * ReadVarArrayVarCount(uint offset, FunctionBody * functionBody);
  66. static VarArrayVarCount const * ReadVarArrayVarCountWithLock(uint offset, FunctionBody * functionBody);
  67. const byte* GetIP();
  68. void SetIP(const byte *const ip);
  69. template<class T> const unaligned T* AuxiliaryContext(const byte*& ip, const byte ** content);
  70. #if DBG_DUMP
  71. byte GetRawByte(int i);
  72. #endif
  73. };
  74. template<typename LayoutType>
  75. inline const unaligned LayoutType * ByteCodeReader::GetLayout()
  76. {
  77. size_t layoutSize = sizeof(LayoutType);
  78. AssertMsg((layoutSize > 0) && (layoutSize < 100), "Ensure valid layout size");
  79. const byte * layoutData = m_currentLocation;
  80. m_currentLocation += layoutSize;
  81. Assert(m_currentLocation <= m_endLocation);
  82. return reinterpret_cast<const unaligned LayoutType *>(layoutData);
  83. }
  84. template<>
  85. inline const unaligned OpLayoutEmpty * ByteCodeReader::GetLayout<OpLayoutEmpty>()
  86. {
  87. return nullptr;
  88. }
  89. } // namespace Js