From 5406b530b70c9b4426137dfe880d53631b83026a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Gy=C3=B6r=C3=B6k?= Date: Sat, 7 Jan 2017 08:03:25 +0100 Subject: [PATCH] add some useful functions --- src/libtriton/arch/instruction.cpp | 43 +++++++++++++++++++++++ src/libtriton/arch/operandWrapper.cpp | 25 +++++++++++++ src/libtriton/includes/instruction.hpp | 6 ++++ src/libtriton/includes/operandWrapper.hpp | 6 ++++ 4 files changed, 80 insertions(+) diff --git a/src/libtriton/arch/instruction.cpp b/src/libtriton/arch/instruction.cpp index d3eec85ed..8d9e631b2 100644 --- a/src/libtriton/arch/instruction.cpp +++ b/src/libtriton/arch/instruction.cpp @@ -286,6 +286,49 @@ namespace triton { } + bool Instruction::isReadFrom(const triton::arch::OperandWrapper &target) const { + switch(target.getType()) { + case triton::arch::OP_IMM: + for (auto pair : readImmediates) { + if (pair.first == target.getConstImmediate()) return true; + } + return false; + case triton::arch::OP_MEM: + for (auto pair : loadAccess) { + if (pair.first.getAddress() == target.getConstMemory().getAddress()) return true; + } + return false; + case triton::arch::OP_REG: + for (auto pair : readRegisters) { + if (pair.first.getId() == target.getConstRegister().getId()) return true; + } + return false; + default: + throw triton::exceptions::Instruction("Instruction::isReadFrom(): Invalid type operand."); + } + } + + + bool Instruction::isWriteTo(const triton::arch::OperandWrapper &target) const { + switch(target.getType()) { + case triton::arch::OP_IMM: + return false; + case triton::arch::OP_MEM: + for (auto pair : storeAccess) { + if (pair.first.getAddress() == target.getConstMemory().getAddress()) return true; + } + return false; + case triton::arch::OP_REG: + for (auto pair : writtenRegisters) { + if (pair.first.getId() == target.getConstRegister().getId()) return true; + } + return false; + default: + throw triton::exceptions::Instruction("Instruction::isWriteTo(): Invalid type operand."); + } + } + + bool Instruction::isPrefixed(void) const { if (this->prefix) return true; diff --git a/src/libtriton/arch/operandWrapper.cpp b/src/libtriton/arch/operandWrapper.cpp index 1923cb5d6..42509e77b 100644 --- a/src/libtriton/arch/operandWrapper.cpp +++ b/src/libtriton/arch/operandWrapper.cpp @@ -152,6 +152,31 @@ namespace triton { this->type = other.type; } + bool OperandWrapper::operator==(const OperandWrapper& other) const { + if (this->type != other.type) + return false; + switch (this->getType()) { + case triton::arch::OP_IMM: return this->imm == other.imm; + case triton::arch::OP_MEM: return this->mem == other.mem; + case triton::arch::OP_REG: return this->reg == other.reg; + default: + throw triton::exceptions::OperandWrapper("OperandWrapper::operator==(): Invalid type operand."); + } + } + + bool OperandWrapper::operator<(const OperandWrapper& other) const { + if (this->type < other.type) + return true; + if (this->type > other.type) + return false; + switch (this->getType()) { + case triton::arch::OP_IMM: return this->imm < other.imm; + case triton::arch::OP_MEM: return this->mem < other.mem; + case triton::arch::OP_REG: return this->reg < other.reg; + default: + throw triton::exceptions::OperandWrapper("OperandWrapper::operator==(): Invalid type operand."); + } + } std::ostream& operator<<(std::ostream& stream, const triton::arch::OperandWrapper& op) { switch (op.getType()) { diff --git a/src/libtriton/includes/instruction.hpp b/src/libtriton/includes/instruction.hpp index 57749c42e..1adae6ae1 100644 --- a/src/libtriton/includes/instruction.hpp +++ b/src/libtriton/includes/instruction.hpp @@ -238,6 +238,12 @@ namespace triton { //! Returns true if the instruction contains an expression which writes into the memory. bool isMemoryWrite(void) const; + //! Returns whether the instruction writes the specified operand. + bool isWriteTo(const triton::arch::OperandWrapper &target) const; + + //! Returns whether the instruction reads the specified operand. + bool isReadFrom(const triton::arch::OperandWrapper &target) const; + //! Returns true if the instruction has a prefix. bool isPrefixed(void) const; diff --git a/src/libtriton/includes/operandWrapper.hpp b/src/libtriton/includes/operandWrapper.hpp index 59f845c38..d56a2414c 100644 --- a/src/libtriton/includes/operandWrapper.hpp +++ b/src/libtriton/includes/operandWrapper.hpp @@ -107,6 +107,12 @@ namespace triton { //! Copies a OperandWrapper. void operator=(const OperandWrapper& other); + + //! Tests two OperandWrappers for equality. + bool operator==(const OperandWrapper& other) const; + + //! Compares two OperandWrappers for ordering. + bool operator<(const OperandWrapper& other) const; }; //! Displays a OperandWrapper according to the concrete type.