ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
genCBMC.hpp
Go to the documentation of this file.
1 #ifndef __GENCBMCVERIF_HPP_DEFINED
2 #define __GENCBMCVERIF_HPP_DEFINED
3 
4 #include <boost/shared_ptr.hpp>
5 #include <ilasynth/ast.hpp>
6 #include <ilasynth/exception.hpp>
7 #include <ilasynth/util.hpp>
8 
9 //
10 // Here we make use of previous work
11 // This is only inteneded to be used
12 // to generate C that works with CBMC
13 // It is a temporary workaround
14 // Don't rely on it too much.
15 //
16 // May life treat you gently.
17 // --Hongce
18 //
19 
20 #include <fstream>
21 
22 namespace ilasynth {
23 class CVar;
24 class CFun;
25 class CVerifGen;
26 
27 // C variable
28 class CVar {
29  friend class CFun;
30  friend class CVerifGen;
31 
32 public:
33  // Variable count.
34  static int varCnt;
35  // Variable type.
36  static std::string boolStr;
37  static std::string bvStr;
38  static std::string memStr;
39  static std::string voidStr;
40 
41 protected:
42  std::string _type;
43  std::string _name;
44  std::string _val;
45  int _width;
46  int _idxWidth; // addr width only for memory variables
47  bool _isConst;
48 
49 public:
50  // Constructor with nptr_t, used for ast nodes.
51  CVar(nptr_t nptr, const std::string& name = "");
52  // Constructor with Node*, used for ast nodes.
53  CVar(const Node* node, const std::string& name = "");
54  // Constructor for bv prototype, used for temp var and func return.
55  CVar(int width);
56  // Constructor for bv mask, used for bitvector mask.
57  CVar(const std::string& name, const std::string& val);
58  // Something like copy construction, except name.
59  CVar(const CVar* var);
60  // Destructor.
61  ~CVar();
62 
63 public:
64  // Define variable, ex." int r0"
65  std::string def() const;
66  // Define reference, ex. " int& r0"
67  std::string refDef() const;
68  // Use variable, ex. " r0"
69  std::string use() const;
70  // Use variable as signed.
71  std::string signedUse() const;
72  // Use the exact length variable.
73  std::string exactUse() const;
74  // Use the casted variable.
75  std::string castUse() const;
76 
77  // malloc in C
78  std::string memdef() const;
79  // Type used in C, ex. BV *
80  std::string ctype() const;
81 
82  bool memberVar;
83 
84 private:
85  void init(nptr_t n);
86  void init(const Node* n);
87 };
88 
89 class CFun {
90  friend class CVerifGen;
91 
92 protected:
93  std::string _name;
94  std::vector<const CVar*> _args;
95  std::vector<std::string> _codeList;
96  std::vector<std::string> _varList;
98  std::vector<std::pair<CVar*, CVar*>>
99  _updates; // it seems that _updates is only used for the Update fun
100 
101 public:
102  // Constructor.
103  CFun(const std::string& name);
104  // Destructor.
105  ~CFun();
106 
107  bool retSet() { return (_ret != NULL); }
108 
109 protected:
110  // Add argument.
111  void addArg(const CVar* arg);
112  // Add body.
113  void addBody(const std::string& code);
114 
115  // Print the function declaration to the output stream.
116  // If the modelname is not specified, assume declare within the class.
117  void dumpDec(std::ostream& out, const std::string& modelName,
118  const int& indent, bool decl = false) const;
119  // Print the variable declaration to output stream.
120  void dumpVarDec(std::ostream& out, const int& indent) const;
121  // Print the code (with tail) to output stream.
122  void dumpCode(std::ostream& out, const int& indent) const;
123 
124  // Print C declaration main difference is, use struct as a pointer
125  void dumpCDec(std::ostream& out, const std::string& modelName,
126  const int& indent) const;
127 };
128 
129 // C CBMC Verification Code Generator.
130 class CVerifGen {
131 public:
132  // Define types.
133  typedef std::map<std::string, CVar*> CVarMap;
134  typedef std::map<std::string, CFun*> CFunMap;
135 
136 private:
137  // Model name.
138  std::string _modelName;
139 
140  // State variables and other global variables(const mem).
142  // Input variables.
144  // Functions.
146 
147  // Uninterpreted function variables.
149  // Uninterpreted functions.
151 
152  // Constant memory to be init.
153  std::map<CVar*, const MemConst*> _memConst;
154 
155  // Constant bitvector mask.
157 
158  // Function seeable variables. Update during function construction.
159  std::map<CFun*, CVarMap*> _varInFun;
160  // Current working function.
162  // Variable currently added.
164  // Var map for the current working function.
166 
168  CVarMap _localConstVar; // because const are refered by their value (not name)
169  // so why create a var that is never used/assigned to.
170 
171 public:
172  // Constructor.
173  CVerifGen(const std::string& prefix);
174 
175  // Destructor.
176  ~CVerifGen();
177 
178  // ------------------------------------------------------------------- //
179  // Create input variable and put it in the _varMap.
180  CVar* addInput(const std::string& name, nptr_t node, bool ms = false);
181 
182  // Create state variable and put it in the _varMap.
183  CVar* addState(const std::string& name, nptr_t node, bool ms = false);
184 
185  // Create new function and put it in the _funMap.
186  CFun* addFun(const std::string& name, bool ms = false);
187  // Create new uninterpreted function and put it in the _unitpFunMap.
188  CVar* addFuncVar(const std::string& name, nptr_t node, bool ms = false);
189 
190  // ------------------------------------------------------------------- //
191  // This will be used by depthFirstVisit.
192  void operator()(const Node* n);
193 
194  // ------------------------------------------------------------------- //
195  // Build function body with ast node.
196  void buildFun(CFun* f, nptr_t nptr);
197 
198  // Set return variable for the function.
199  void setFunReturn(CFun* f, nptr_t nptr);
200 
201  // Add a variable to be updated at the end of the function.
202  void addFunUpdate(CFun* f, nptr_t lhs, nptr_t rhs);
203  // Add a variable to be updated at the end of the function.
204  void addFunUpdate(CFun* f, nptr_t lhs, CVar* rhs);
205 
206  // Terminate function building.
207  void endFun(CFun* f);
208 
209  // Apply the function and return to some variable.
210  CVar* appFun(CFun* appFun, CFun* envFun);
211 
212  // Export all code into the output stream.
213  void exportAllToFile(const std::string& fileName) const;
214 
215  // Export all code into the directory.
216  void exportAllToDir(const std::string& dirName) const;
217 
218 private:
219  // ------------------------------------------------------------------- //
220  // Create a class for memory representation.
221  void defMemClass(std::ostream& out) const;
222 
223  // Initial constant memory.
224  void setMemConst(std::ostream& out) const;
225 
226  // Create a class for the model.
227  void genModel(std::ostream& out) const;
228 
229  // Declare uninterpreted function (extern).
230  void defUnitpFunc(std::ostream& out) const;
231 
232  // Create common headers, class definitions, type definitions, ...
233  void createCommon(std::ostream& out) const;
234 
235  // ------------------------------------------------------------------- //
236  // Convert a bool variable into c code.
237  CVar* getBoolVarC(const BoolVar* n);
238  // Convert a bool constant into c code.
239  CVar* getBoolConstC(const BoolConst* n);
240  // Convert a bool op into c code.
241  CVar* getBoolOpC(const BoolOp* n);
242  // Convert a bitvector variable into c code.
243  CVar* getBvVarC(const BitvectorVar* n);
244  // Convert a bitvector constant into c code.
245  CVar* getBvConstC(const BitvectorConst* n);
246  // Convert a bitvector op into c code.
247  CVar* getBvOpC(const BitvectorOp* n);
248  // Convert a memory variable into c code.
249  CVar* getMemVarC(const MemVar* n);
250  // Convert a memory const into c code.
251  CVar* getMemConstC(const MemConst* n);
252  // Convert a memory op into c code.
253  CVar* getMemOpC(const MemOp* n);
254  // Convert a function into c code.
255  CVar* getFuncVarC(const FuncVar* n);
256  // Check if the node ITE.
257  bool isITE(nptr_t n);
258 
259  // ------------------------------------------------------------------- //
260  // Traverse and generate code in depth-first order.
261  void depthFirstTraverse(nptr_t n);
262 
263  // ------------------------------------------------------------------- //
264  // Helper function for getting exact signed code for multiprecision int
265  std::string getSignedCCode(CVar* var);
266 
267  // ------------------------------------------------------------------- //
268  // Helper function for inserting element in map, with assertion on 1st.
269  template <class T>
270  void checkAndInsert(std::map<std::string, T*>& mp, const std::string& name,
271  T* var, bool force = false);
272  // Helper function for finding element in map, with assertion on find.
273  CVar* findVar(CVarMap& mp, const std::string& name);
274 };
275 
276 } // namespace ilasynth
277 
278 #endif
CVar * addInput(const std::string &name, nptr_t node, bool ms=false)
void dumpCode(std::ostream &out, const int &indent) const
CVar * getBoolOpC(const BoolOp *n)
CVarMap * _curVarMap
Definition: genCBMC.hpp:165
Definition: func.hpp:33
Definition: bitvec.hpp:83
CVarMap _localArray
Definition: genCBMC.hpp:167
void dumpDec(std::ostream &out, const std::string &modelName, const int &indent, bool decl=false) const
CVar * getBvConstC(const BitvectorConst *n)
CVarMap _localConstVar
Definition: genCBMC.hpp:168
std::string signedUse() const
std::string memdef() const
CVar * getMemOpC(const MemOp *n)
std::string castUse() const
std::string exactUse() const
bool retSet()
Definition: genCBMC.hpp:107
CVarMap _masks
Definition: genCBMC.hpp:156
CVarMap _states
Definition: genCBMC.hpp:141
std::string def() const
void exportAllToDir(const std::string &dirName) const
CVar * getFuncVarC(const FuncVar *n)
void addBody(const std::string &code)
std::string _type
Definition: genCBMC.hpp:42
void addArg(const CVar *arg)
void dumpCDec(std::ostream &out, const std::string &modelName, const int &indent) const
std::vector< const CVar * > _args
Definition: genCBMC.hpp:94
static int varCnt
Definition: genCBMC.hpp:34
Definition: genCBMC.hpp:89
void endFun(CFun *f)
std::map< CFun *, CVarMap * > _varInFun
Definition: genCBMC.hpp:159
CVar * _ret
Definition: genCBMC.hpp:97
CVar * addFuncVar(const std::string &name, nptr_t node, bool ms=false)
std::map< std::string, CFun * > CFunMap
Definition: genCBMC.hpp:134
CVerifGen(const std::string &prefix)
Definition: mem.hpp:46
void setMemConst(std::ostream &out) const
bool memberVar
Definition: genCBMC.hpp:82
void createCommon(std::ostream &out) const
Definition: genCBMC.hpp:130
void defMemClass(std::ostream &out) const
std::string _val
Definition: genCBMC.hpp:44
bool _isConst
Definition: genCBMC.hpp:47
Definition: bool.hpp:78
CFun(const std::string &name)
std::string refDef() const
std::string use() const
static std::string boolStr
Definition: genCBMC.hpp:36
boost::shared_ptr< Node > nptr_t
Definition: node.hpp:24
void exportAllToFile(const std::string &fileName) const
static std::string voidStr
Definition: genCBMC.hpp:39
Definition: bool.hpp:46
CFunMap _unitpFuncMap
Definition: genCBMC.hpp:150
void depthFirstTraverse(nptr_t n)
CVar * getBoolVarC(const BoolVar *n)
CVar * getBvOpC(const BitvectorOp *n)
CVar * findVar(CVarMap &mp, const std::string &name)
Definition: abstraction.hpp:21
bool isITE(nptr_t n)
CFun * addFun(const std::string &name, bool ms=false)
CVar * getMemConstC(const MemConst *n)
CVar * getBvVarC(const BitvectorVar *n)
Definition: node.hpp:55
CVar * addState(const std::string &name, nptr_t node, bool ms=false)
Definition: bitvec.hpp:38
static std::string memStr
Definition: genCBMC.hpp:38
std::map< std::string, CVar * > CVarMap
Definition: genCBMC.hpp:133
static std::string bvStr
Definition: genCBMC.hpp:37
CVar * getMemVarC(const MemVar *n)
std::map< CVar *, const MemConst * > _memConst
Definition: genCBMC.hpp:153
Definition: mem.hpp:30
int _idxWidth
Definition: genCBMC.hpp:46
void dumpVarDec(std::ostream &out, const int &indent) const
std::vector< std::string > _codeList
Definition: genCBMC.hpp:95
int _width
Definition: genCBMC.hpp:45
CVar * getBoolConstC(const BoolConst *n)
void init(nptr_t n)
CVar * _curVar
Definition: genCBMC.hpp:163
CFunMap _funMap
Definition: genCBMC.hpp:145
CVarMap _unitpFuncVarMap
Definition: genCBMC.hpp:148
Definition: bool.hpp:35
std::vector< std::string > _varList
Definition: genCBMC.hpp:96
std::string getSignedCCode(CVar *var)
Definition: bitvec.hpp:54
CVarMap _inputs
Definition: genCBMC.hpp:143
Definition: mem.hpp:67
void buildFun(CFun *f, nptr_t nptr)
void addFunUpdate(CFun *f, nptr_t lhs, nptr_t rhs)
CFun * _curFun
Definition: genCBMC.hpp:161
std::string _modelName
Definition: genCBMC.hpp:138
std::vector< std::pair< CVar *, CVar * > > _updates
Definition: genCBMC.hpp:99
void genModel(std::ostream &out) const
std::string _name
Definition: genCBMC.hpp:93
std::string _name
Definition: genCBMC.hpp:43
Definition: genCBMC.hpp:28
CVar * appFun(CFun *appFun, CFun *envFun)
void defUnitpFunc(std::ostream &out) const
std::string ctype() const
CVar(nptr_t nptr, const std::string &name="")
void checkAndInsert(std::map< std::string, T *> &mp, const std::string &name, T *var, bool force=false)
void operator()(const Node *n)
void setFunReturn(CFun *f, nptr_t nptr)