ByteCodeReader.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. 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. uint GetCurrentOffset() const;
  22. const byte * SetCurrentOffset(int byteOffset);
  23. const byte * SetCurrentRelativeOffset(const byte * ip, int byteOffset);
  24. template<typename LayoutType> inline const unaligned LayoutType * GetLayout();
  25. template<typename LayoutType> inline const unaligned LayoutType * GetLayout(const byte*& ip);
  26. // Read*Op advance the IP,
  27. // Peek*Op doesn't move the IP
  28. // *ByteOp only read one byte of the opcode,
  29. // *Op interprets and remove the large layout prefix
  30. private:
  31. OpCode ReadOp(const byte *&ip, LayoutSize& layoutSize) const;
  32. OpCode ReadPrefixedOp(const byte *&ip, LayoutSize& layoutSize, OpCode prefix) const;
  33. public:
  34. OpCode ReadOp(LayoutSize& layoutSize);
  35. OpCode ReadPrefixedOp(LayoutSize& layoutSize, OpCode prefix);
  36. OpCode PeekOp(LayoutSize& layoutSize) const;
  37. OpCode PeekOp() const { LayoutSize layoutSize; return PeekOp(layoutSize); }
  38. OpCode PeekOp(const byte * ip, LayoutSize& layoutSize);
  39. static OpCode ReadByteOp(const byte*& ip);
  40. static OpCode PeekByteOp(const byte * ip);
  41. // Declare reading functions
  42. #define LAYOUT_TYPE(layout) \
  43. const unaligned OpLayout##layout* layout(); \
  44. const unaligned OpLayout##layout* layout(const byte*& ip);
  45. #include "LayoutTypes.h"
  46. #ifndef TEMP_DISABLE_ASMJS
  47. #define LAYOUT_TYPE(layout) \
  48. const unaligned OpLayout##layout* layout(); \
  49. const unaligned OpLayout##layout* layout(const byte*& ip);
  50. #define EXCLUDE_DUP_LAYOUT
  51. #include "LayoutTypesAsmJs.h"
  52. #endif
  53. template <typename T>
  54. static AuxArray<T> const * ReadAuxArray(uint offset, FunctionBody * functionBody);
  55. static PropertyIdArray const * ReadPropertyIdArray(uint offset, FunctionBody * functionBody, uint extraSlots = 0);
  56. static VarArrayVarCount const * ReadVarArrayVarCount(uint offset, FunctionBody * functionBody);
  57. const byte* GetIP();
  58. void SetIP(const byte *const ip);
  59. template<class T> const unaligned T* AuxiliaryContext(const byte*& ip, const byte ** content);
  60. #if DBG_DUMP
  61. byte GetRawByte(int i);
  62. #endif
  63. };
  64. } // namespace Js