Skip to content

Commit

Permalink
Seem to have figured out others matcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Jan 1, 2020
1 parent 3738edc commit 79e4a96
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 56 deletions.
95 changes: 72 additions & 23 deletions src/Matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ class PortMatcher : public MatchFinder::MatchCallback {
auto makeFieldMatcher(const std::string &name) {
/* clang-format off */
return cxxRecordDecl(
//isExpansionInMainFile(),
//isDerivedFrom(
// hasName("::sc_core::sc_module")
// ),
isExpansionInMainFile(),
isDerivedFrom(
hasName("::sc_core::sc_module")
),
forEach(
fieldDecl(hasType(cxxRecordDecl(hasName(name)))).bind(name)
)
Expand Down Expand Up @@ -278,19 +278,7 @@ class PortMatcher : public MatchFinder::MatchCallback {
unless(isDerivedFrom(matchesName("sc_event_queue")))
);

auto match_non_sc_types = cxxRecordDecl(
match_module_decls,
forEach(
fieldDecl(
allOf(
unless(hasType(cxxRecordDecl(matchesName("sc*")))),
unless(hasType(namedDecl(hasName("sc_in_clk"))))
)
).bind("other_fields")
)
);

auto match_sc_signal= cxxRecordDecl(
auto match_sc_signal= cxxRecordDecl(
match_module_decls,
forEach(
fieldDecl(
Expand All @@ -314,13 +302,63 @@ class PortMatcher : public MatchFinder::MatchCallback {
);

/* clang-format on */
auto match_all_ports = cxxRecordDecl(
match_module_decls,
eachOf(
forEach(fieldDecl(hasType(namedDecl(hasName("sc_in_clk"))))
.bind("sc_in_clk")),
forEach(fieldDecl(hasType(cxxRecordDecl(hasName("sc_in"))))
.bind("sc_in")),
forEach(fieldDecl(hasType(cxxRecordDecl(hasName("sc_out"))))
.bind("sc_out")),
forEach(fieldDecl(hasType(cxxRecordDecl(hasName("sc_inout"))))
.bind("sc_inout")),
forEach(fieldDecl(
hasType(cxxRecordDecl(isDerivedFrom(hasName("sc_signal_inout_if"))))
).bind("sc_signal")),
forEach(fieldDecl(hasType(cxxRecordDecl(hasName("sc_stream_in"))))
.bind("sc_stream_in")),
forEach(fieldDecl(hasType(cxxRecordDecl(hasName("sc_stream_out"))))
.bind("sc_stream_out"))
)

/*
forEach(fieldDecl(hasType(namedDecl(hasName("sc_in_clk")))).bind("sc_in_clk"),
fieldDecl(hasType(cxxRecordDecl(hasName("sc_in")))).bind("sc_in"),
fieldDecl(hasType(cxxRecordDecl(hasName("sc_out")))).bind("sc_out"),
fieldDecl(hasType(cxxRecordDecl(hasName("sc_inout")))).bind("sc_inout"),
fieldDecl(hasType(cxxRecordDecl(hasName("sc_signal")))).bind("sc_signal"),
fieldDecl(hasType(cxxRecordDecl(hasName("sc_stream_in")))).bind("sc_stream_in"),
fieldDecl(hasType(cxxRecordDecl(hasName("sc_stream_out")))).bind("sc_stream_out")
)
*/
);

auto match_non_sc_types = cxxRecordDecl(
forEachDescendant(
fieldDecl(
anyOf(hasType(builtinType()),
hasType(cxxRecordDecl(
allOf(
unless(hasName("sc_in")),
unless(hasName("sc_inout")),
unless(hasName("sc_out")),
unless(hasName("sc_signal")),
unless(hasName("sc_stream_in")),
unless(hasName("sc_stream_out"))
))))).bind("other_fields"))
);

// unless(
// forEach(fieldDecl(hasType(cxxRecordDecl(hasName("sc_signal")))))),

auto match_in_ports = makeFieldMatcher("sc_in");
auto match_out_ports = makeFieldMatcher("sc_out");
auto match_in_out_ports = makeFieldMatcher("sc_inout");
auto match_stream_in_ports = makeFieldMatcher("sc_stream_in");
auto match_stream_out_ports = makeFieldMatcher("sc_stream_out");

/*
finder.addMatcher(match_in_ports, this);
finder.addMatcher(match_out_ports, this);
finder.addMatcher(match_in_out_ports, this);
Expand All @@ -330,6 +368,9 @@ class PortMatcher : public MatchFinder::MatchCallback {
finder.addMatcher(match_sc_ports, this);
finder.addMatcher(match_stream_in_ports, this);
finder.addMatcher(match_stream_out_ports, this);
*/
finder.addMatcher(match_all_ports, this);
finder.addMatcher(match_non_sc_types, this);
}

virtual void run(const MatchFinder::MatchResult &result) {
Expand Down Expand Up @@ -365,8 +406,10 @@ class PortMatcher : public MatchFinder::MatchCallback {

if (auto fd = checkMatch<FieldDecl>("other_fields", result)) {
auto field_name{fd->getIdentifier()->getNameStart()};
llvm::outs() << " Found others fields: " << field_name << "\n";
llvm::outs() << " Found other_fields: " << field_name << "\n";
insert_port(other_fields_, fd);
llvm::outs() << "WHOA dump\n";
fd->dump();
}

if (auto fd = checkMatch<FieldDecl>("sc_stream_in", result)) {
Expand Down Expand Up @@ -423,8 +466,9 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
DeclarationsToInstancesMapType;

// This will store all the modules as ModuleDecl
//typedef ModuleDecl* ModulePairType;
typedef std::map<CXXRecordDecl *, ModuleDecl*> ModuleMapType;
// typedef ModuleDecl* ModulePairType;
typedef std::pair<CXXRecordDecl *, ModuleDecl *> ModulePairType;
typedef std::map<CXXRecordDecl *, ModuleDecl *> ModuleMapType;

private:
std::string top_module_decl_;
Expand Down Expand Up @@ -509,9 +553,10 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
// specializations or not.
//
if (isa<ClassTemplateSpecializationDecl>(decl)) {
// llvm::outs() << "TEMPLATE SPECIAL\n";
//llvm::outs() << "TEMPLATE SPECIAL\n";
found_template_declarations_.push_back(std::make_tuple(name, decl));
} else {
//llvm::outs() << "NOT TEMPLATE SPECIAL\n";
found_declarations_.push_back(std::make_tuple(name, decl));
}

Expand All @@ -524,8 +569,8 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
// This is the new data structure that uses ModuleDecl internally.
// Unpruned
auto add_module{new ModuleDecl(name, decl)};
modules_.insert(std::pair<CXXRecordDecl *, ModuleDecl*>(
decl, add_module));
modules_.insert(
std::pair<CXXRecordDecl *, ModuleDecl *>(decl, add_module));

// Instances should not be in subtree matching.
//
Expand Down Expand Up @@ -554,9 +599,13 @@ class ModuleDeclarationMatcher : public MatchFinder::MatchCallback {
}
}

const ModuleMapType &getFoundModuleDeclarations() const { return modules_; }

/*
const ModuleDeclarationMapType &getFoundModuleDeclarations() const {
return pruned_declarations_map_;
}
*/

void removeModule(CXXRecordDecl *decl) {
// Remove declarations from modules_
Expand Down
30 changes: 18 additions & 12 deletions src/SystemCClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,18 @@ bool SystemCConsumer::fire() {

// Check if the top-level module one of the sc_module declarations?
//
// Map CXXRecordDecl => std::string
// Map CXXRecordDecl => ModuleDecl*
auto found_module_declarations{
module_declaration_handler.getFoundModuleDeclarations()};
auto found_top_module{std::find_if(
found_module_declarations.begin(), found_module_declarations.end(),
[this](
const ModuleDeclarationMatcher::ModuleDeclarationPairType &element) {
return (element.second == getTopModule());
[this](const ModuleDeclarationMatcher::ModulePairType &element) {
return (element.second->getName() == getTopModule());
})};

if (found_top_module != found_module_declarations.end()) {
os_ << "Found the top module: " << found_top_module->second << ", "
<< found_top_module->second << "\n";
os_ << "Found the top module: " << found_top_module->second->getName()
<< ", " << found_top_module->second << "\n";
}

// ===========================================================
Expand Down Expand Up @@ -151,14 +150,17 @@ bool SystemCConsumer::fire() {
// FIXME: This has to be replaced once xlat is fixed.
vector<ModuleDecl *> module_decl_instances;
ModuleDecl *p_dummy_module_decl{
new ModuleDecl{found_module_declarations[cxx_decl], cxx_decl}};
// new ModuleDecl{found_module_declarations[cxx_decl], cxx_decl}};
// TODO: Remove deference pointer copy constructor
new ModuleDecl{*found_module_declarations[cxx_decl]}};
// ==================

os_ << "CXXRecordDecl* " << cxx_decl
<< ", module type: " << found_module_declarations[cxx_decl];
<< ", module type: " << found_module_declarations[cxx_decl]->getName();
for (const auto &instance : instance_list) {
auto add_module_decl{
new ModuleDecl{found_module_declarations[cxx_decl], cxx_decl}};
// new ModuleDecl{found_module_declarations[cxx_decl], cxx_decl}};
new ModuleDecl{*found_module_declarations[cxx_decl]}};

// Insert what you know about the parsed sc_module
// 1. Insert the instance name from Matchers
Expand All @@ -184,11 +186,14 @@ bool SystemCConsumer::fire() {
//
//
// cxx_decl->dump();


/*
FindPorts ports{static_cast<CXXRecordDecl *>(cxx_decl), os_};
// ports.dump();
//auto port_matcher{ module_declaration_handler.getPortMatcher() };
//add_module_decl->addPorts(port_matcher.getInputPorts(), "sc_in");
//add_module_decl->addPorts(port_matcher.getOutputPorts(), "sc_out");
// auto port_matcher{ module_declaration_handler.getPortMatcher() };
// add_module_decl->addPorts(port_matcher.getInputPorts(), "sc_in");
// add_module_decl->addPorts(port_matcher.getOutputPorts(), "sc_out");
add_module_decl->addInputPorts(ports.getInputPorts());
add_module_decl->addOutputPorts(ports.getOutputPorts());
add_module_decl->addInputOutputPorts(ports.getInputOutputPorts());
Expand All @@ -199,6 +204,7 @@ bool SystemCConsumer::fire() {
// 5. Find signals
FindSignals signals{add_module_decl->getModuleClassDecl(), os_};
add_module_decl->addSignals(signals.getSignals());
*/

// 5. Find entry functions
FindEntryFunctions findEntries{add_module_decl->getModuleClassDecl(),
Expand Down
8 changes: 4 additions & 4 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ file(COPY data DESTINATION ${CMAKE_BINARY_DIR}/tests/ )
file(COPY ../examples/llnl-examples DESTINATION ${CMAKE_BINARY_DIR}/tests/data )

set( UNIT_TEST_LIST
t1
t2
t3
sreg-test
# t1
# t2
# t3
# sreg-test
# t4-matchers
t5-template-matching
# subtree-matcher-test
Expand Down
16 changes: 14 additions & 2 deletions tests/data/templated-module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
typedef float MY_FLOAT;
typedef MY_FLOAT MY_FLOAT2;

struct my_data {
public:
int ya_data;
};

SC_MODULE( non_template ) {
int x;
sc_in<bool> blah;
my_data mdt;
sc_signal<int> array_signal[10];

void ef() {}
SC_CTOR(non_template) {
Expand All @@ -16,12 +23,17 @@ SC_MODULE( non_template ) {
template <typename S, typename T>
SC_MODULE( test ){
sc_in_clk clk;

sc_in<S> inS;
sc_in<T> inT;
sc_in<T> outT;

sc_out<S> outS;

sc_signal<S> test_signal;
sc_signal<S> data_array[10];
sc_signal<T> not_data_array[10];

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

// FieldDecl of an sc_module
//non_template simple_module;
Expand Down
16 changes: 12 additions & 4 deletions tests/matchers/match-structural.cmd.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#set bind-root false
set bind-root false
# match sc_modules
# m cxxRecordDecl(isDerivedFrom(hasName("::sc_core::sc_module")))

Expand Down Expand Up @@ -41,17 +41,25 @@ set output diag
# Notice that we are using the namedDecl here because sc_in_clk is actually
# a typedef to sc_in<bool>.
#
m cxxRecordDecl(isDerivedFrom(hasName("sc_module")), forEach(fieldDecl(hasType(namedDecl(hasName("sc_in_clk")))).bind("sc_in_clk")))
# m cxxRecordDecl(isDerivedFrom(hasName("sc_module")), forEach(fieldDecl(hasType(namedDecl(hasName("sc_in_clk")))).bind("sc_in_clk")))
# Match entry function
# 1. Find constructor
# 2. Find method with the name
# m cxxRecordDecl(isExpansionInMainFile(), isDerivedFrom(hasName("::sc_core::sc_module")), hasDescendant(cxxConstructorDecl().bind("constructor")))
#set output dump
set output dump
#m cxxRecordDecl(isExpansionInMainFile(), isDerivedFrom(hasName("::sc_core::sc_module")) )
# Detect field declarations that are of sc_module type
#m fieldDecl(hasType(cxxRecordDecl(isDerivedFrom(hasName("::sc_core::sc_module")))))
# Detect variable declarations that are of sc_module type
m varDecl(hasType(cxxRecordDecl(isDerivedFrom(hasName("::sc_core::sc_module")))))
# m varDecl(hasType(cxxRecordDecl(isDerivedFrom(hasName("::sc_core::sc_module")))))
#m cxxRecordDecl(isExpansionInMainFile(), forEach(fieldDecl(allOf(unless(hasType(cxxRecordDecl(isDerivedFrom(hasName("sc_port"))))), hasType(cxxRecordDecl(hasDefinition())), unless(hasType(cxxRecordDecl(hasName("sc_signal")))))).bind("others")))
#m fieldDecl(hasType(cxxRecordDecl(isDerivedFrom(hasName("sc_signal_inout_if")))))
m cxxRecordDecl(isExpansionInMainFile(), forEach(fieldDecl(hasType(arrayType(hasElementType(unless(builtinType()))))).bind("field")))
#m cxxRecordDecl(forEachDescendant(fieldDecl(hasName("array_signal"))))
22 changes: 11 additions & 11 deletions tests/t5-template-matching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ TEST_CASE("Only parse a single top-level module", "[parsing]") {

auto found_decl{found_module->second};
REQUIRE(found_decl->getIPorts().size() == 1);
REQUIRE(found_decl->getOtherVars().size() == 1);
REQUIRE(found_decl->getOtherVars().size() == 2);
}
}

Expand All @@ -106,21 +106,21 @@ TEST_CASE("Testing top-level module: test", "[top-module]") {
tooling::buildASTFromCodeWithArgs(code, systemc_clang::catch_test_args)
.release();

llvm::outs() << "================ TESTMATCHER =============== \n";

// Name of top-level module declaration.
std::string top{"test"};

ModuleDeclarationMatcher module_declaration_handler{};
MatchFinder matchRegistry{};
module_declaration_handler.registerMatchers(matchRegistry);
// llvm::outs() << "================ TESTMATCHER =============== \n";
// ModuleDeclarationMatcher module_declaration_handler{};
// MatchFinder matchRegistry{};
// module_declaration_handler.registerMatchers(matchRegistry);
// Run all the matchers
matchRegistry.matchAST(from_ast->getASTContext());
module_declaration_handler.set_top_module_decl(top);
module_declaration_handler.pruneMatches();
module_declaration_handler.dump();

llvm::outs() << "================ END =============== \n";
// matchRegistry.matchAST(from_ast->getASTContext());
// module_declaration_handler.set_top_module_decl(top);
// module_declaration_handler.pruneMatches();
// module_declaration_handler.dump();
//
// llvm::outs() << "================ END =============== \n";

SystemCConsumer sc{from_ast};
sc.setTopModule(top);
Expand Down

0 comments on commit 79e4a96

Please sign in to comment.