Skip to content

Commit

Permalink
Add instructionAPI/InstructionAST.h
Browse files Browse the repository at this point in the history
  • Loading branch information
hainest committed Apr 3, 2024
1 parent a5b2e66 commit fad9801
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 94 deletions.
123 changes: 66 additions & 57 deletions docs/instructionAPI/public/InstructionAST.h.rst
Original file line number Diff line number Diff line change
@@ -1,89 +1,98 @@
.. _`sec:InstructionAST.h`:

InstructionAST.h
================
################

.. cpp:namespace:: Dyninst::InstructionAPI

InstructionAST Class
--------------------
.. cpp:enum:: formatStyle

The InstructionAST class is the base class for all nodes in the ASTs
used by the Operand class. It defines the necessary interfaces for
traversing and searching an abstract syntax tree representing an
operand. For the purposes of searching an InstructionAST, we provide two
related interfaces. The first, ``getUses``, will return the registers
that appear in a given tree. The second, ``isUsed``, will take as input
another tree and return true if that tree is a (not necessarily proper)
subtree of this one. ``isUsed`` requires us to define an equality
relation on these abstract syntax trees, and the equality operator is
provided by the InstructionAST, with the details implemented by the
classes derived from InstructionAST. Two AST nodes are equal if the
following conditions hold:
.. cpp:enumerator:: defaultStyle
.. cpp:enumerator:: memoryAccessStyle

- They are of the same type
.. cpp:class:: InstructionAST : public boost::enable_shared_from_this<InstructionAST>

- If leaf nodes, they represent the same immediate value or the same
register
**The base class for all nodes in the ASTs used by the Operand class**

- If non-leaf nodes, they represent the same operation and their
corresponding children are equal
.. cpp:type:: boost::shared_ptr<InstructionAST> Ptr

.. code-block:: cpp
A reference-counted pointer to an ``InstructionAST``.

typedef boost::shared_ptr<InstructionAST> Ptr
.. cpp:function:: bool operator==(const InstructionAST &rhs) const

A type definition for a reference-counted pointer to an
``InstructionAST``.
Checks if this :cpp:class::`AST` node is equal to ``rhs``.

.. code-block:: cpp
bool operator==(const InstructionAST &rhs) const
Non-leaf nodes are equal if they are of the same type and their children are equal.

Compare two AST nodes for equality.
.. cpp:function:: virtual void getChildren(vector<InstructionAST::Ptr> & children) const

Non-leaf nodes are equal if they are of the same type and their children
are equal. ``RegisterAST``\ s are equal if they represent the same
register. ``Immediate``\ s are equal if they represent the same value.
Note that it is not safe to compare two ``InstructionAST::Ptr``
variables, as those are pointers. Instead, test the underlying
``InstructionAST`` objects.
Children of this node are appended to the vector ``children``.

.. code-block:: cpp
.. cpp:function:: virtual void getUses(set<InstructionAST::Ptr> & uses)

virtual void getChildren(vector<InstructionAPI::Ptr> & children) const
Appends the set of expressions used.

Children of this node are appended to the vector ``children``.
See :ref:`sec:instructionast-use-sets` for details.

.. code-block:: cpp
.. cpp:function:: virtual bool isUsed(InstructionAST::Ptr i) const

virtual void getUses(set<InstructionAPI::Ptr> & uses)
Checks if ``i`` is used by this node.

The use set of an ``InstructionAST`` is defined as follows:
Unlike :cpp:func:`getUses`, this looks for ``i`` as a subtree of the
current tree. ``getUses`` is designed to return a minimal set of
registers used in this tree, whereas ``isUsed`` is designed to allow
searches for arbitrary subexpressions.

See :ref:`sec:instructionast-use-sets` for details.

.. cpp:function:: virtual std::string format(formatStyle how = defaultStyle) const

Returns a string representation of the :cpp:class:`AST` node.

- A ``RegisterAST`` uses itself
Produces assembly language by default.

- A ``BinaryFunction`` uses the use sets of its children
virtual std::string format(Architecture arch, formatStyle how = defaultStyle) const = 0

- A ``Immediate`` uses nothing
.. cpp:function:: protected virtual bool isStrictEqual(const InstructionAST& rhs) const= 0
.. cpp:function:: protected virtual bool checkRegID(MachRegister, unsigned int = 0, unsigned int = 0) const
.. cpp:function:: protected virtual const Result& eval() const = 0

- A ``Dereference uses the use set of its child``
.. _`sec:instructionast-notes`:

The use set oft his node is appended to the vector ``uses``.
Notes
=====

.. code-block:: cpp
InstructionAST defines the necessary interfaces for
traversing and searching an abstract syntax tree representing an
operand. For the purposes of searching an InstructionAST, we provide two
related interfaces. The first, ``getUses``, will return the registers
that appear in a given tree. The second, ``isUsed``, will take as input
another tree and return true if that tree is a (not necessarily proper)
subtree of this one. ``isUsed`` requires us to define an equality
relation on these abstract syntax trees, and the equality operator is
provided by the InstructionAST, with the details implemented by the
classes derived from InstructionAST. Two AST nodes are equal if the
following conditions hold:

- They are of the same type

- If leaf nodes, they represent the same immediate value or the same
register

virtual bool isUsed(InstructionAPI::Ptr findMe) const
- If non-leaf nodes, they represent the same operation and their
corresponding children are equal

Unlike ``getUses``, ``isUsed`` looks for ``findMe`` as a subtree of the
current tree. ``getUses`` is designed to return a minimal set of
registers used in this tree, whereas ``isUsed`` is designed to allow
searches for arbitrary subexpressions. ``findMe`` is the AST node to
find in the use set of this node.
.. _`sec:instructionast-use-sets`:

Use Sets
^^^^^^^^

The use set of an ``InstructionAST`` is defined as follows:

Returns ``true`` if ``findMe`` is used by this AST node.
- A ``RegisterAST`` uses itself

.. code-block:: cpp
- A ``BinaryFunction`` uses the use sets of its children

virtual std::string format(formatStyle how == defaultStyle) const
- A ``Immediate`` uses nothing

The ``format`` interface returns the contents of an ``InstructionAPI``
object as a string. By default, ``format`` produces assembly language.
- A ``Dereference`` uses the use set of its child
38 changes: 1 addition & 37 deletions instructionAPI/h/InstructionAST.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,7 @@ namespace Dyninst
defaultStyle,
memoryAccessStyle
};
/// The %InstructionAST class is the base class for all nodes in the ASTs used by the %Operand class.
/// It defines the necessary interfaces for traversing and searching
/// an abstract syntax tree representing an operand.
/// For the purposes of searching an %InstructionAST, we provide two related interfaces. The first,
/// \c getUses, will return the registers that appear in a given tree. The second, \c isUsed, will
/// take as input another tree and return true if that tree is a (not necessarily proper) subtree of this one.
/// \c isUsed requires us to define an equality relation on these abstract
/// syntax trees, and the equality operator is provided by the %InstructionAST, with the details
/// implemented by the classes derived from %InstructionAST. Two AST nodes are equal if the following conditions hold:
/// - They are of the same type
/// - If leaf nodes, they represent the same immediate value or the same register
/// - If non-leaf nodes, they represent the same operation and their corresponding children are equal

class INSTRUCTION_EXPORT InstructionAST : public boost::enable_shared_from_this<InstructionAST>
{
public:
Expand All @@ -76,41 +65,16 @@ namespace Dyninst
InstructionAST(const InstructionAST&) = default;
virtual ~InstructionAST();

/// Compare two AST nodes for equality.
///
/// Non-leaf nodes are equal
/// if they are of the same type and their children are equal. %RegisterASTs
/// are equal if they represent the same register. %Immediates are equal if they
/// represent the same value.
bool operator==(const InstructionAST& rhs) const;

/// Children of this node are appended to the vector \c children
virtual void getChildren(vector<InstructionAST::Ptr>& children) const = 0;

/// \param uses The use set of this node is appended to the vector \c uses
///
/// The use set of an %InstructionAST is defined as follows:
/// - A %RegisterAST uses itself
/// - A %BinaryFunction uses the use sets of its children
/// - An %Immediate uses nothing
/// - A %Dereference uses the use set of its child
virtual void getUses(set<InstructionAST::Ptr>& uses) = 0;

/// \return True if \c findMe is used by this AST node.
/// \param findMe AST node to find in the use set of this node
///
/// Unlike \c getUses, \c isUsed looks for \c findMe as a subtree
/// of the current tree. \c getUses is designed to return a minimal
/// set of registers used in this tree, whereas \c isUsed is designed
/// to allow searches for arbitrary subexpressions
virtual bool isUsed(InstructionAST::Ptr findMe) const = 0;

/// The \c format interface returns the contents of an %InstructionAST
/// object as a string. By default, \c format() produces assembly language.
virtual std::string format(Architecture arch, formatStyle how = defaultStyle) const = 0;

/// The \c format interface returns the contents of an %InstructionAST
/// object as a string. By default, \c format() produces assembly language.
virtual std::string format(formatStyle how = defaultStyle) const = 0;

protected:
Expand Down

0 comments on commit fad9801

Please sign in to comment.