Ghidra Decompiler Analysis Engine
pcodeparse.hh
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef __PCODE_SNIPPET__
17 #define __PCODE_SNIPPET__
18 
19 #include "pcodecompile.hh"
20 #include "sleighbase.hh"
21 
22 // Classes for compiling a standalone snippet of pcode, given an existing sleigh language
23 
24 struct IdentRec {
25  const char *nm;
26  int4 id;
27 };
28 
29 class PcodeLexer {
30 public:
31  enum { // Lexer states
32  start,
33  special2, // Middle of special 2 character operator
34  special3, // First character of special 3 character operator
35  special32, // Second character of special 3 character operator
36  comment, // Middle of an endofline comment
37  punctuation, // Punctuation character
38  identifier, // Middle of an identifier
39  hexstring, // Middle of a hexidecimal number
40  decstring, // Middle of a decimal number
41  endstream, // Reached end of stream
42  illegal // Scanned an illegal character
43  };
44 private:
45  static const IdentRec idents[];
46  int4 curstate;
47  char curchar,lookahead1,lookahead2;
48  char curtoken[256];
49  int4 tokpos;
50  bool endofstream;
51  bool endofstreamsent;
52  istream *s;
53  string curidentifier;
54  uintb curnum;
55  void starttoken(void) { curtoken[0] = curchar; tokpos = 1; }
56  void advancetoken(void) { curtoken[tokpos++] = curchar; }
57  bool isIdent(char c) const { return (isalnum(c)||(c=='_')||(c=='.')); }
58  bool isHex(char c) const { return isxdigit(c); }
59  bool isDec(char c) const { return isdigit(c); }
60  int4 findIdentifier(const string &str) const;
61  int4 moveState(void);
62 public:
63  PcodeLexer(void) { s = (istream *)0; }
64  void initialize(istream *t);
65  int4 getNextToken(void);
66  const string &getIdentifier(void) const { return curidentifier; }
67  uintb getNumber(void) const { return curnum; }
68 };
69 
70 class PcodeSnippet : public PcodeCompile {
71  PcodeLexer lexer;
72  const SleighBase *sleigh; // Language from which we get symbols
73  SymbolTree tree; // Symbols in the local scope of the snippet (temporaries)
74  uintb tempbase;
75  int4 errorcount;
76  string firsterror;
77  ConstructTpl *result;
78  virtual uintb allocateTemp(void);
79  virtual void addSymbol(SleighSymbol *sym);
80 public:
81  PcodeSnippet(const SleighBase *slgh);
82  void setResult(ConstructTpl *res) { result = res; }
83  ConstructTpl *releaseResult(void) { ConstructTpl *res = result; result = (ConstructTpl *)0; return res; }
84  virtual ~PcodeSnippet(void);
85  virtual const Location *getLocation(SleighSymbol *sym) const { return (const Location *)0; }
86  virtual void reportError(const Location *loc, const string &msg);
87  virtual void reportWarning(const Location *loc, const string &msg) {}
88  bool hasErrors(void) const { return (errorcount != 0); }
89  const string getErrorMessage(void) const { return firsterror; }
90  void setUniqueBase(uintb val) { tempbase = val; }
91  uintb getUniqueBase(void) const { return tempbase; }
92  void clear(void);
93  int lex(void);
94  bool parseStream(istream& s);
95  void addOperand(const string &name,int4 index);
96 };
97 
98 
99 #endif
PcodeCompile
Definition: pcodecompile.hh:55
sleighbase.hh
Base class for applications that process SLEIGH format specifications.
Location
Definition: pcodecompile.hh:21
PcodeSnippet
Definition: pcodeparse.hh:70
IdentRec
Definition: pcodeparse.hh:24
PcodeLexer
Definition: pcodeparse.hh:29
SleighBase
Common core of classes that read or write SLEIGH specification files natively.
Definition: sleighbase.hh:57
ConstructTpl
Definition: semantics.hh:161
SleighSymbol
Definition: slghsymbol.hh:23