1 //! \file 2 /* 3 ** Copyright (C) - Triton 4 ** 5 ** This program is under the terms of the Apache License 2.0. 6 */ 7 8 #ifndef TRITON_MEMORYACCESS_HPP 9 #define TRITON_MEMORYACCESS_HPP 10 11 #include <triton/archEnums.hpp> 12 #include <triton/ast.hpp> 13 #include <triton/bitsVector.hpp> 14 #include <triton/cpuSize.hpp> 15 #include <triton/dllexport.hpp> 16 #include <triton/immediate.hpp> 17 #include <triton/register.hpp> 18 #include <triton/tritonTypes.hpp> 19 20 21 22 //! The Triton namespace 23 namespace triton { 24 /*! 25 * \addtogroup triton 26 * @{ 27 */ 28 29 //! The Triton namespace 30 namespace arch { 31 /*! 32 * \ingroup triton 33 * \addtogroup arch 34 * @{ 35 */ 36 37 /*! \class MemoryAccess 38 * \brief This class is used to represent a memory access. 39 */ 40 class MemoryAccess : public BitsVector { 41 protected: 42 //! The memory' address. 43 triton::uint64 address; 44 45 //! Contains the pc relative if it exists. 46 triton::uint64 pcRelative; 47 48 //! LEA - If the operand has a segment register, this attribute is filled. 49 triton::arch::Register segmentReg; 50 51 //! LEA - If the operand has a base register, this attribute is filled. 52 triton::arch::Register baseReg; 53 54 //! LEA - If the operand has an index register, this attribute is filled. 55 triton::arch::Register indexReg; 56 57 //! LEA - If the operand has a displacement, this attribute is filled. 58 triton::arch::Immediate displacement; 59 60 //! LEA - If the operand has a scale, this attribute is filled. 61 triton::arch::Immediate scale; 62 63 //! The AST of the memory access (LEA). 64 triton::ast::SharedAbstractNode leaAst; 65 66 private: 67 //! Copy a MemoryAccess. 68 void copy(const MemoryAccess& other); 69 70 public: 71 //! Constructor. 72 TRITON_EXPORT MemoryAccess(); 73 74 //! Constructor. 75 TRITON_EXPORT MemoryAccess(triton::uint64 address, triton::uint32 size /* bytes */); 76 77 //! Constructor by copy. 78 TRITON_EXPORT MemoryAccess(const MemoryAccess& other); 79 80 //! Returns the AST of the memory access (LEA). 81 TRITON_EXPORT triton::ast::SharedAbstractNode getLeaAst(void) const; 82 83 //! Returns the address of the memory. 84 TRITON_EXPORT triton::uint64 getAddress(void) const; 85 86 //! LEA - Gets pc relative. 87 TRITON_EXPORT triton::uint64 getPcRelative(void) const; 88 89 //! Returns the size (in bits) of the memory vector. 90 TRITON_EXPORT triton::uint32 getBitSize(void) const; 91 92 //! Returns the size (in bytes) of the memory vector. 93 TRITON_EXPORT triton::uint32 getSize(void) const; 94 95 //! Returns the type of the operand (triton::arch::OPERAND_MEMORY). 96 TRITON_EXPORT triton::arch::operand_e getType(void) const; 97 98 //! LEA - Returns the segment register operand. 99 TRITON_EXPORT triton::arch::Register& getSegmentRegister(void); 100 101 //! LEA - Returns the base register operand. 102 TRITON_EXPORT triton::arch::Register& getBaseRegister(void); 103 104 //! LEA - Returns the index register operand. 105 TRITON_EXPORT triton::arch::Register& getIndexRegister(void); 106 107 //! LEA - Returns the displacement operand. 108 TRITON_EXPORT triton::arch::Immediate& getDisplacement(void); 109 110 //! LEA - Returns the scale operand. 111 TRITON_EXPORT triton::arch::Immediate& getScale(void); 112 113 //! LEA - Returns the segment register operand. 114 TRITON_EXPORT const triton::arch::Register& getConstSegmentRegister(void) const; 115 116 //! LEA - Returns the base register operand. 117 TRITON_EXPORT const triton::arch::Register& getConstBaseRegister(void) const; 118 119 //! LEA - Returns the index register operand. 120 TRITON_EXPORT const triton::arch::Register& getConstIndexRegister(void) const; 121 122 //! LEA - Returns the displacement operand. 123 TRITON_EXPORT const triton::arch::Immediate& getConstDisplacement(void) const; 124 125 //! LEA - Returns the scale operand. 126 TRITON_EXPORT const triton::arch::Immediate& getConstScale(void) const; 127 128 //! Returns true if `other` and `self` overlap. 129 TRITON_EXPORT bool isOverlapWith(const MemoryAccess& other) const; 130 131 //! Sets the address of the memory access. 132 TRITON_EXPORT void setAddress(triton::uint64 addr); 133 134 //! LEA - Sets pc relative. 135 TRITON_EXPORT void setPcRelative(triton::uint64 addr); 136 137 //! LEA - Sets the segment register operand. 138 TRITON_EXPORT void setSegmentRegister(const triton::arch::Register& segment); 139 140 //! LEA - Sets the base register operand. 141 TRITON_EXPORT void setBaseRegister(const triton::arch::Register& base); 142 143 //! LEA - Sets the index register operand. 144 TRITON_EXPORT void setIndexRegister(const triton::arch::Register& index); 145 146 //! LEA - Sets the displacement operand. 147 TRITON_EXPORT void setDisplacement(const triton::arch::Immediate& displacement); 148 149 //! LEA - Sets the scale operand. 150 TRITON_EXPORT void setScale(const triton::arch::Immediate& scale); 151 152 //! Sets the AST of the memory access (LEA). 153 TRITON_EXPORT void setLeaAst(const triton::ast::SharedAbstractNode& ast); 154 155 //! Copies a MemoryAccess. 156 TRITON_EXPORT MemoryAccess& operator=(const MemoryAccess& other); 157 }; 158 159 //! Displays an MemoryAccess. 160 TRITON_EXPORT std::ostream& operator<<(std::ostream& stream, const MemoryAccess& mem); 161 162 //! Displays an MemoryAccess. 163 TRITON_EXPORT std::ostream& operator<<(std::ostream& stream, const MemoryAccess* mem); 164 165 //! Compares two MemoryAccess. 166 TRITON_EXPORT bool operator==(const MemoryAccess& mem1, const MemoryAccess& mem2); 167 168 //! Compares two MemoryAccess. 169 TRITON_EXPORT bool operator!=(const MemoryAccess& mem1, const MemoryAccess& mem2); 170 171 //! Compares two MemoryAccess (needed for std::map) 172 TRITON_EXPORT bool operator<(const MemoryAccess& mem1, const MemoryAccess& mem2); 173 174 //! Defines the force memory initialization constant. 175 const bool FORCE_MEMORY_INITIALIZATION = true; 176 177 /*! @} End of arch namespace */ 178 }; 179 /*! @} End of triton namespace */ 180 }; 181 182 #endif /* TRITON_MEMORYACCESS_HPP */ 183