Skip to content

Commit

Permalink
Map of declaration to instances should be available in matchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Dec 19, 2019
1 parent c4a7750 commit 37bc6f3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 27 deletions.
26 changes: 18 additions & 8 deletions src/Matchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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_);
Expand Down
71 changes: 52 additions & 19 deletions src/Matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
typedef std::tuple<std::string, Decl *> InstanceDeclType;
typedef std::vector<InstanceDeclType> InstanceDeclarationsType;

typedef std::tuple<std::string, FieldDecl *> InstanceFieldType;
typedef std::tuple<std::string, VarDecl *> InstanceVarType;
typedef std::vector<std::tuple<std::string, FieldDecl *> > instance_fields;
typedef std::vector<std::tuple<std::string, VarDecl *> > instance_vars;
private:
InstanceDeclarationsType instances_;

public:
// Finds the instance with the same type as the argument.
Expand All @@ -52,7 +50,7 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
}
}
} else {
// VarDecl
// This is a VarDecl instance.
auto p_var{dyn_cast<VarDecl>(p_field_var_decl)};
auto qtype{p_var->getType().getTypePtr()};
if (qtype->isRecordType()) {
Expand Down Expand Up @@ -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 */

Expand All @@ -103,8 +102,35 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
result.Nodes.getNodeAs<VarDecl>("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<CXXConstructExpr *>(
result.Nodes.getNodeAs<CXXConstructExpr>("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<Stmt *>(
result.Nodes.getNodeAs<Stmt>("constructor_arg"))) {
cout << "Found instance name: " << instance_name->getStmtClassName()
<< ": \n";
instance_name->dump();
}
}
}

void dump() {
Expand All @@ -122,9 +148,7 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
}
}

private:
InstanceDeclarationsType instances_;
};
}; // namespace sc_ast_matchers

// FieldMatcher class
class FieldMatcher : public MatchFinder::MatchCallback {
Expand Down Expand Up @@ -158,7 +182,8 @@ class FieldMatcher : public MatchFinder::MatchCallback {
std::vector<std::string> input_port_names;
};

//

///////////////////////////////////////////////////////////////////////////////
// Class ModuleDeclarationMatcher
//
//
Expand All @@ -169,24 +194,21 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
ModuleDeclarationType;
typedef std::vector<std::tuple<std::string, PortDecl *> > PortType;

typedef std::vector<InstanceMatcher::InstanceDeclType> InstanceListType;
typedef std::pair<CXXRecordDecl*, InstanceListType > DeclarationInstancePairType;
typedef std::map< CXXRecordDecl*, InstanceListType > DeclarationsToInstancesMapType;

private:
// Template functions.
template <typename T>
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;
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/SystemCClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 37bc6f3

Please sign in to comment.