diff --git a/src/Matchers.cpp b/src/Matchers.cpp index 4a916f297..36d5d4f0d 100644 --- a/src/Matchers.cpp +++ b/src/Matchers.cpp @@ -221,19 +221,26 @@ void ModuleDeclarationMatcher::pruneMatches() { for (auto const &element : found_declarations_) { auto decl{get<1>(element)}; //std::cout << "## fd name: " << get<0>(element) << "\n "; + InstanceListType instance_list; if (instance_matcher.findInstance(decl)) { std::cout << "## GOOD MODULE: " << get<0>(element) << std::endl; pruned_declarations_.push_back(element); + instance_list.push_back(element); } + + declaration_instance_map_.insert(DeclarationInstancePairType(decl, instance_list)); } for (auto const &element : found_template_declarations_) { auto decl{get<1>(element)}; //std::cout << "## ftd name: " << get<0>(element) << "\n "; + InstanceListType instance_list; if (instance_matcher.findInstance(decl)) { std::cout << "## GOOD TEMPLATE MODULE: " << get<0>(element) << std::endl; pruned_declarations_.push_back(element); + instance_list.push_back(element); } + declaration_instance_map_.insert(DeclarationInstancePairType(decl, instance_list)); } } @@ -256,14 +263,17 @@ void ModuleDeclarationMatcher::dump() { // Print the instances. instance_matcher.dump(); - // Map structure - // Input ports - // for ( const auto &i: in_ports_ ) { - // llvm::outs() << "name: " << i.first << " "; - // (i.second)->printTemplateArguments(llvm::outs()); - // llvm::outs() << "\n"; - // } - // + cout << "\n## Dump map of decl->instances\n"; + + for (const auto &i : declaration_instance_map_ ) { + auto decl{ i.first }; + auto instance_list{ i.second }; + + cout << "decl: " << decl->getIdentifier()->getNameStart(); + for (const auto &instance : instance_list ) { + cout << ", instance type: " << get<0>(instance) << ", " << get<1>(instance) << "\n"; + } + } cout << "## Printing ports" << endl; printTemplateArguments(clock_ports_); diff --git a/src/Matchers.h b/src/Matchers.h index fe2b7cffd..a4dd8bba4 100644 --- a/src/Matchers.h +++ b/src/Matchers.h @@ -24,10 +24,8 @@ class InstanceMatcher : public MatchFinder::MatchCallback { typedef std::tuple InstanceDeclType; typedef std::vector InstanceDeclarationsType; - typedef std::tuple InstanceFieldType; - typedef std::tuple InstanceVarType; - typedef std::vector > instance_fields; - typedef std::vector > instance_vars; + private: + InstanceDeclarationsType instances_; public: // Finds the instance with the same type as the argument. @@ -52,7 +50,7 @@ class InstanceMatcher : public MatchFinder::MatchCallback { } } } else { - // VarDecl + // This is a VarDecl instance. auto p_var{dyn_cast(p_field_var_decl)}; auto qtype{p_var->getType().getTypePtr()}; if (qtype->isRecordType()) { @@ -82,7 +80,8 @@ class InstanceMatcher : public MatchFinder::MatchCallback { varDecl( hasType( cxxRecordDecl(isDerivedFrom(hasName("::sc_core::sc_module"))) - ) + ), + hasDescendant(cxxConstructExpr(hasArgument(0, stringLiteral().bind("constructor_arg"))).bind("constructor_expr")) ).bind("instances_in_vardecl"); /* clang-format on */ @@ -103,8 +102,35 @@ class InstanceMatcher : public MatchFinder::MatchCallback { result.Nodes.getNodeAs("instances_in_vardecl"))) { std::string name{instance->getIdentifier()->getNameStart()}; cout << "Found a member variable instance: " << name << endl; + instance->dump(); instances_.push_back(std::make_tuple(name, instance)); } + + if (auto instance_name = const_cast( + result.Nodes.getNodeAs("constructor_expr"))) { + cout << "Found constructor expression argument: " + << instance_name->getNumArgs() << "\n"; + auto first_arg{instance_name->getArg(0)}; + + cout << " L VALUE \n"; + first_arg->dump(); + + clang::LangOptions LangOpts; + LangOpts.CPlusPlus = true; + clang::PrintingPolicy Policy(LangOpts); + + std::string s; + llvm::raw_string_ostream sstream(s); + first_arg->printPretty(sstream, 0, Policy); + cout << "instance name: " << sstream.str() << "\n"; + + if (auto instance_name = const_cast( + result.Nodes.getNodeAs("constructor_arg"))) { + cout << "Found instance name: " << instance_name->getStmtClassName() + << ": \n"; + instance_name->dump(); + } + } } void dump() { @@ -122,9 +148,7 @@ class InstanceMatcher : public MatchFinder::MatchCallback { } } - private: - InstanceDeclarationsType instances_; -}; +}; // namespace sc_ast_matchers // FieldMatcher class class FieldMatcher : public MatchFinder::MatchCallback { @@ -158,7 +182,8 @@ class FieldMatcher : public MatchFinder::MatchCallback { std::vector input_port_names; }; -// + +/////////////////////////////////////////////////////////////////////////////// // Class ModuleDeclarationMatcher // // @@ -169,24 +194,21 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback { ModuleDeclarationType; typedef std::vector > PortType; + typedef std::vector InstanceListType; + typedef std::pair DeclarationInstancePairType; + typedef std::map< CXXRecordDecl*, InstanceListType > DeclarationsToInstancesMapType; + private: // Template functions. template void insert_port( PortType & port, T *decl ); - public: - void registerMatchers(MatchFinder &finder); - virtual void run(const MatchFinder::MatchResult &result); - const ModuleDeclarationType &getFoundModuleDeclarations() const; - const PortType &getFields(const std::string &port_type); - - void pruneMatches(); - void dump(); - private: ModuleDeclarationType found_declarations_; ModuleDeclarationType found_template_declarations_; ModuleDeclarationType pruned_declarations_; + DeclarationsToInstancesMapType declaration_instance_map_; + // Match nested instances InstanceMatcher instance_matcher; @@ -198,6 +220,17 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback { PortType inout_ports_; PortType other_fields_; PortType signal_fields_; + + + public: + const DeclarationsToInstancesMapType & getInstances() { return declaration_instance_map_; }; + void registerMatchers(MatchFinder &finder); + virtual void run(const MatchFinder::MatchResult &result); + const ModuleDeclarationType &getFoundModuleDeclarations() const; + const PortType &getFields(const std::string &port_type); + + void pruneMatches(); + void dump(); }; }; // namespace sc_ast_matchers diff --git a/src/SystemCClang.cpp b/src/SystemCClang.cpp index 3b71fd16a..b8b90a623 100644 --- a/src/SystemCClang.cpp +++ b/src/SystemCClang.cpp @@ -121,9 +121,19 @@ bool SystemCConsumer::fire() { // Find the netlist. //////////////////////////////////////////////////////////////// // This actually also finds instances, but now we have AST matchers to do it. + + /* FindNetlist findNetlist{scmain.getSCMainFunctionDecl()}; findNetlist.dump(); systemcModel_->addNetlist(findNetlist); + */ + + // + // Create a ModuleDecl for each instance with the appropriately parsed ModuleDecl. + // + + auto found_instances{ module_declaration_handler }; + // Go through each instance and find its appropriate module declaration. //////////////////////////////////////////////////////////////// // Figure out the module map.