1 #ifndef ROSE_BinaryAnalysis_SymbolicExprParser_H
2 #define ROSE_BinaryAnalysis_SymbolicExprParser_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
6 #include <BaseSemantics2.h>
7 #include <BinarySymbolicExpr.h>
8 #include <Sawyer/BiMap.h>
9 #include <Sawyer/CommandLine.h>
10 #include <Sawyer/Message.h>
11 #include <Sawyer/SharedPointer.h>
38 SyntaxError(
const std::string &mesg,
const std::string &inputName,
unsigned lineNumber,
unsigned columnNumber);
40 void print(std::ostream&)
const;
47 : std::runtime_error(mesg) {}
49 void print(std::ostream&)
const;
71 unsigned lineNumber_, columnNumber_;
76 : tokenType_(
NONE), lineNumber_(0), columnNumber_(0) {}
81 : tokenType_(tokenType), lexeme_(lexeme), exprType_(exprType),
82 lineNumber_(lineNumber), columnNumber_(columnNumber) {
89 : tokenType_(
BITVECTOR), lexeme_(lexeme), exprType_(exprType), bits_(bv),
90 lineNumber_(lineNumber), columnNumber_(columnNumber) {
91 ASSERT_require(exprType.
nBits() == bv.
size());
103 const std::string &
lexeme()
const {
return lexeme_; }
124 std::istream &input_;
126 unsigned lineNumber_, columnNumber_;
127 const Token endToken_;
128 std::vector<Token> tokens_;
143 const std::string&
name()
const {
return name_; }
155 void shift(
size_t n=1);
199 void fillTokenList(
size_t idx);
206 std::string docString_;
216 const std::string&
title()
const {
return title_; }
217 void title(
const std::string &s) { title_ = s; }
225 const std::string&
docString()
const {
return docString_; }
226 void docString(
const std::string &s) { docString_ = s; }
330 RegToVarMap reg2var_;
335 : regdict_(regdict) {}
374 ExprToMem exprToMem_;
379 : OperatorExpansion(solver) {}
400 virtual SymbolicExpr::Ptr
immediateExpansion(
const Token &name,
const SymbolicExpr::Nodes &operands) ROSE_OVERRIDE;
419 NameToVarMap name2var_;
429 const NameToVarMap&
map()
const {
return name2var_; }
471 AtomTable atomTable_;
472 OperatorTable operatorTable_;
490 static void initDiagnostics();
496 SymbolicExpr::Ptr
parse(
const std::string&,
const std::string &inputName=
"string");
501 SymbolicExpr::Ptr
parse(std::istream &input,
const std::string &filename,
502 unsigned lineNumber=1,
unsigned columnNumber=0);
507 SymbolicExpr::Ptr
parse(TokenStream&);
518 const AtomTable&
atomTable()
const {
return atomTable_; }
558 std::ostream& operator<<(std::ostream&,
const SymbolicExprParser::SyntaxError&);
559 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.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
size_t nBits() const
Property: Total width of values.
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.
int nextCharacter()
Returns the next character.
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.
std::string inputName
Name of input, usually a file name.
Sawyer::SharedPointer< SymbolicExprCmdlineParser > Ptr
Shared-ownership pointer.
OperatorTable & operatorTable()
Return all operator expansion functors.
SymbolicExpr::Ptr immediateExpansion(const SymbolicExprParser::Token &) ROSE_OVERRIDE
Expand a parsed atom into some other expression.
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.
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).
Reference-counting intrusive smart pointer.
const std::string & name() const
Name of this input stream.
Name space for the entire library.
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.
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.
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.
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.