1 #ifndef ROSE_BinaryAnalysis_SymbolicExprParser_H
2 #define ROSE_BinaryAnalysis_SymbolicExprParser_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
6 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics.h>
7 #include <Rose/BinaryAnalysis/SymbolicExpr.h>
8 #include <Rose/Exception.h>
9 #include <Sawyer/BiMap.h>
10 #include <Sawyer/CommandLine.h>
11 #include <Sawyer/Message.h>
12 #include <Sawyer/SharedPointer.h>
15 namespace BinaryAnalysis {
39 SyntaxError(
const std::string &mesg,
const std::string &inputName,
unsigned lineNumber,
unsigned columnNumber);
41 void print(std::ostream&)
const;
50 void print(std::ostream&)
const;
72 unsigned lineNumber_, columnNumber_;
77 : tokenType_(
NONE), lineNumber_(0), columnNumber_(0) {}
82 : tokenType_(tokenType), lexeme_(lexeme), exprType_(exprType),
83 lineNumber_(lineNumber), columnNumber_(columnNumber) {
90 : tokenType_(
BITVECTOR), lexeme_(lexeme), exprType_(exprType), bits_(bv),
91 lineNumber_(lineNumber), columnNumber_(columnNumber) {
92 ASSERT_require(exprType.
nBits() == bv.
size());
104 const std::string &
lexeme()
const {
return lexeme_; }
125 std::istream &input_;
127 unsigned lineNumber_, columnNumber_;
128 const Token endToken_;
129 std::vector<Token> tokens_;
144 const std::string&
name()
const {
return name_; }
156 void shift(
size_t n=1);
200 void fillTokenList(
size_t idx);
207 std::string docString_;
217 const std::string&
title()
const {
return title_; }
218 void title(
const std::string &s) { title_ = s; }
226 const std::string&
docString()
const {
return docString_; }
227 void docString(
const std::string &s) { docString_ = s; }
331 RegToVarMap reg2var_;
336 : regdict_(regdict) {}
375 ExprToMem exprToMem_;
420 NameToVarMap name2var_;
430 const NameToVarMap&
map()
const {
return name2var_; }
451 static Ptr instance() {
459 static std::string docString();
472 AtomTable atomTable_;
473 OperatorTable operatorTable_;
491 static void initDiagnostics();
503 unsigned lineNumber=1,
unsigned columnNumber=0);
519 const AtomTable&
atomTable()
const {
return atomTable_; }
559 std::ostream& operator<<(std::ostream&,
const SymbolicExprParser::SyntaxError&);
560 std::ostream& operator<<(std::ostream&,
const SymbolicExprParser::SubstitutionError&);
const OperatorTable & operatorTable() const
Return all operator expansion functors.
Virtual base class for atom and operator expansion.
SmtSolverPtr Ptr
Reference counting pointer for SMT solvers.
const Token & operator[](size_t idx)
Returns the specified token without consuming it.
SymbolicExpr::Ptr immediateExpansion(const SymbolicExprParser::Token &) override
Expand a parsed atom into some other expression.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Sawyer::SharedPointer< Node > Ptr
Shared-ownership pointer to an expression Node.
size_t nBits() const
Property: Total width of values.
const ValueSaver::Ptr valueSaver() const
Property: functor responsible for saving a parsed value in user storage.
SymbolicExpr::Ptr delayedExpansion(const SymbolicExpr::Ptr &, const SymbolicExprParser *) override
Substitute one expression with another.
virtual SymbolicExpr::Ptr immediateExpansion(const Token &name)=0
Expand a parsed atom into some other expression.
void shift(size_t n=1)
Consume the specified number of tokens.
Expand register name to placeholder variables.
SymbolicExpr::Ptr delayedExpansion(const SymbolicExpr::Ptr &) const
Perform delayed expansion.
int nextCharacter()
Returns the next character.
static Ptr instance(const RegisterDictionary *)
Allocating constructor.
Token(const Sawyer::Container::BitVector &bv, const SymbolicExpr::Type &exprType, const std::string &lexeme, unsigned lineNumber, unsigned columnNumber)
Construct a token for a numeric constant.
SymbolicExpr::Type consumeType()
Parse and consume a type specification.
unsigned lineNumber() const
Line number for start of token.
static Ptr instance()
Allocating constructor.
std::string inputName
Name of input, usually a file name.
Sawyer::SharedPointer< SymbolicExprCmdlineParser > Ptr
Shared-ownership pointer.
OperatorTable & operatorTable()
Return all operator expansion functors.
unsigned columnNumber() const
Current column number.
virtual SymbolicExpr::Ptr delayedExpansion(const SymbolicExpr::Ptr &src, const SymbolicExprParser *parser)
Substitute one expression with another.
std::vector< OperatorExpansion::Ptr > OperatorTable
Ordered operator table.
Tokens generated by the lexical analysis.
Type of symbolic expression.
Sawyer::SharedPointer< AtomExpansion > Ptr
Shared-ownership pointer to an AtomExpansion.
void consumeInlineComment()
Skip over angle-bracket comments.
SymbolicExpr::Ptr immediateExpansion(const SymbolicExprParser::Token &) override
Expand a parsed atom into some other expression.
Virtual base class for expanding atoms.
Main namespace for the ROSE library.
Parses symbolic expressions from text.
SymbolicExpr::Type exprType() const
Type of expression.
int consumeCharacter()
Consume the next character.
unsigned columnNumber
Column number (0-origin).
virtual SymbolicExpr::Ptr delayedExpansion(const SymbolicExpr::Ptr &, const SymbolicExprParser *) override
Substitute one expression with another.
virtual SymbolicExpr::Ptr immediateExpansion(const Token &name, const SymbolicExpr::Nodes &operands) override
Operator to expand a list into an expression tree.
Reference-counting intrusive smart pointer.
const std::string & name() const
Name of this input stream.
Token(Type tokenType, const SymbolicExpr::Type &exprType, const std::string &lexeme, unsigned lineNumber, unsigned columnNumber)
Constructs a specific token from a string.
Token()
Constructs an end-of-input token with no position information.
Token scan()
Parse and consume the next token.
Virtual base class for expanding operators.
void docString(const std::string &s)
Property: Documentation string.
size_t size() const
Size of vector in bits.
Information about a parsed switch value.
AtomTable & atomTable()
Return all atom expansion functors.
Expand register names to register values.
virtual SymbolicExpr::Ptr immediateExpansion(const Token &name, const SymbolicExpr::Nodes &operands)=0
Operator to expand a list into an expression tree.
std::string consumeTerm()
Parse and consume a term.
SymbolicExprParser()
Default constructor.
const std::string & lexeme() const
Lexeme from which token was parsed.
Sawyer::SharedPointer< OperatorExpansion > Ptr
Shared-ownership pointer.
void consumeWhiteSpace()
Skip over characters until a non-white-space character is encountered.
SymbolicExpr::Ptr immediateExpansion(const SymbolicExprParser::Token &) override
Expand a parsed atom into some other expression.
void defineRegisters(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &)
Add definitions for registers.
Sawyer::SharedPointer< RegisterSubstituter > Ptr
Shared-ownership pointer.
SymbolicExprParser::SyntaxError syntaxError(const std::string &mesg, const std::string &name="input") const
Creates a syntax error from a token plus message.
unsigned lineNumber() const
Current line number.
const std::string & docString() const
Property: Documentation string.
void title(const std::string &s)
Property: Title to use for documentation.
Expand unrecognized terms to placholder variables.
std::string docString() const
Documentation string.
void print(std::ostream &) const
Print error message to stream.
void appendOperatorExpansion(const OperatorExpansion::Ptr &)
Append a new functor for expanding operators into symbolic expressions.
Expand "memory" function to placeholder.
Sawyer::Container::BiMap< std::string, SymbolicExpr::Ptr > NameToVarMap
Mapping between term names and placeholder variables.
Sawyer::SharedPointer< TermPlaceholders > Ptr
Shared-ownership pointer.
Base class for reference counted objects.
Syntax errors that are thrown.
void consumeWhiteSpaceAndComments()
Skip over white space and/or inline comments.
InstructionSemantics2::BaseSemantics::RiscOperatorsPtr riscOperators() const
Property: Semantic state used during delayed expansion.
int consumeEscapeSequence()
Skip over an escape sequence and return the escaped character.
std::vector< AtomExpansion::Ptr > AtomTable
Ordered atom table.
Position within a command-line.
Sawyer::SharedPointer< Expansion > Ptr
Shared-ownership pointer to an Expansion.
unsigned lineNumber
Line number (1-origin).
void riscOperators(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &ops)
Property: Semantic state used during delayed expansion.
Base class parsing a value from input.
const std::string & title() const
Property: Title to use for documentation.
Type tokenType() const
Token type.
InstructionSemantics2::BaseSemantics::RiscOperatorsPtr riscOperators() const
Property: Semantic state used during delayed expansion.
const Sawyer::Container::BitVector & bits() const
Bit vector for numeric constants.
void riscOperators(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &ops)
Property: Semantic state used during delayed expansion.
Defines registers available for a particular architecture.
TokenStream(std::istream &input, const std::string &name="input", unsigned lineNumber=1, unsigned columnNumber=0)
Scan tokens from a character stream.
Sawyer::SharedPointer< RegisterToValue > Ptr
Shared-ownership pointer.
Parse a symbolic expression from the command-line.
static Ptr instance(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &)
Allocating constructor.
Substitution error during expansion.
static Ptr instance(const SmtSolver::Ptr &solver)
Allocating constructor.
Base class for all ROSE exceptions.
Sawyer::SharedPointer< MemorySubstituter > Ptr
Shared-ownership pointer.
unsigned columnNumber() const
Column number for start of token.
SymbolicExpr::Ptr parse(const std::string &, const std::string &inputName="string")
Create a symbolic expression by parsing a string.
Container associating values with keys.
const NameToVarMap & map() const
Mapping between terms and variables.
std::shared_ptr< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
void appendAtomExpansion(const AtomExpansion::Ptr &)
Append a new functor for expanding atoms into symbolic expressions.
const AtomTable & atomTable() const
Return all atom expansion functors.