ROSE 0.11.145.192
SymbolicExpressionParser.h
1#ifndef ROSE_BinaryAnalysis_SymbolicExpressionParser_H
2#define ROSE_BinaryAnalysis_SymbolicExpressionParser_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
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>
14
15namespace Rose {
16namespace BinaryAnalysis {
17
32public:
34
36 public:
37 std::string inputName;
38 unsigned lineNumber;
39 unsigned columnNumber;
40 SyntaxError(const std::string &mesg, const std::string &inputName, unsigned lineNumber, unsigned columnNumber);
41 ~SyntaxError() throw () {}
42 void print(std::ostream&) const;
43 };
44
47 public:
48 SubstitutionError(const std::string &mesg)
49 : Rose::Exception(mesg) {}
50 ~SubstitutionError() throw () {}
51 void print(std::ostream&) const;
52 };
53
55
56 class Token {
57 public:
67
68 private:
69 Type tokenType_;
70 std::string lexeme_; // lexeme
71 SymbolicExpression::Type exprType_; // type for expression
72 Sawyer::Container::BitVector bits_; // bits representing constant terms
73 unsigned lineNumber_, columnNumber_; // for start of token
74
75 public:
78 : tokenType_(NONE), lineNumber_(0), columnNumber_(0) {}
79
82 unsigned lineNumber, unsigned columnNumber)
83 : tokenType_(tokenType), lexeme_(lexeme), exprType_(exprType),
84 lineNumber_(lineNumber), columnNumber_(columnNumber) {
85 ASSERT_forbid(BITVECTOR == tokenType);
86 }
87
90 unsigned lineNumber, unsigned columnNumber)
91 : tokenType_(BITVECTOR), lexeme_(lexeme), exprType_(exprType), bits_(bv),
92 lineNumber_(lineNumber), columnNumber_(columnNumber) {
93 ASSERT_require(exprType.nBits() == bv.size());
94 }
95
97 SymbolicExpressionParser::SyntaxError syntaxError(const std::string &mesg, const std::string &name="input") const {
98 return SymbolicExpressionParser::SyntaxError(mesg, name, lineNumber_, columnNumber_);
99 }
100
102 Type tokenType() const { return tokenType_; }
103
105 const std::string &lexeme() const { return lexeme_; }
106
108 SymbolicExpression::Type exprType() const { return exprType_; }
109
111 const Sawyer::Container::BitVector& bits() const { return bits_; }
112
114 unsigned lineNumber() const { return lineNumber_; }
115
117 unsigned columnNumber() const { return columnNumber_; }
118 };
119
121
126 std::istream &input_;
127 std::string name_;
128 unsigned lineNumber_, columnNumber_;
129 const Token endToken_;
130 std::vector<Token> tokens_;
131 int readAhead_;
132
133 public:
138 explicit TokenStream(std::istream &input, const std::string &name="input",
139 unsigned lineNumber=1, unsigned columnNumber=0)
140 : input_(input), name_(name), lineNumber_(lineNumber), columnNumber_(columnNumber), readAhead_(EOF) {
141 init();
142 }
143
145 const std::string& name() const { return name_; }
146
148 unsigned lineNumber() const { return lineNumber_; }
149
151 unsigned columnNumber() const { return columnNumber_; }
152
154 const Token& operator[](size_t idx);
155
157 void shift(size_t n=1);
158
161
165
168
171
175
178
185 std::string consumeTerm();
186
192
196
197 private:
198 void init();
199
200 // Try to fill the token vector so it contains tokens up through at least [idx]
201 void fillTokenList(size_t idx);
202 };
203
205
207 std::string title_;
208 std::string docString_;
209 public:
210 virtual ~Expansion() {}
211
214
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; }
243 };
244
246
247 class AtomExpansion: public Expansion {
248 public:
251
259 };
260
262 typedef std::vector<AtomExpansion::Ptr> AtomTable;
263
265
267 public:
270
271 protected:
272 SmtSolverPtr solver; // may be null
273
274 explicit OperatorExpansion(const SmtSolverPtr &solver);
275
276 public:
277 virtual ~OperatorExpansion();
278
282 virtual SymbolicExpression::Ptr immediateExpansion(const Token &name, const SymbolicExpression::Nodes &operands) = 0;
283 };
284
286 typedef std::vector<OperatorExpansion::Ptr> OperatorTable;
287
289
314
316
362
364
369 public:
372
373 // internal
375
376 private:
377 ExprToMem exprToMem_;
379
380 protected:
381 MemorySubstituter(const SmtSolver::Ptr &solver /*=NULL*/)
382 : OperatorExpansion(solver) {}
383
384 public:
386 static Ptr instance(const SmtSolver::Ptr &solver /*=NULL*/);
387
402 // internal
403 virtual SymbolicExpression::Ptr immediateExpansion(const Token &name, const SymbolicExpression::Nodes &operands) override;
405 };
406
408
414 public:
417
420
421 private:
422 NameToVarMap name2var_;
423
424 protected:
426
427 public:
429 static Ptr instance();
430
432 const NameToVarMap& map() const { return name2var_; }
433
434 // internal
436 };
437
439
444 protected:
448
449 public:
452
453 static Ptr instance() {
454 return Ptr(new SymbolicExprCmdlineParser);
455 }
456
457 static Ptr instance(const Sawyer::CommandLine::ValueSaver::Ptr &valueSaver) {
459 }
460
461 static std::string docString();
462
463 private:
464 virtual Sawyer::CommandLine::ParsedValue operator()(const char *input, const char **rest,
465 const Sawyer::CommandLine::Location &loc) override;
466 };
467
468 static SymbolicExprCmdlineParser::Ptr symbolicExprParser(SymbolicExpression::Ptr &storage);
469 static SymbolicExprCmdlineParser::Ptr symbolicExprParser(std::vector<SymbolicExpression::Ptr> &storage);
470 static SymbolicExprCmdlineParser::Ptr symbolicExprParser();
471
473private:
474 AtomTable atomTable_;
475 OperatorTable operatorTable_;
476 SmtSolverPtr solver_; // optional solver for simplifications
477
478public:
479 static Sawyer::Message::Facility mlog;
480
481public:
484
488 explicit SymbolicExpressionParser(const SmtSolverPtr &solver);
489
491
492 // used internally to initialize the diagnostics system
493 static void initDiagnostics();
494
499 SymbolicExpression::Ptr parse(const std::string&, const std::string &inputName="string");
500
504 SymbolicExpression::Ptr parse(std::istream &input, const std::string &filename,
505 unsigned lineNumber=1, unsigned columnNumber=0);
506
511
514
517
521 const AtomTable& atomTable() const { return atomTable_; }
522 AtomTable& atomTable() { return atomTable_; }
528 const OperatorTable& operatorTable() const { return operatorTable_; }
529 OperatorTable& operatorTable() { return operatorTable_; }
536 std::string docString() const;
537
556
557private:
558 void init();
559};
560
561std::ostream& operator<<(std::ostream&, const SymbolicExpressionParser::SyntaxError&);
562std::ostream& operator<<(std::ostream&, const SymbolicExpressionParser::SubstitutionError&);
563
564} // namespace
565} // namespace
566
567#endif
568#endif
SmtSolverPtr Ptr
Reference counting pointer for SMT solvers.
Definition SmtSolver.h:45
Sawyer::SharedPointer< AtomExpansion > Ptr
Shared-ownership pointer to an AtomExpansion.
virtual SymbolicExpression::Ptr immediateExpansion(const Token &name)=0
Expand a parsed atom into some other expression.
Virtual base class for atom and operator expansion.
void title(const std::string &s)
Property: Title to use for documentation.
virtual SymbolicExpression::Ptr delayedExpansion(const SymbolicExpression::Ptr &src, const SymbolicExpressionParser *)
Substitute one expression with another.
const std::string & docString() const
Property: Documentation string.
Sawyer::SharedPointer< Expansion > Ptr
Shared-ownership pointer to an Expansion.
const std::string & title() const
Property: Title to use for documentation.
void docString(const std::string &s)
Property: Documentation string.
void riscOperators(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &ops)
Property: Semantic state used during delayed expansion.
virtual SymbolicExpression::Ptr delayedExpansion(const SymbolicExpression::Ptr &, const SymbolicExpressionParser *) override
Substitute one expression with another.
Sawyer::SharedPointer< MemorySubstituter > Ptr
Shared-ownership pointer.
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 SmtSolver::Ptr &solver)
Allocating constructor.
InstructionSemantics::BaseSemantics::RiscOperatorsPtr riscOperators() const
Property: Semantic state used during delayed expansion.
Sawyer::SharedPointer< OperatorExpansion > Ptr
Shared-ownership pointer.
virtual SymbolicExpression::Ptr immediateExpansion(const Token &name, const SymbolicExpression::Nodes &operands)=0
Operator to expand a list into an expression tree.
SymbolicExpression::Ptr delayedExpansion(const SymbolicExpression::Ptr &, const SymbolicExpressionParser *) override
Substitute one expression with another.
Sawyer::SharedPointer< RegisterSubstituter > Ptr
Shared-ownership pointer.
void riscOperators(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &ops)
Property: Semantic state used during delayed expansion.
SymbolicExpression::Ptr immediateExpansion(const SymbolicExpressionParser::Token &) override
Expand a parsed atom into some other expression.
static Ptr instance(const RegisterDictionaryPtr &)
Allocating constructor.
InstructionSemantics::BaseSemantics::RiscOperatorsPtr riscOperators() const
Property: Semantic state used during delayed expansion.
Sawyer::SharedPointer< RegisterToValue > Ptr
Shared-ownership pointer.
static Ptr instance(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &)
Allocating constructor.
SymbolicExpression::Ptr immediateExpansion(const SymbolicExpressionParser::Token &) override
Expand a parsed atom into some other expression.
Sawyer::SharedPointer< SymbolicExprCmdlineParser > Ptr
Shared-ownership pointer.
std::string inputName
Name of input, usually a file name.
void print(std::ostream &) const
Print error message to stream.
const NameToVarMap & map() const
Mapping between terms and variables.
Sawyer::Container::BiMap< std::string, SymbolicExpression::Ptr > NameToVarMap
Mapping between term names and placeholder variables.
SymbolicExpression::Ptr immediateExpansion(const SymbolicExpressionParser::Token &) override
Expand a parsed atom into some other expression.
Sawyer::SharedPointer< TermPlaceholders > Ptr
Shared-ownership pointer.
void consumeInlineComment()
Skip over angle-bracket comments.
void consumeWhiteSpaceAndComments()
Skip over white space and/or inline comments.
std::string consumeTerm()
Parse and consume a term.
SymbolicExpression::Type consumeType()
Parse and consume a type specification.
int consumeEscapeSequence()
Skip over an escape sequence and return the escaped character.
void shift(size_t n=1)
Consume the specified number of tokens.
Token scan()
Parse and consume the next token.
TokenStream(std::istream &input, const std::string &name="input", unsigned lineNumber=1, unsigned columnNumber=0)
Scan tokens from a character stream.
const std::string & name() const
Name of this input stream.
const Token & operator[](size_t idx)
Returns the specified token without consuming it.
void consumeWhiteSpace()
Skip over characters until a non-white-space character is encountered.
const std::string & lexeme() const
Lexeme from which token was parsed.
unsigned lineNumber() const
Line number for start of token.
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.
const Sawyer::Container::BitVector & bits() const
Bit vector for numeric constants.
Token(Type tokenType, const SymbolicExpression::Type &exprType, const std::string &lexeme, unsigned lineNumber, unsigned columnNumber)
Constructs a specific token from a string.
SymbolicExpression::Type exprType() const
Type of expression.
SymbolicExpressionParser::SyntaxError syntaxError(const std::string &mesg, const std::string &name="input") const
Creates a syntax error from a token plus message.
unsigned columnNumber() const
Column number for start of token.
Token()
Constructs an end-of-input token with no position information.
void appendOperatorExpansion(const OperatorExpansion::Ptr &)
Append a new functor for expanding operators into symbolic expressions.
RegisterSubstituter::Ptr defineRegisters(const RegisterDictionaryPtr &)
Add definitions for registers.
SymbolicExpression::Ptr parse(TokenStream &)
Create a symbolic expression by parsing a token stream.
const OperatorTable & operatorTable() const
Return all operator expansion functors.
void appendAtomExpansion(const AtomExpansion::Ptr &)
Append a new functor for expanding atoms into symbolic expressions.
SymbolicExpression::Ptr parse(std::istream &input, const std::string &filename, unsigned lineNumber=1, unsigned columnNumber=0)
Create a symbolic expression by parsing a file.
void defineRegisters(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &)
Add definitions for registers.
OperatorTable & operatorTable()
Return all operator expansion functors.
SymbolicExpression::Ptr delayedExpansion(const SymbolicExpression::Ptr &) const
Perform delayed expansion.
SymbolicExpressionParser(const SmtSolverPtr &solver)
Parser using a specific SMT solver for simplifications.
std::string docString() const
Documentation string.
SymbolicExpression::Ptr parse(const std::string &, const std::string &inputName="string")
Create a symbolic expression by parsing a string.
const AtomTable & atomTable() const
Return all atom expansion functors.
AtomTable & atomTable()
Return all atom expansion functors.
std::vector< OperatorExpansion::Ptr > OperatorTable
Ordered operator table.
std::vector< AtomExpansion::Ptr > AtomTable
Ordered atom table.
size_t nBits() const
Property: Total width of values.
Base class for all ROSE exceptions.
Information about a parsed switch value.
Base class parsing a value from input.
const ValueSaver::Ptr valueSaver() const
Property: functor responsible for saving a parsed value in user storage.
One-to-one mapping between source and target values.
Definition BiMap.h:26
size_t size() const
Size of vector in bits.
Definition BitVector.h:208
Container associating values with keys.
Definition Sawyer/Map.h:72
Collection of streams.
Definition Message.h:1606
Base class for reference counted objects.
Reference-counting intrusive smart pointer.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.
Position within a command-line.