systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
SystemCClang.cpp
Go to the documentation of this file.
1#include "SystemCClang.h"
2
4
9//#include "model/Model.h"
10
11#include "clang/AST/ASTImporter.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14
15using namespace systemc_clang;
16
17using namespace sc_ast_matchers;
18using namespace sc_ast_matchers::utils;
19
20bool SystemCConsumer::preFire() { return true; }
21
22bool SystemCConsumer::postFire() { return true; }
23
25 FindSCMain *scmain, ModuleDeclarationMatcher *module_declaration_handler) {
26 LLVM_DEBUG(
27 llvm::dbgs() << "=============== ##### TEST NetlistMatcher ##### \n";);
28 NetlistMatcher netlist_matcher{};
29 MatchFinder netlist_registry{};
30 netlist_matcher.registerMatchers(netlist_registry, systemc_model_,
31 module_declaration_handler);
32
33 netlist_registry.match(*(scmain->getSCMainFunctionDecl()), getContext());
34
35 auto instances{systemc_model_->getInstances()};
36 for (const auto &inst : instances) {
37 ModuleInstance *mdecl{inst};
38 auto ctordecl{mdecl->getConstructorDecl()};
39 if (ctordecl != nullptr) {
40 const FunctionDecl *fd{dyn_cast<FunctionDecl>(ctordecl)};
41 ctordecl->getBody(fd);
42
43 LLVM_DEBUG(llvm::dbgs() << "==============> RUN netlist matcher: "
44 << mdecl->getInstanceName() << "\n";);
45 // fd->dump();
46 netlist_registry.match(*fd, getContext());
47 LLVM_DEBUG(llvm::dbgs() << "==============> DONE netlist matcher\n";);
48 //}
49 }
50 }
51 LLVM_DEBUG(netlist_matcher.dump();
52 llvm::dbgs() << "##### END TEST NetlistMatcher ##### \n";);
53}
54
56 clang::TranslationUnitDecl *tu{getContext().getTranslationUnitDecl()};
57 // Reflection database.
58 systemc_model_ = new Model{};
59
60 FindGlobalEvents globals{tu, os_};
63
64 ModuleDeclarationMatcher module_declaration_handler{};
65 MatchFinder matchRegistry{};
66 module_declaration_handler.registerMatchers(matchRegistry);
67 matchRegistry.matchAST(getContext());
68 module_declaration_handler.processInstanceCXXDecls(getContext());
69
70 LLVM_DEBUG(llvm::dbgs() << "== Print module declarations pruned\n";
71 module_declaration_handler.dump();
72 llvm::dbgs() << "================ END =============== \n";);
74 // Find the sc_main
76 FindSCMain scmain{tu, os_};
77
78 if (scmain.isSCMainFound()) {
79 clang::FunctionDecl *fnDecl{scmain.getSCMainFunctionDecl()};
80
81 // TODO: find any instances in sc_main.
82
83 FindSimTime scstart{fnDecl, os_};
84 systemc_model_->addSimulationTime(scstart.returnSimTime());
85 } else {
86 LLVM_DEBUG(llvm::dbgs() << "\n Could not find SCMain";);
87 }
88 //
89 // Create a ModuleInstance for each instance with the appropriately parsed
90 // ModuleInstance.
91 //
92
93 // MultiMap CXXRecordDecl => ModuleInstance*
94 // Instances with same CXXRecordDecl will have multiple entries
95 auto found_module_declarations{
96 module_declaration_handler.getFoundModuleDeclarations()};
97
98 LLVM_DEBUG(llvm::dbgs() << "############# ============= NEW FIRE "
99 "============ ################\n";
100 llvm::dbgs() << "Size of module instances: "
101 << found_module_declarations.size() << "\n";);
102
103 for (const auto &inst : found_module_declarations) {
104 clang::CXXRecordDecl *cxx_decl{inst.first};
105 ModuleInstance *add_module_decl{inst.second};
106
108 // processModuleDeclaration(cxx_decl, add_module_decl);
109
111 // auto base_decls{getAllBaseClasses(cxx_decl)};
112 // for (const auto &base: base_decls) {
113 // auto name{ base->getNameAsString() };
114 // ModuleInstance *base_module_instance{ new ModuleInstance{name, base} };
115 // processModuleDeclaration(const_cast<clang::CXXRecordDecl*>(base),
116 // base_module_instance); llvm::dbgs() << " ############### Base module
117 // instance ################# \n"; add_module_decl->addBaseInstance(
118 // base_module_instance);
119
120 /*
121for (const auto &base_decl : base_decls) {
122 llvm::dbgs() << "=============================== BASES " <<
123decl->getNameAsString() << " =======================\n"; llvm::dbgs() << "Run
124base instance matcher: "
125 << base_decl->getNameAsString() << " \n";
126 InstanceMatcher base_instance_matcher;
127 MatchFinder base_instance_reg{};
128 base_instance_matcher.registerMatchers(base_instance_reg);
129 base_instance_matcher.setParentFieldDecl(vd);
130 base_instance_reg.match(*base_decl, context);
131 llvm::dbgs() << "+ Dump base instance matcher\n";
132 base_instance_matcher.dump();
133 llvm::dbgs() << "+ End dump base instance matcher\n";
134
136 instance_matcher_= base_instance_matcher;
137 }
138
139*/
140
141 //}
142
143 systemc_model_->addInstance(add_module_decl);
144 }
145
146 LLVM_DEBUG(
147 llvm::dbgs() << "############# ===== END NEW FIRE ============ "
148 "################\n";
149
150 llvm::dbgs() << "=============== Populate sub-modules ============= \n";);
151 // This must have the instance matcher already run.
152 // You need systemc_model_ and instance_matcher to build the hierarchy of
153 // sub-modules.
154 auto instance_matcher{module_declaration_handler.getInstanceMatcher()};
155 auto instance_map{instance_matcher.getInstanceMap()};
156
157 LLVM_DEBUG(
158 llvm::dbgs() << "- Print out all the instances in the instance map\n";);
159
161 LLVM_DEBUG(
162 llvm::dbgs() << "===========END Populate sub-modules ============= \n";);
163
164 // Netlist matcher code
165 //
166 // All instances are within the SystemC model.
167 // This must come after instances of ModuleInstance have been generated.
168 // This is because the netlist matcher inserts the port bindings into the
169 // instance.
170
171 processNetlist(&scmain, &module_declaration_handler);
172
173 LLVM_DEBUG(
174 llvm::dbgs() << "\nParsed SystemC model from systemc-clang\n";
175 llvm::dbgs() << "============= MODEL ============================\n";
176 systemc_model_->dump(llvm::dbgs());
177 llvm::dbgs() << "==============END========================\n";);
178 return true;
179}
180
181void SystemCConsumer::HandleTranslationUnit(clang::ASTContext &context) {
182 // ///////////////////////////////////////////////////////////////
183 // / Pass 1: Find the necessary information.
184 // ///////////////////////////////////////////////////////////////
185
186 bool pre = false;
187 pre = preFire();
188
189 if (!pre) {
190 return;
191 }
192
193 bool f = false;
194 f = fire();
195
196 if (!f) {
197 return;
198 }
199 postFire();
200}
201
202SystemCConsumer::SystemCConsumer(clang::ASTUnit *from_ast, std::string top)
203 : os_{llvm::errs()},
204 sm_{from_ast->getSourceManager()},
205 context_{from_ast->getASTContext()},
206 top_{top},
207 systemc_model_{nullptr} {}
208
209SystemCConsumer::SystemCConsumer(clang::CompilerInstance &ci, std::string top)
210 : os_{llvm::errs()},
211 sm_{ci.getSourceManager()},
212 context_{ci.getASTContext()},
213 top_{top},
214 systemc_model_{nullptr} {}
215
217 if (systemc_model_ != nullptr) {
218 delete systemc_model_;
219 systemc_model_ = nullptr;
220 }
221}
222
223void SystemCConsumer::setTopModule(const std::string &top_module_decl) {
224 top_ = top_module_decl;
225}
226
228
229const std::string &SystemCConsumer::getTopModule() const { return top_; }
230
231clang::ASTContext &SystemCConsumer::getContext() const { return context_; }
232
233clang::SourceManager &SystemCConsumer::getSourceManager() const { return sm_; }
Class ModuleDeclarationMatcher.
Definition Matchers.h:40
void registerMatchers(MatchFinder &finder)
Register the matchers.
Definition Matchers.h:61
void registerMatchers(MatchFinder &finder, systemc_clang::Model *model, ModuleDeclarationMatcher *module_matcher)
std::map< std::string, clang::VarDecl * > globalEventMapType
clang::FunctionDecl * getSCMainFunctionDecl() const
void populateNestedModules()
Definition Model.cpp:9
void addGlobalEvents(FindGlobalEvents::globalEventMapType)
Definition Model.cpp:90
void addSimulationTime(FindSimTime::simulationTimeMapType)
Definition Model.cpp:80
void dump(raw_ostream &)
Definition Model.cpp:141
void addInstance(ModuleInstance *)
Definition Model.cpp:76
std::vector< ModuleInstance * > & getInstances()
Definition Model.cpp:133
Forward declarations.
const clang::CXXConstructorDecl * getConstructorDecl() const
const std::string & getTopModule() const
clang::SourceManager & sm_
clang::SourceManager & getSourceManager() const
clang::ASTContext & context_
SystemCConsumer(clang::CompilerInstance &, std::string top="!none")
virtual void HandleTranslationUnit(clang::ASTContext &context)
void setTopModule(const std::string &top_module_decl)
void processNetlist(FindSCMain *scmain, ModuleDeclarationMatcher *module_declaration_handler)
clang::ASTContext & getContext() const