Skip to content

Commit

Permalink
Migrate to using matchers (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Dec 16, 2019
1 parent df564c2 commit ec95e87
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 38 deletions.
31 changes: 30 additions & 1 deletion src/Matchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void ModuleDeclarationMatcher::run(const MatchFinder::MatchResult &result) {

const ModuleDeclarationMatcher::ModuleDeclarationType &
ModuleDeclarationMatcher::getFoundModuleDeclarations() const {
return found_declarations_;
return pruned_declarations_;
}

const ModuleDeclarationMatcher::PortType &ModuleDeclarationMatcher::getFields(
Expand All @@ -213,6 +213,30 @@ const ModuleDeclarationMatcher::PortType &ModuleDeclarationMatcher::getFields(
assert(true);
}

void ModuleDeclarationMatcher::pruneMatches() {
// Must have found instances.
// 1. For every module found, check if there is an instance.
// 2. If there is an instance, then add it into the list.

for (auto const &element : found_declarations_) {
auto decl{get<1>(element)};
//std::cout << "## fd name: " << get<0>(element) << "\n ";
if (instance_matcher.findInstance(decl)) {
std::cout << "## GOOD MODULE: " << get<0>(element) << std::endl;
pruned_declarations_.push_back(element);
}
}

for (auto const &element : found_template_declarations_) {
auto decl{get<1>(element)};
//std::cout << "## ftd name: " << get<0>(element) << "\n ";
if (instance_matcher.findInstance(decl)) {
std::cout << "## GOOD TEMPLATE MODULE: " << get<0>(element) << std::endl;
pruned_declarations_.push_back(element);
}
}
}

void ModuleDeclarationMatcher::dump() {
for (const auto &i : found_declarations_) {
cout << "module name : " << get<0>(i) << ", " << get<1>(i)
Expand All @@ -224,6 +248,11 @@ void ModuleDeclarationMatcher::dump() {
<< std::endl;
}

for (const auto &i : pruned_declarations_) {
cout << "pruned module name: " << get<0>(i) << ", " << get<1>(i)
<< std::endl;
}

// Print the instances.
instance_matcher.dump();

Expand Down
26 changes: 20 additions & 6 deletions src/Matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class InstanceMatcher : public MatchFinder::MatchCallback {

public:
// Finds the instance with the same type as the argument.
void findInstance(CXXRecordDecl *decl) {
bool findInstance(CXXRecordDecl *decl) {
// First check in the instance_fields.
// Check to see if the pointer to the type is the same as the sc_module
// type.
Expand All @@ -35,8 +35,8 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
// Get the CXXRecordDecl for the instance.
// The instance is the second element in the tuple.
auto qtype{get<1>(element)->getType().getTypePtr()};
if (qtype->isRecordType()) {
if (auto dp = qtype->getAs<TemplateSpecializationType>()) {
if (dp->isRecordType()) {
auto rt{dp->getAsCXXRecordDecl()};
return (rt == decl);
}
Expand All @@ -45,25 +45,39 @@ class InstanceMatcher : public MatchFinder::MatchCallback {

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()};
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;
}

return false;
}

void registerMatchers(MatchFinder &finder) {
Expand Down Expand Up @@ -94,9 +108,6 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
cout << "Found a member field instance: " << name << endl;
list_instance_fields_.push_back(std::make_tuple(name, instance));

// Get the pointer to the type declaration.
auto qtype{instance->getType().getTypePtr()};
qtype->dump();
}

if (auto instance = const_cast<VarDecl *>(
Expand All @@ -105,6 +116,7 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
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";
Expand All @@ -122,8 +134,8 @@ class InstanceMatcher : public MatchFinder::MatchCallback {
cout << "RECORD type: " << rt << "\n";
}
}
*/

qtype->dump();
}
}

Expand Down Expand Up @@ -193,11 +205,13 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
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_;

// Match nested instances
InstanceMatcher instance_matcher;
Expand Down
2 changes: 1 addition & 1 deletion src/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using namespace std;
class Model {
public:
typedef pair<string, ModuleDecl *> modulePairType;
typedef map<string, ModuleDecl *> moduleMapType;
typedef multimap<string, ModuleDecl *> moduleMapType;

typedef pair<string, EventContainer *> eventPairType;
typedef map<string, EventContainer *> eventMapType;
Expand Down
21 changes: 17 additions & 4 deletions src/SystemCClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,29 @@ bool SystemCConsumer::fire() {
// This is important so that the top-level module can be found.
//
os_ << "================ TESTMATCHER =============== \n";

//InstanceMatcher match_instances{};
ModuleDeclarationMatcher module_declaration_handler{};
MatchFinder matchRegistry{};
module_declaration_handler.registerMatchers(matchRegistry);

// Run all the matchers

module_declaration_handler.registerMatchers(matchRegistry);
//match_instances.registerMatchers( matchRegistry );

matchRegistry.matchAST(getContext());
//match_instances.dump();
module_declaration_handler.pruneMatches();
os_ << "== Print module declarations pruned\n";
module_declaration_handler.dump();
os_ << "================ END =============== \n";

// Check if the top-level module one of the sc_module declarations?
//
auto found_module_declarations{
module_declaration_handler.getFoundModuleDeclarations()};

os_ << "@@@@@@@@@@@@ FOUND MODUE: " << found_module_declarations.size() <<"\n";
auto found_top_module{std::find_if(
found_module_declarations.begin(), found_module_declarations.end(),
[this](std::tuple<std::string, CXXRecordDecl *> &element) {
Expand All @@ -66,17 +77,18 @@ bool SystemCConsumer::fire() {
// Every module declaration that is found should be added to the model.
//

/*
for (auto const &element : found_module_declarations) {
auto module_declaration{new ModuleDecl{get<0>(element), get<1>(element)}};
os_ << "@@@@@ name: " << get<0>(element) << "\n";
systemcModel_->addModuleDecl(module_declaration);
}
*/
auto mdv{ systemcModel_->getModuleDecl() };
os_ << "@@@@@@@ SIZE MDV: " << mdv.size() << "\n";

/*
// Find the sc_modules
//
//
FindSCModules scmod{tu, os_};
FindSCModules::moduleMapType scmodules{scmod.getSystemCModulesMap()};
Expand All @@ -88,6 +100,7 @@ bool SystemCConsumer::fire() {
systemcModel_->addModuleDecl(md);
}
*/

////////////////////////////////////////////////////////////////
// Find the sc_main
Expand Down
7 changes: 3 additions & 4 deletions tests/data/matcher-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,17 @@ SC_MODULE( test ){
}
};

/*
int sc_main(int argc, char *argv[]) {
sc_signal<int> sig1;
sc_signal<double> double_sig;
test test_instance("testing");
test_instance.in1(sig1);
test_instance.in_out(double_sig);
test_instance.in_out1(double_sig);
test_instance.out1(sig1);

simple_module simple("simple_module");
/*simple_module simple("simple_module");
simple.one(sig1);
*/

return 0;
}
*/
10 changes: 10 additions & 0 deletions tests/data/templated-module.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "systemc.h"

SC_MODULE( non_template ) {
int x;

void ef() {}
SC_CTOR(non_template) {
SC_METHOD(ef) {}
};
};

Expand All @@ -14,6 +18,10 @@ SC_MODULE( test ){

sc_in<T> inT;
sc_in<T> outT;

// FieldDecl of an sc_module
//non_template simple_module;

void entry_function_1() {
while(true) {
// do nothing
Expand All @@ -34,5 +42,7 @@ int sc_main(int argc, char *argv[]) {
test_instance.inS(sig1);
test_instance.outS(sig1);

non_template nt("non-templated-module-instance");

return 0;
}
24 changes: 13 additions & 11 deletions tests/sreg-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@ TEST_CASE("sreg example",
auto model{sc.getSystemCModel()};
auto module_decl{model->getModuleDecl()};

cout << "MODULE SIZE: " << module_decl.size();
SECTION("Found sc_modules", "[modules]") {
REQUIRE(module_decl.size() == 2);
REQUIRE(module_decl["test"] != nullptr);
REQUIRE(module_decl["sreg"] != nullptr);
INFO( "ERROR: number of sc_module declarations found: " << module_decl.size() );
CHECK(module_decl.size() == 5);
REQUIRE(module_decl.find("test") != module_decl.end() );
REQUIRE(module_decl.find("sreg") != module_decl.end() );
}

SECTION("Found sreg instances", "[instances]") {
auto module_instances{model->getModuleInstanceMap()};
auto p_module{module_decl["sreg"]};
REQUIRE(module_instances[p_module].size() == 3);
auto p_module{module_decl.find("sreg")};
REQUIRE(module_instances[p_module->second].size() == 3);
}

SECTION("Checking sreg_bypass ports", "[ports]") {
auto module_instances{model->getModuleInstanceMap()};
auto p_module{module_decl["sreg"]};
auto sreg_bypass{module_instances[p_module][0]};
auto p_module{module_decl.find("sreg")};
auto sreg_bypass{module_instances[p_module->second][0]};

REQUIRE(sreg_bypass->getIPorts().size() == 2);
REQUIRE(sreg_bypass->getOPorts().size() == 0);
Expand All @@ -64,8 +66,8 @@ TEST_CASE("sreg example",

SECTION("Checking sreg_fwd ports", "[ports]") {
auto module_instances{model->getModuleInstanceMap()};
auto p_module{module_decl["sreg"]};
auto sreg_fwd{module_instances[p_module][1]};
auto p_module{module_decl.find("sreg")};
auto sreg_fwd{module_instances[p_module->second][1]};

REQUIRE(sreg_fwd->getIPorts().size() == 2);
REQUIRE(sreg_fwd->getOPorts().size() == 0);
Expand All @@ -78,8 +80,8 @@ TEST_CASE("sreg example",

SECTION("Checking sreg_fwd_rev ports", "[ports]") {
auto module_instances{model->getModuleInstanceMap()};
auto p_module{module_decl["sreg"]};
auto sreg_fwd_rev{module_instances[p_module][2]};
auto p_module{module_decl.find("sreg")};
auto sreg_fwd_rev{module_instances[p_module->second][2]};

REQUIRE(sreg_fwd_rev->getIPorts().size() == 2);
REQUIRE(sreg_fwd_rev->getOPorts().size() == 0);
Expand Down
13 changes: 7 additions & 6 deletions tests/t1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ int sc_main(int argc, char *argv[]) {

SECTION("Found sc_modules", "[modules]") {
// There should be 2 modules identified.
INFO( "ERROR: number of sc_module declarations found: " << module_decl.size() );
REQUIRE(module_decl.size() == 2);

// Check their names, and that their pointers are not nullptr.
REQUIRE(module_decl["test"] != nullptr);
REQUIRE(module_decl["simple_module"] != nullptr);
REQUIRE(module_decl.find("test") != module_decl.end() );
REQUIRE(module_decl.find("simple_module") != module_decl.end() );

SECTION("Checking member ports for test instance", "[ports]") {
// These checks should be performed on the declarations.
Expand All @@ -113,8 +114,8 @@ int sc_main(int argc, char *argv[]) {
// This is necessary until the parsing code is restructured.
// There is only one module instance
auto module_instances{model->getModuleInstanceMap()};
auto p_module{module_decl["test"]};
auto test_module{module_instances[p_module].front()};
auto p_module{module_decl.find("test")};
auto test_module{module_instances[p_module->second].front()};

// Check if the proper number of ports are found.
REQUIRE(test_module->getIPorts().size() == 3);
Expand All @@ -128,8 +129,8 @@ int sc_main(int argc, char *argv[]) {

SECTION("Checking member ports for simple module instance", "[ports]") {
auto module_instances{model->getModuleInstanceMap()};
auto p_module{module_decl["simple_module"]};
auto test_module{module_instances[p_module].front()};
auto p_module{module_decl.find("simple_module")};
auto test_module{module_instances[p_module->second].front()};

// Check if the proper number of ports are found.
REQUIRE(test_module->getIPorts().size() == 3);
Expand Down
4 changes: 2 additions & 2 deletions tests/t2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ int sc_main(int argc, char *argv[]) {

SECTION("No ports bound for test declaration", "[ports]") {
// The module instances have all the information.
auto test_module{module_decl["test"]};
auto test_module{module_decl.find("test")};
// There is only one module instance

// Check if the proper number of ports are found.
INFO("FAIL_TEST: A module must have a port bound for it to be recognized.");
REQUIRE(test_module != nullptr);
REQUIRE(test_module != module_decl.end() );
}
}
4 changes: 2 additions & 2 deletions tests/t3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ TEST_CASE("Read SystemC model from file for testing", "[parsing]") {

SECTION("No ports bound for test declaration", "[ports]") {
// The module instances have all the information.
auto test_module{module_decl["test"]};
auto test_module{module_decl.find("test")};
// There is only one module instance

// Check if the proper number of ports are found.
INFO(
"FAIL_TEST: A module must have a port bound for it to be "
"recognized.");
REQUIRE(test_module != nullptr);
REQUIRE(test_module != module_decl.end() );
}
}
Loading

0 comments on commit ec95e87

Please sign in to comment.