Skip to content

Commit

Permalink
Add new AST MultiRegisterAST to instructionAPI
Browse files Browse the repository at this point in the history
This commit introduce a new ast that denotes the use of multiple
registers as a single operand.

For now, the visitor for such a class
is basically visiting the registers contained in it, in order.

The constructor of the Expression class has been restructured to
avoid redundant implementation.
  • Loading branch information
bbiiggppiigg committed Apr 23, 2024
1 parent 6ef6687 commit 818d82a
Show file tree
Hide file tree
Showing 25 changed files with 452 additions and 6 deletions.
9 changes: 9 additions & 0 deletions dataflowAPI/src/AbslocInterface.C
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "AbslocInterface.h"

#include "Register.h"
#include "MultiRegister.h"
// Pile of InstructionAPI includes
#include "Expression.h"
#include "Result.h"
Expand Down Expand Up @@ -266,6 +267,14 @@ public:
defined = false;
results.push_back(0);
}
virtual void visit(MultiRegisterAST* r)
{
for (auto my_Reg : r->getRegs() ){
visit(my_Reg.get());
}
// TODO
}

virtual void visit(Dereference* )
{
//defined = false;
Expand Down
9 changes: 9 additions & 0 deletions dataflowAPI/src/ExpressionConversionVisitor.C
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <list>

#include "Register.h"
#include "MultiRegister.h"
#include "../rose/SgAsmExpression.h"

using namespace Dyninst;
Expand Down Expand Up @@ -160,6 +161,14 @@ void ExpressionConversionVisitor::visit(RegisterAST *regast) {
return;
}

void ExpressionConversionVisitor::visit(MultiRegisterAST *multiregast) {
// has no children
for (auto my_Reg : multiregast->getRegs() ){
visit(my_Reg.get());
}
return;
}

void ExpressionConversionVisitor::visit(Dereference *deref) {
// get child
assert(m_stack.size());
Expand Down
1 change: 1 addition & 0 deletions dataflowAPI/src/ExpressionConversionVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace Dyninst
DATAFLOW_EXPORT virtual void visit(InstructionAPI::BinaryFunction *binfunc);
DATAFLOW_EXPORT virtual void visit(InstructionAPI::Immediate *immed);
DATAFLOW_EXPORT virtual void visit(InstructionAPI::RegisterAST *regast);
DATAFLOW_EXPORT virtual void visit(InstructionAPI::MultiRegisterAST *multiregast);
DATAFLOW_EXPORT virtual void visit(InstructionAPI::Dereference *deref);

private:
Expand Down
6 changes: 6 additions & 0 deletions dataflowAPI/src/stackanalysis.C
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "instructionAPI/h/InstructionDecoder.h"
#include "instructionAPI/h/Instruction.h"
#include "instructionAPI/h/Register.h"
#include "instructionAPI/h/MultiRegister.h"
#include "instructionAPI/h/Result.h"
#include "parseAPI/h/CFG.h"
#include "parseAPI/h/CodeObject.h"
Expand Down Expand Up @@ -1119,7 +1120,12 @@ public:
defined = false;
}
}
virtual void visit(MultiRegisterAST *multirast) {
if (!defined) return;
for(auto my_Reg : multirast->getRegs())
visit(my_Reg.get());

}
virtual void visit(Dereference *) {
defined = false;
}
Expand Down
7 changes: 7 additions & 0 deletions dyninstAPI/src/BPatch_memoryAccessAdapter.C
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "Instruction.h"
#include "Immediate.h"
#include "Register.h"
#include "MultiRegister.h"
#include "Dereference.h"

#include "common/src/arch.h"
Expand Down Expand Up @@ -298,6 +299,12 @@ void BPatch_memoryAccessAdapter::visit(RegisterAST* r)
}
#endif
}
void BPatch_memoryAccessAdapter::visit(MultiRegisterAST* mr)
{
for (auto my_Reg : mr->getRegs())
visit(my_Reg.get());
}


void BPatch_memoryAccessAdapter::visit(Immediate* i)
{
Expand Down
1 change: 1 addition & 0 deletions dyninstAPI/src/BPatch_memoryAccessAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class BPatch_memoryAccessAdapter : public Dyninst::InstructionAPI::Visitor
virtual void visit(Dyninst::InstructionAPI::BinaryFunction* b);
virtual void visit(Dyninst::InstructionAPI::Dereference* d);
virtual void visit(Dyninst::InstructionAPI::RegisterAST* r);
virtual void visit(Dyninst::InstructionAPI::MultiRegisterAST* mr);
virtual void visit(Dyninst::InstructionAPI::Immediate* i);
private:
unsigned int bytes;
Expand Down
7 changes: 7 additions & 0 deletions dyninstAPI/src/IAPI_to_AST.C
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "IAPI_to_AST.h"

#include "Register.h"
#include "MultiRegister.h"
#include "BinaryFunction.h"
#include "Immediate.h"
#include "Dereference.h"
Expand Down Expand Up @@ -93,3 +94,9 @@ void ASTFactory::visit(RegisterAST* r)
(void*)(astreg)));
#endif
}
void ASTFactory::visit(MultiRegisterAST* r)
{
for (auto my_Reg : r->getRegs())
visit(my_Reg.get());
}

1 change: 1 addition & 0 deletions dyninstAPI/src/IAPI_to_AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ASTFactory : public Dyninst::InstructionAPI::Visitor
virtual void visit(Dyninst::InstructionAPI::Dereference* d);
virtual void visit(Dyninst::InstructionAPI::Immediate* i);
virtual void visit(Dyninst::InstructionAPI::RegisterAST* r);
virtual void visit(Dyninst::InstructionAPI::MultiRegisterAST* r);
std::deque<AstNodePtr> m_stack;
virtual ~ASTFactory() {}
ASTFactory() {}
Expand Down
7 changes: 7 additions & 0 deletions dyninstAPI/src/Relocation/Transformers/Movement-adhoc.C
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "instructionAPI/h/InstructionDecoder.h"
#include "../CFG/RelocGraph.h"
#include "instructionAPI/h/Visitor.h"
#include "instructionAPI/h/MultiRegister.h"

#include "StackMod.h"
#include "function.h"
Expand Down Expand Up @@ -383,6 +384,12 @@ public:
foundSP = true;
}
}
virtual void visit(MultiRegisterAST* r)
{
for (auto my_Reg : r->getRegs())
visit(my_Reg.get());
}

virtual void visit(Dereference* )
{
if (foundDeref) {
Expand Down
13 changes: 13 additions & 0 deletions dyninstAPI/src/StackMod/StackAccess.C
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "InstructionCategories.h"
#include "InstructionDecoder.h"
#include "Expression.h"
#include "MultiRegister.h"
#include "Result.h"
#include "Dereference.h"
#include "Immediate.h"
Expand Down Expand Up @@ -224,6 +225,11 @@ public:
containsToppedReg = true;
results.push_back((long) top);
}
virtual void visit(InstructionAPI::MultiRegisterAST *r) {
for (auto my_Reg : r->getRegs())
visit(my_Reg.get());
}


virtual void visit(InstructionAPI::Dereference *) {
defined = false;
Expand Down Expand Up @@ -586,6 +592,13 @@ class zeroAllGPRegisters : public InstructionAPI::Visitor

results.push_back(0);
}
virtual void visit(MultiRegisterAST* r)
{
for (auto my_Reg : r->getRegs())
visit(my_Reg.get());

}

virtual void visit(Dereference* )
{
defined = false;
Expand Down
2 changes: 2 additions & 0 deletions instructionAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(_sources
src/Operation.C
src/Operand.C
src/Register.C
src/MultiRegister.C
src/Ternary.C
src/Expression.C
src/BinaryFunction.C
Expand Down Expand Up @@ -51,6 +52,7 @@ set(_public_headers
h/Operand.h
h/Operation_impl.h
h/Register.h
h/MultiRegister.h
h/Result.h
h/Ternary.h
h/Visitor.h)
Expand Down
1 change: 1 addition & 0 deletions instructionAPI/h/ArchSpecificFormatters.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace Dyninst {
// Helper function for formatting consecutive registers that are displayed as a single operand
// Called when architecture is passed to Instruction.format.
static std::string formatRegister(MachRegister m_Reg, uint32_t num_elements, unsigned m_Low , unsigned m_High );
static std::string formatMultiRegister(MachRegister m_Reg, uint32_t size);
private:
std::map<std::string, std::string> binaryFuncModifier;
};
Expand Down
3 changes: 3 additions & 0 deletions instructionAPI/h/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ namespace Dyninst
typedef boost::shared_ptr<Expression> Ptr;
protected:
Expression(Result_Type t);
Expression(uint32_t total_size);
Expression(MachRegister r);
Expression(MachRegister r, uint32_t len);
Expression(std::vector<MachRegister> rs);
public:
virtual ~Expression();
Expression(const Expression&) = default;
Expand Down
1 change: 1 addition & 0 deletions instructionAPI/h/InstructionAST.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ namespace Dyninst
virtual std::string format(formatStyle how = defaultStyle) const = 0;

protected:
friend class MultiRegisterAST;
friend class RegisterAST;
friend class Immediate;
virtual bool isStrictEqual(const InstructionAST& rhs) const= 0;
Expand Down
111 changes: 111 additions & 0 deletions instructionAPI/h/MultiRegister.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* See the dyninst/COPYRIGHT file for copyright information.
*
* We provide the Paradyn Tools (below described as "Paradyn")
* on an AS IS basis, and do not warrant its validity or performance.
* We reserve the right to update, modify, or discontinue this
* software at any time. We shall have no obligation to supply such
* updates or modifications or any other form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#if !defined(MULTIREGISTER_H)
#define MULTIREGISTER_H

#include "Expression.h"
#include "Register.h"
#include <stdint.h>
#include <string>
#include <vector>
#include <map>
#include <sstream>

#include "registers/MachRegister.h"
#include "Architecture.h"

namespace Dyninst
{
namespace InstructionAPI
{
/// A %MultiRegisterAST object represents a ordered collection of registers as a single operand
/// As a %MultiRegisterAST is a %Expression, it may contain the physical register's contents if
/// they are known.
///


class INSTRUCTION_EXPORT MultiRegisterAST : public Expression
{
public:
/// \brief A type definition for a reference-counted pointer to a %MultiRegisterAST.
typedef boost::shared_ptr<MultiRegisterAST> Ptr;

/// Construct a register, assigning it the ID \c id.
MultiRegisterAST(MachRegister r, uint32_t num_elements = 1 );
MultiRegisterAST(std::vector<RegisterAST::Ptr> _in);

virtual ~MultiRegisterAST();
MultiRegisterAST(const MultiRegisterAST&) = default;

/// By definition, a %MultiRegisterAST object has no children.
/// \param children Since a %MultiRegisterAST has no children, the \c children parameter is unchanged by this method.
virtual void getChildren(vector<InstructionAST::Ptr>& children) const;
virtual void getChildren(vector<Expression::Ptr>& children) const;

/// By definition, the use set of a %MultiRegisterAST object is itself.
/// \param uses This %MultiRegisterAST will be inserted into \c uses.
virtual void getUses(set<InstructionAST::Ptr>& uses);

/// \c isUsed returns true if \c findMe is a %MultiRegisterAST that represents
/// the same register as this %MultiRegisterAST, and false otherwise.
virtual bool isUsed(InstructionAST::Ptr findMe) const;

/// The \c format method on a %MultiRegisterAST object returns the name associated with its ID.
virtual std::string format(Architecture, formatStyle how = defaultStyle) const;
/// The \c format method on a %MultiRegisterAST object returns the name associated with its ID.
virtual std::string format(formatStyle how = defaultStyle) const;

/// We define a partial ordering on registers by their register number so that they may be placed into sets
/// or other sorted containers.
// For multiRegisters, the partial order is determined by baseReg ID, followed by length
bool operator<(const MultiRegisterAST& rhs) const;

virtual void apply(Visitor* v);
virtual bool bind(Expression* e, const Result& val);

RegisterAST::Ptr getBaseRegAST() const { return m_Regs[0]; }
uint32_t length() const { return m_Regs.size(); }
const std::vector<RegisterAST::Ptr> getRegs() const { return m_Regs; }
protected:

virtual bool checkRegID(MachRegister id, unsigned int low, unsigned int high) const;
virtual bool isStrictEqual(const InstructionAST& rhs) const;
virtual bool isFlag() const;

std::vector<RegisterAST::Ptr> m_Regs;
};
}
}




#endif // !defined(MULTIREGISTER_H)
3 changes: 2 additions & 1 deletion instructionAPI/h/Register.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Dyninst

class INSTRUCTION_EXPORT RegisterAST : public Expression
{
friend class MultiRegisterAST;
public:
/// \brief A type definition for a reference-counted pointer to a %RegisterAST.
typedef boost::shared_ptr<RegisterAST> Ptr;
Expand Down Expand Up @@ -108,8 +109,8 @@ namespace Dyninst
virtual void apply(Visitor* v);
virtual bool bind(Expression* e, const Result& val);

protected:
virtual bool isStrictEqual(const InstructionAST& rhs) const;
protected:
virtual bool isFlag() const;
virtual bool checkRegID(MachRegister id, unsigned int low, unsigned int high) const;
MachRegister getPromotedReg() const;
Expand Down
2 changes: 2 additions & 0 deletions instructionAPI/h/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace InstructionAPI
class BinaryFunction;
class Immediate;
class RegisterAST;
class MultiRegisterAST;
class Dereference;

class Visitor
Expand All @@ -57,6 +58,7 @@ namespace InstructionAPI
virtual void visit(BinaryFunction* b) = 0;
virtual void visit(Immediate* i) = 0;
virtual void visit(RegisterAST* r) = 0;
virtual void visit(MultiRegisterAST* r) = 0;
virtual void visit(Dereference* d) = 0;
};
}
Expand Down

0 comments on commit 818d82a

Please sign in to comment.