ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
cppsimgen.hpp
Go to the documentation of this file.
1 #ifndef __CPPSIMGEN_HPP_DEFINED
2 #define __CPPSIMGEN_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 #include <fstream>
10 
11 namespace ilasynth {
12 class CppVar;
13 class CppFun;
14 class CppSimFun;
15 
16 // Cpp variable
17 class CppVar {
18  friend class CppFun;
19  friend class CppSimGen;
20 
21 public:
23 
24  // Variable count.
25  static int varCnt;
26  // Variable type.
27  static std::string boolStr;
28  static std::string bvStr;
29  static std::string memStr;
30  static std::string voidStr;
31 
32 protected:
33  std::string _type;
34  std::string _name;
35  std::string _val;
36  int _width;
37  int _idxwidth;
38  bool _isConst;
39 
40 public:
41  // Constructor with nptr_t, used for ast nodes.
42  CppVar(nptr_t nptr, const std::string& name = "");
43  // Constructor with Node*, used for ast nodes.
44  CppVar(const Node* node, const std::string& name = "");
45  // Constructor for bv prototype, used for temp var and func return.
46  CppVar(int width);
47  // Constructor for bv mask, used for bitvector mask.
48  CppVar(const std::string& name, const std::string& val);
49  // Something like copy construction, except name.
50  CppVar(const CppVar* var);
51  // Destructor.
52  ~CppVar();
53 
54 public:
55  // Define variable, ex." int r0"
56  std::string def() const;
57  // Define reference, ex. " int& r0"
58  std::string refDef() const;
59  // Define constructor init requirement, ex. "type_mem(16,8) m1"
60  std::string ctorDef() const;
61  // Corrected Mem Type, ex "type_mem<BV?, depth>"
62  std::string vType() const;
63  // Use variable, ex. " r0"
64  std::string use() const;
65  // Use variable as signed.
66  std::string signedUse() const;
67  // Use the exact length variable.
68  std::string exactUse() const;
69  // Use the casted variable.
70  std::string castUse() const;
71 
72 private:
73  void init(nptr_t n);
74  void init(const Node* n);
75 };
76 
77 class CppFun {
78  friend class CppSimGen;
79 
80 protected:
81  std::string _name;
82  std::vector<const CppVar*> _args;
83  std::vector<std::string> _codeList;
84  std::vector<std::string> _varList;
86  std::vector<std::pair<CppVar*, CppVar*>> _updates;
87 
88 public:
89  // Constructor.
90  CppFun(const std::string& name);
91  // Destructor.
92  ~CppFun();
93 
94  bool retSet() { return (_ret != NULL); }
95 
96 protected:
97  // Add argument.
98  void addArg(const CppVar* arg);
99  // Add body.
100  void addBody(const std::string& code);
101 
102  // Print the function declaration to the output stream.
103  // If the modelname is not specified, assume declare within the class.
104  void dumpDec(std::ostream& out, const std::string& modelName,
105  const int& indent) const;
106  // Print the variable declaration to output stream.
107  void dumpVarDec(std::ostream& out, const int& indent) const;
108  // Print the code (with tail) to output stream.
109  void dumpCode(std::ostream& out, const int& indent) const;
110 };
111 
112 // Cpp simulator generator.
113 class CppSimGen {
114 public:
115  // Define types.
116  typedef std::map<std::string, CppVar*> CppVarMap;
117  typedef std::map<std::string, CppFun*> CppFunMap;
118 
119 private:
120  // Model name.
121  std::string _modelName;
122 
123  // State variables and other global variables(const mem).
125  // Input variables.
127  // Functions.
129 
130  // Uninterpreted function variables.
132  // Uninterpreted functions.
134 
135  // Constant memory to be init.
136  std::map<CppVar*, const MemConst*> _memConst;
137 
138  // Constant bitvector mask.
140 
141  // Function seeable variables. Update during function construction.
142  std::map<CppFun*, CppVarMap*> _varInFun;
143  // Current working function.
145  // Variable currently added.
147  // Var map for the current working function.
149 
150 public:
151  // Constructor.
152  CppSimGen(const std::string& prefix);
153 
154  // Destructor.
155  ~CppSimGen();
156 
157  // ------------------------------------------------------------------- //
158  // Create input variable and put it in the _varMap.
159  CppVar* addInput(const std::string& name, nptr_t node, bool ms = false);
160 
161  // Create state variable and put it in the _varMap.
162  CppVar* addState(const std::string& name, nptr_t node, bool ms = false);
163 
164  // Create new function and put it in the _funMap.
165  CppFun* addFun(const std::string& name, bool ms = false);
166  // Create new uninterpreted function and put it in the _unitpFunMap.
167  CppVar* addFuncVar(const std::string& name, nptr_t node, bool ms = false);
168 
169  // ------------------------------------------------------------------- //
170  // This will be used by depthFirstVisit.
171  void operator()(const Node* n);
172 
173  // ------------------------------------------------------------------- //
174  // Build function body with ast node.
175  void buildFun(CppFun* f, nptr_t nptr);
176 
177  // Set return variable for the function.
178  void setFunReturn(CppFun* f, nptr_t nptr);
179 
180  // Add a variable to be updated at the end of the function.
181  void addFunUpdate(CppFun* f, nptr_t lhs, nptr_t rhs);
182  // Add a variable to be updated at the end of the function.
183  void addFunUpdate(CppFun* f, nptr_t lhs, CppVar* rhs);
184 
185  // Terminate function building.
186  void endFun(CppFun* f);
187 
188  // Apply the function and return to some variable.
189  CppVar* appFun(CppFun* appFun, CppFun* envFun);
190 
191  // Export all code into the output stream.
192  void exportAllToFile(const std::string& fileName) const;
193 
194  // Export all code into the directory.
195  void exportAllToDir(const std::string& dirName) const;
196 
197 private:
198  // ------------------------------------------------------------------- //
199  // Create a class for memory representation.
200  void defMemClass(std::ostream& out) const;
201 
202  // Initial constant memory.
203  void setMemConst(std::ostream& out) const;
204 
205  // Create a class for the model.
206  void genModel(std::ostream& out) const;
207 
208  // Declare uninterpreted function (extern).
209  void defUnitpFunc(std::ostream& out) const;
210 
211  // Create common headers, class definitions, type definitions, ...
212  void createCommon(std::ostream& out) const;
213 
214  // ------------------------------------------------------------------- //
215  // Convert a bool variable into cpp code.
216  CppVar* getBoolVarCpp(const BoolVar* n);
217  // Convert a bool constant into cpp code.
218  CppVar* getBoolConstCpp(const BoolConst* n);
219  // Convert a bool op into cpp code.
220  CppVar* getBoolOpCpp(const BoolOp* n);
221  // Convert a bitvector variable into cpp code.
222  CppVar* getBvVarCpp(const BitvectorVar* n);
223  // Convert a bitvector constant into cpp code.
225  // Convert a bitvector op into cpp code.
226  CppVar* getBvOpCpp(const BitvectorOp* n);
227  // Convert a memory variable into cpp code.
228  CppVar* getMemVarCpp(const MemVar* n);
229  // Convert a memory const into cpp code.
230  CppVar* getMemConstCpp(const MemConst* n);
231  // Convert a memory op into cpp code.
232  CppVar* getMemOpCpp(const MemOp* n);
233  // Convert a function into cpp code.
234  CppVar* getFuncVarCpp(const FuncVar* n);
235  // Check if the node ITE.
236  bool isITE(nptr_t n);
237 
238  // ------------------------------------------------------------------- //
239  // Traverse and generate code in depth-first order.
240  void depthFirstTraverse(nptr_t n);
241 
242  // ------------------------------------------------------------------- //
243  // Helper function for getting exact signed code for multiprecision int
244  std::string getSignedCppCode(CppVar* var);
245 
246  // ------------------------------------------------------------------- //
247  // Helper function for inserting element in map, with assertion on 1st.
248  template <class T>
249  void checkAndInsert(std::map<std::string, T*>& mp, const std::string& name,
250  T* var, bool force = false);
251  // Helper function for finding element in map, with assertion on find.
252  CppVar* findVar(CppVarMap& mp, const std::string& name);
253 };
254 
255 } // namespace ilasynth
256 
257 #endif
std::string _val
Definition: cppsimgen.hpp:35
std::vector< std::pair< CppVar *, CppVar * > > _updates
Definition: cppsimgen.hpp:86
Definition: func.hpp:33
Definition: bitvec.hpp:83
void endFun(CppFun *f)
CppVar * _ret
Definition: cppsimgen.hpp:85
bool retSet()
Definition: cppsimgen.hpp:94
CppVar * findVar(CppVarMap &mp, const std::string &name)
std::map< CppFun *, CppVarMap * > _varInFun
Definition: cppsimgen.hpp:142
std::vector< std::string > _varList
Definition: cppsimgen.hpp:84
void setMemConst(std::ostream &out) const
static std::string voidStr
Definition: cppsimgen.hpp:30
void depthFirstTraverse(nptr_t n)
CppVar * addInput(const std::string &name, nptr_t node, bool ms=false)
std::vector< const CppVar * > _args
Definition: cppsimgen.hpp:82
CppVar * getMemOpCpp(const MemOp *n)
void setFunReturn(CppFun *f, nptr_t nptr)
void createCommon(std::ostream &out) const
std::string def() const
static int varCnt
Definition: cppsimgen.hpp:25
CppVar * getBvConstCpp(const BitvectorConst *n)
CppVar(nptr_t nptr, const std::string &name="")
std::map< std::string, CppFun * > CppFunMap
Definition: cppsimgen.hpp:117
int _width
Definition: cppsimgen.hpp:36
static std::string bvStr
Definition: cppsimgen.hpp:28
std::string vType() const
CppVarMap * _curVarMap
Definition: cppsimgen.hpp:148
CppFun(const std::string &name)
CppVar * getBoolOpCpp(const BoolOp *n)
CppVar * getBoolConstCpp(const BoolConst *n)
bool isITE(nptr_t n)
CppVarMap _unitpFuncVarMap
Definition: cppsimgen.hpp:131
CppVar * addFuncVar(const std::string &name, nptr_t node, bool ms=false)
static std::string boolStr
Definition: cppsimgen.hpp:27
CppVar * getBvVarCpp(const BitvectorVar *n)
boost::multiprecision::cpp_int mp_int_t
Definition: common.hpp:9
std::map< CppVar *, const MemConst * > _memConst
Definition: cppsimgen.hpp:136
Definition: mem.hpp:46
std::string _name
Definition: cppsimgen.hpp:81
std::string getSignedCppCode(CppVar *var)
void checkAndInsert(std::map< std::string, T *> &mp, const std::string &name, T *var, bool force=false)
Definition: bool.hpp:78
void buildFun(CppFun *f, nptr_t nptr)
CppFunMap _funMap
Definition: cppsimgen.hpp:128
std::string ctorDef() const
std::vector< std::string > _codeList
Definition: cppsimgen.hpp:83
void addArg(const CppVar *arg)
boost::shared_ptr< Node > nptr_t
Definition: node.hpp:24
CppSimGen(const std::string &prefix)
CppVar * appFun(CppFun *appFun, CppFun *envFun)
void dumpDec(std::ostream &out, const std::string &modelName, const int &indent) const
Definition: bool.hpp:46
std::string exactUse() const
CppVarMap _states
Definition: cppsimgen.hpp:124
Definition: cppsimgen.hpp:77
std::string _type
Definition: cppsimgen.hpp:33
void genModel(std::ostream &out) const
void init(nptr_t n)
std::string _modelName
Definition: cppsimgen.hpp:121
CppVar * _curVar
Definition: cppsimgen.hpp:146
Definition: abstraction.hpp:21
void addFunUpdate(CppFun *f, nptr_t lhs, nptr_t rhs)
void dumpVarDec(std::ostream &out, const int &indent) const
Definition: node.hpp:55
CppVar * getBoolVarCpp(const BoolVar *n)
Definition: bitvec.hpp:38
CppFun * addFun(const std::string &name, bool ms=false)
std::string _name
Definition: cppsimgen.hpp:34
void operator()(const Node *n)
CppVar * getBvOpCpp(const BitvectorOp *n)
Definition: mem.hpp:30
void exportAllToDir(const std::string &dirName) const
void addBody(const std::string &code)
mp_int_t cppBvType
Definition: cppsimgen.hpp:22
bool _isConst
Definition: cppsimgen.hpp:38
int _idxwidth
Definition: cppsimgen.hpp:37
std::string use() const
CppVarMap _masks
Definition: cppsimgen.hpp:139
void defUnitpFunc(std::ostream &out) const
CppFunMap _unitpFuncMap
Definition: cppsimgen.hpp:133
std::string castUse() const
std::map< std::string, CppVar * > CppVarMap
Definition: cppsimgen.hpp:116
void dumpCode(std::ostream &out, const int &indent) const
Definition: bool.hpp:35
void defMemClass(std::ostream &out) const
Definition: cppsimgen.hpp:113
std::string signedUse() const
CppVarMap _inputs
Definition: cppsimgen.hpp:126
CppVar * addState(const std::string &name, nptr_t node, bool ms=false)
CppVar * getFuncVarCpp(const FuncVar *n)
Definition: bitvec.hpp:54
Definition: mem.hpp:67
CppVar * getMemVarCpp(const MemVar *n)
CppFun * _curFun
Definition: cppsimgen.hpp:144
std::string refDef() const
static std::string memStr
Definition: cppsimgen.hpp:29
CppVar * getMemConstCpp(const MemConst *n)
Definition: cppsimgen.hpp:17
void exportAllToFile(const std::string &fileName) const