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