Skip to content

Commit

Permalink
Instances stored as Decl for both FieldDecl and VarDecl.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Dec 17, 2019
1 parent 3409ebf commit c4a7750
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/Matchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void ModuleDeclarationMatcher::dump() {
// }
//

cout << "Printing ports" << endl;
cout << "## Printing ports" << endl;
printTemplateArguments(clock_ports_);
printTemplateArguments(in_ports_);
printTemplateArguments(out_ports_);
Expand Down
113 changes: 44 additions & 69 deletions src/Matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ using namespace scpar;

namespace sc_ast_matchers {

//
// InstanceMatcher
//
//
class InstanceMatcher : public MatchFinder::MatchCallback {
public:
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;
Expand All @@ -29,54 +35,37 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
// First check in the instance_fields.
// Check to see if the pointer to the type is the same as the sc_module
// type.
auto it = std::find_if(
list_instance_fields_.begin(), list_instance_fields_.end(),
[&decl](const InstanceFieldType &element) {

auto found_it = std::find_if(
instances_.begin(), instances_.end(),
[&decl](const InstanceDeclType &element) {
// Get the CXXRecordDecl for the instance.
// The instance is the second element in the tuple.
auto qtype{get<1>(element)->getType().getTypePtr()};
auto p_field_var_decl{get<1>(element)};

if (auto *p_field{dyn_cast<FieldDecl>(p_field_var_decl)}) {
auto qtype{p_field->getType().getTypePtr()};
if (qtype->isRecordType()) {
if (auto dp = qtype->getAs<TemplateSpecializationType>()) {
auto rt{dp->getAsCXXRecordDecl()};
return (rt == decl);
if (auto dp = qtype->getAs<TemplateSpecializationType>()) {
auto rt{dp->getAsCXXRecordDecl()};
return (rt == decl);
}
}
}
});

if (it != list_instance_fields_.end()) {
std::cout << "FOUND a FIELD instance: " << std::endl;
return true;
}

auto vit = std::find_if(
list_instance_vars_.begin(), list_instance_vars_.end(),
[&decl](const InstanceVarType &element) {
// Get the CXXRecordDecl for the instance.
// The instance is the second element in the tuple.
// Returns a Type*
auto qtype{get<1>(element)->getType().getTypePtr()};
/*
if (auto dp = qtype->getAs<TemplateSpecializationType>()) {
if (dp->isRecordType()) {
auto rt{dp->getAsCXXRecordDecl()};
} else {
// VarDecl
auto p_var{dyn_cast<VarDecl>(p_field_var_decl)};
auto qtype{p_var->getType().getTypePtr()};
if (qtype->isRecordType()) {
auto rt{qtype->getAsCXXRecordDecl()};
return (rt == decl);
}
} else
*/

if ( qtype->isRecordType() ) {
std::cout << " ==> class type\n";
auto rt { qtype->getAsCXXRecordDecl() };
std::cout << " r: " << rt << " :::: " << qtype->getAsRecordDecl() << endl;
return ( rt == decl );
}
});

if ( vit != list_instance_vars_.end()) {
std::cout << "FOUND a VAR instance: " << std::endl;
return true;
if (found_it != instances_.end()) {
std::cout << "FOUND AN FIELD instance: " << std::endl;
return true;
}

return false;
}

Expand Down Expand Up @@ -106,53 +95,35 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
result.Nodes.getNodeAs<FieldDecl>("instances_in_fielddecl"))) {
std::string name{instance->getIdentifier()->getNameStart()};
cout << "Found a member field instance: " << name << endl;
list_instance_fields_.push_back(std::make_tuple(name, instance));

instances_.push_back(std::make_tuple(name, instance));
}

if (auto instance = const_cast<VarDecl *>(
result.Nodes.getNodeAs<VarDecl>("instances_in_vardecl"))) {
std::string name{instance->getIdentifier()->getNameStart()};
cout << "Found a member variable instance: " << name << endl;
list_instance_vars_.push_back(std::make_tuple(name, instance));

/*
// const Type * returned
//
cout << "Figure out type of vardecl\n";
auto qtype{instance->getType().getTypePtr()};
if (auto dp = qtype->getAs<TemplateSpecializationType>()) {
auto tn{dp->getTemplateName()};
auto tunder{tn.getUnderlying()};
auto name{tunder.getAsTemplateDecl()->getNameAsString()};
cout << "template name: \n";
tn.dump();
cout << ", NAME: " << name << endl;
if (dp->isRecordType()) {
auto rt{dp->getAsCXXRecordDecl()};
cout << "RECORD type: " << rt << "\n";
}
}
*/

instances_.push_back(std::make_tuple(name, instance));
}
}

void dump() {
for (const auto &i : list_instance_fields_) {
cout << "fields module name: " << get<0>(i) << ", " << get<1>(i)
<< std::endl;
}
for (const auto &i : list_instance_vars_) {
cout << "vars module name: " << get<0>(i) << ", " << get<1>(i)
// Instances holds both FieldDecl and VarDecl as its base class Decl.
for (const auto &i : instances_) {
cout << "module declarations name: " << get<0>(i) << ", " << get<1>(i)
<< std::endl;

auto p_field_var_decl{get<1>(i)};
if (isa<FieldDecl>(p_field_var_decl)) {
cout << " ==> FieldDecl\n";
} else {
cout << " ==> VarDecl\n";
}
}
}

private:
instance_fields list_instance_fields_;
instance_vars list_instance_vars_;
InstanceDeclarationsType instances_;
};

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

//
// Class ModuleDeclarationMatcher
//
//
class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
//
public:
Expand Down
6 changes: 4 additions & 2 deletions src/SystemCClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ 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);
Expand All @@ -129,11 +130,12 @@ bool SystemCConsumer::fire() {
////////////////////////////////////////////////////////////////
Model::moduleMapType moduleMap{systemcModel_->getModuleDecl()};

// <string, ModuleDecl*>
for (Model::moduleMapType::iterator mit = moduleMap.begin(),
mitend = moduleMap.end();
mit != mitend; mit++) {
ModuleDecl *mainmd{mit->second};
int numInstances{mainmd->getNumInstances()};
ModuleDecl *mainmd{ mit->second };
int numInstances{ mainmd->getNumInstances() };
vector<ModuleDecl *> moduleDeclVec;

os_ << "\nFor module: " << mit->first << " num instance : " << numInstances << "\n";
Expand Down

0 comments on commit c4a7750

Please sign in to comment.