ilang  1.1.4
ILAng: A Modeling and Verification Platform for SoCs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros
ilator.h
Go to the documentation of this file.
1 
4 #ifndef ILANG_TARGET_SC_ILATOR_H__
5 #define ILANG_TARGET_SC_ILATOR_H__
6 
7 #include <memory>
8 #include <string>
9 
10 #include <fmt/format.h>
11 
13 #include <ilang/util/log.h>
14 
16 namespace ilang {
17 
18 #define ILATOR_PRECISE_MEM
19 
21 class Ilator {
22 public:
23  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
25  Ilator(const InstrLvlAbsPtr& m);
27  ~Ilator();
28 
29  // ------------------------- METHODS -------------------------------------- //
33  void Generate(const std::string& dst, bool opt);
34 
35 private:
37  typedef fmt::memory_buffer StrBuff;
39  typedef std::unordered_map<ExprPtr, std::string, ExprHash> ExprVarMap;
41  class CxxFunc {
42  public:
43  CxxFunc(const std::string& in_name, const ExprPtr& in_ret,
44  const ExprPtr& in_target = nullptr)
45  : name(in_name), ret(in_ret), target(in_target) {}
46  CxxFunc(const std::string& in_name, const SortPtr& in_ret_type)
47  : name(in_name), ret_type(in_ret_type) {}
48  const std::string name = "";
49  const ExprPtr ret = nullptr;
50  const ExprPtr target = nullptr;
51  const SortPtr ret_type = nullptr;
52  std::vector<SortPtr> args;
53  };
54 
55  // ------------------------- MEMBERS -------------------------------------- //
57  InstrLvlAbsPtr m_;
58 
60  std::map<std::string, CxxFunc*> functions_;
62  std::map<std::string, CxxFunc*> externs_;
64  std::map<std::string, CxxFunc*> memory_updates_;
66  std::set<std::string> source_files_;
68  std::set<ExprPtr> const_mems_;
70  std::set<ExprPtr> global_vars_;
71 
72  // ------------------------- HELPERS -------------------------------------- //
74  void Reset();
76  bool SanityCheck() const;
78  bool Bootstrap(const std::string& root, bool opt);
79 
81  bool GenerateIlaBasics(const std::string& dir);
83  bool GenerateInstrContent(const InstrPtr& instr, const std::string& dir);
85  bool GenerateMemoryUpdate(const std::string& dir);
87  bool GenerateConstantMemory(const std::string& dir);
89  bool GenerateInitialSetup(const std::string& dir);
91  bool GenerateExecuteKernel(const std::string& dir);
93  bool GenerateGlobalHeader(const std::string& dir);
95  bool GenerateBuildSupport(const std::string& dir);
96 
98  bool RenderExpr(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut);
100  void DfsExpr(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut);
102  void DfsVar(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut) const;
104  void DfsConst(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut);
106  void DfsOp(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut);
108  void DfsOpMemory(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut);
110  void DfsOpAppFunc(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut);
112  void DfsOpSpecial(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut);
114  void DfsOpRegular(const ExprPtr& expr, StrBuff& buff, ExprVarMap& lut) const;
115 
117  CxxFunc* RegisterFunction(const std::string& func_name,
118  ExprPtr return_expr = nullptr);
120  CxxFunc* RegisterExternalFunc(const FuncPtr& func);
122  CxxFunc* RegisterMemoryUpdate(const ExprPtr& expr);
123 
125  void BeginFuncDef(CxxFunc* func, StrBuff& buff) const;
127  void EndFuncDef(CxxFunc* func, StrBuff& buff) const;
129  void WriteFuncDecl(CxxFunc* func, StrBuff& buff) const;
131  void CommitSource(const std::string& file_name, const std::string& dir,
132  const StrBuff& buff);
133 
135  inline std::string GetProjectName() const { return m_->name().str(); }
137  static inline std::string GetLocalVar(const ExprVarMap& lut) {
138  return fmt::format("local_var_{}", lut.size());
139  }
141  static inline std::string GetCxxType(const ExprPtr& expr) {
142  return GetCxxType(expr->sort());
143  }
145  static std::string GetCxxType(const SortPtr& sort);
147  static std::string GetCxxName(const ExprPtr& expr);
149  static std::string GetValidFuncName(const InstrLvlAbsCnstPtr& m);
151  static std::string GetDecodeFuncName(const InstrPtr& instr);
153  static std::string GetUpdateFuncName(const InstrPtr& instr);
155  static std::string GetMemoryFuncName(const ExprPtr& expr);
157  inline std::string LookUp(const ExprPtr& e, const ExprVarMap& lut) const {
158  auto pos = lut.find(e);
159  ILA_ASSERT(pos != lut.end());
160  return pos->second;
161  }
162 
163 }; // class Ilator
164 
165 } // namespace ilang
166 
167 #endif // ILANG_TARGET_SC_ILATOR_H__
#define ILA_ASSERT(b)
Assertion with message logged to FATAL (lvl 3). (Debug)
Definition: log.h:33
The ILAtor class - for CMake-based SystemC simulator generation.
Definition: ilator.h:21
Expr::ExprPtr ExprPtr
Pointer type for normal use of Expr.
Definition: expr.h:138
InstrLvlAbs::InstrLvlAbsCnstPtr InstrLvlAbsCnstPtr
Pointer type for read-only usage of InstrLvlAbs.
Definition: instr_lvl_abs.h:328
Ilator(const InstrLvlAbsPtr &m)
Constructor with a fixed ILA model.
Sort::SortPtr SortPtr
Pointer type for storing/passing Sort.
Definition: sort.h:82
void Generate(const std::string &dst, bool opt)
Generate the SystemC simulator.
Instr::InstrPtr InstrPtr
Pointer type for normal use of Instr.
Definition: instr.h:132
Func::FuncPtr FuncPtr
Pointer type for normal use of Func.
Definition: func.h:83
InstrLvlAbs::InstrLvlAbsPtr InstrLvlAbsPtr
Pointer type for normal use of InstrLvlAbs.
Definition: instr_lvl_abs.h:326
~Ilator()
Destructor.