systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
ModuleInstance.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <string>
3#include <tuple>
4
5#include "ModuleInstance.h"
6
7#include "SignalDecl.h"
8#include "APIntUtils.h"
9
10using namespace systemc_clang;
11
13 : module_name_{"NONE"},
14 instance_name_{"NONE"},
15 class_decl_{nullptr},
16 constructor_stmt_{nullptr},
17 constructor_decl_{nullptr},
18 instance_decl_{nullptr} {}
19
20ModuleInstance::ModuleInstance(const std::string &name,
21 const clang::CXXRecordDecl *decl)
22 : module_name_{name},
23 instance_name_{"NONE"},
24 class_decl_{const_cast<clang::CXXRecordDecl *>(decl)},
25 instance_decl_{nullptr} {}
26
28 const std::tuple<const std::string &, clang::CXXRecordDecl *> &element)
29 : module_name_{get<0>(element)}, class_decl_{get<1>(element)} {}
30
68
73
78
81
83 in_ports_ = from.in_ports_;
87 // submodules_ = from.submodules_;
88
91
93
97 signals_ = from.signals_;
98
101 vef_ = from.vef_;
102
103 // Class template parameters.
105
106 // Nested submodules
108
109 return *this;
110}
111
113 // This only clears the globally (not instance-specific) allocated structures.
114 // these are AST nodes
115 class_decl_ = nullptr;
116 constructor_stmt_ = nullptr;
117 constructor_decl_ = nullptr;
118
119 // Ports are globally allocated.
120 in_ports_.clear();
121 out_ports_.clear();
122 inout_ports_.clear();
123 other_fields_.clear();
124 // submodules_.clear();
125 istreamports_.clear();
126 ostreamports_.clear();
127 signals_.clear();
128}
129
131 DEBUG_WITH_TYPE("DebugDestructors", llvm::dbgs() << "\n~ModuleInstance\n";);
132 class_decl_ = nullptr;
133 constructor_stmt_ = nullptr;
134 instance_decl_ = nullptr;
135 // IMPORTANT: Only the instance-specific details should be deleted.
136 // DO NOT delete the information collected through incomplete types.
137 //
138
139 for (auto base : base_instances_) {
140 delete base;
141 }
142
143 for (auto &v : vef_) {
144 if (v != nullptr) {
145 delete v;
146 }
147 v = nullptr;
148 }
149 // Delete all pointers in ports.
150 for (auto &input_port : in_ports_) {
151 // It is a tuple
152 // 0. string, 1. PortDecl*
153 // Only one to delete is (1)
154 //
155 // delete input_port.second;
156 auto iport{get<1>(input_port)};
157 if (iport) {
158 delete iport;
159 }
160 }
161 in_ports_.clear();
162
163 for (auto &output_port : out_ports_) {
164 delete get<1>(output_port);
165 }
166 out_ports_.clear();
167
168 for (auto &io_port : inout_ports_) {
169 // Second is the PortDecl*.
170 delete get<1>(io_port);
171 }
172 inout_ports_.clear();
173
174 for (auto &other : other_fields_) {
175 // Second is the PortDecl*.
176 delete get<1>(other);
177 }
178 other_fields_.clear();
179 // Delete EntryFunction container
180 for (auto &ef : vef_) {
181 delete ef;
182 }
183 vef_.clear();
184
185 for (auto &sig : signals_) {
186 delete sig.second;
187 }
188}
189
194
196
198 const std::vector<std::string> &parm_list) {
199 template_parameters_ = parm_list;
200}
201
203 const std::vector<std::string> &parm_list) {
204 template_args_ = parm_list;
205}
206
207const std::vector<std::string> &ModuleInstance::getTemplateParameters() const {
209}
210
211void ModuleInstance::setModuleName(const std::string &name) {
212 module_name_ = name;
213}
215 base_instances_.push_back(base);
216}
217
219 const std::vector<std::string> &instanceList) {
220 instance_list_ = instanceList;
221}
222
223void ModuleInstance::addPortBinding(const std::string &port_name,
224 PortBinding *pb) {
225 port_bindings_.insert(portBindingPairType(port_name, pb));
226}
227
229 std::map<std::string, std::string> portSignalMap) {
230 port_signal_map_.insert(portSignalMap.begin(), portSignalMap.end());
231}
232
234 const std::string &port_type) {
235 if (port_type == "sc_in") {
236 std::copy(begin(found_ports), end(found_ports), back_inserter(in_ports_));
237 }
238 if (port_type == "sc_out") {
239 std::copy(begin(found_ports), end(found_ports), back_inserter(out_ports_));
240 }
241 if (port_type == "sc_inout") {
242 std::copy(begin(found_ports), end(found_ports),
243 back_inserter(inout_ports_));
244 }
245 if (port_type == "others") {
246 std::copy(begin(found_ports), end(found_ports),
247 back_inserter(other_fields_));
248 }
249
250 if (port_type == "sc_signal") {
252 for (auto const &signal_port : found_ports) {
253 auto port_decl{get<1>(signal_port)};
254 auto name{port_decl->getName()};
255
256 // SignalContainer
257 SignalDecl *signal_entry{new SignalDecl{*port_decl}};
258 signals_.insert(ModuleInstance::signalPairType(name, signal_entry));
259 }
260 }
261
262 if (port_type == "sc_stream_in") {
263 std::copy(begin(found_ports), end(found_ports),
264 back_inserter(istreamports_));
265 }
266 if (port_type == "sc_stream_out") {
267 std::copy(begin(found_ports), end(found_ports),
268 back_inserter(ostreamports_));
269 }
270}
271
273 for (auto mit : p) {
274 std::string n{mit.first};
275 FindTemplateTypes *tt = new FindTemplateTypes(mit.second);
276 InterfaceDecl *pd = new InterfaceDecl(n, tt);
277
278 iinterfaces_.insert(interfacePairType(mit.first, pd));
279 }
280}
281
283 for (auto mit : p) {
284 std::string name{mit.first};
285 FindTemplateTypes *tt = new FindTemplateTypes(*mit.second);
286 InterfaceDecl *pd = new InterfaceDecl(name, tt);
287
288 ointerfaces_.insert(interfacePairType(name, pd));
289 }
290}
291
294 for (auto mit : p) {
295 iointerfaces_.insert(
296 interfacePairType(mit.first, new InterfaceDecl(mit.first, mit.second)));
297 }
298}
299
304
305void ModuleInstance::addConstructor(Stmt *constructor) {
306 constructor_stmt_ = constructor;
307}
308
309const clang::Stmt *ModuleInstance::getConstructorStmt() const {
310 return constructor_stmt_;
311}
312
313const clang::CXXConstructorDecl *ModuleInstance::getConstructorDecl() const {
314 return constructor_decl_;
315}
316
319 vef_ = *efv;
320 for (unsigned int i = 0; i < efv->size(); ++i) {
321 EntryFunctionContainer *ef = (*efv)[i];
322
323 // Set the entry name.
324 std::string entryName{ef->getName()};
325 std::string entryType{""};
326
327 // Set the process type
328 switch (ef->getProcessType()) {
330 entryType = "SC_THREAD";
331 break;
332 }
334 entryType = "SC_METHOD";
335 break;
336 }
338 entryType = "SC_CTHREAD";
339 break;
340 }
341 default: {
342 entryType = "ERROR";
343 break;
344 }
345 }
347 entryName,
348 new ProcessDecl(entryType, entryName, ef->getEntryMethod(), ef)));
349 }
350}
351
353 nested_modules_.push_back(nested_module);
354}
355
356const std::vector<std::string> &ModuleInstance::getInstanceList() {
357 return instance_list_;
358}
359
360const std::vector<EntryFunctionContainer *>
364
366
370
371const std::vector<ModuleInstance *> &ModuleInstance::getNestedModuleInstances()
372 const {
373 return nested_modules_;
374}
375
379
380const std::vector<ModuleInstance *> &ModuleInstance::getBaseInstances() {
381 return base_instances_;
382}
383
387
391
395
399
403
407
411
415
419
423
424std::string ModuleInstance::getName() const { return module_name_; }
425
428}
429
431 return (class_decl_ == nullptr);
432}
433
434const clang::CXXRecordDecl *ModuleInstance::getModuleClassDecl() {
435 assert(!(class_decl_ == nullptr));
436 return class_decl_;
437}
438
441}
442void ModuleInstance::dumpInstances(raw_ostream &os, int tabn) {
443 if (instance_list_.empty()) {
444 os << " none \n";
445 }
446
447 for (size_t i = 0; i < instance_list_.size(); i++) {
448 os << instance_list_.at(i) << " ";
449 }
450
451 // Dump the submodules
452 //
453 for (auto const &submod : nested_modules_) {
454 os << "nested module " << submod << " module type name "
455 << submod->getName();
456
457 if (submod->getInstanceInfo().getInstanceNames().size() > 0) {
458 os << " instance name: ";
459 }
460 for (auto const &name : submod->getInstanceInfo().getInstanceNames()) {
461 os << name << " ";
462 }
463 os << "\n";
464 }
465}
466
468 std::string str{};
469
470 str +=
471 "number_of_ports_bound: " + std::to_string(port_bindings_.size()) + "\n";
472
473 // binding_j["number_of_ports_bound"] = port_bindings_.size();
474 for (auto const &pb : port_bindings_) {
475 auto port_name{get<0>(pb)};
476 auto binding{get<1>(pb)};
477
478 str +=
479 "caller_instance_type_name: " + binding->getCallerInstanceTypeName() +
480 "\n";
481 str += "caller_instance_name: " + binding->getCallerInstanceName() + "\n";
482
483 for (const auto &sub : binding->getCallerArraySubscripts()) {
484 auto is_int_lit{clang::dyn_cast<clang::IntegerLiteral>(sub)};
485 auto is_dref_expr{clang::dyn_cast<clang::DeclRefExpr>(sub)};
486
487 str += "caller_array_subscripts: ";
488 if (is_int_lit) {
489 str += " " + systemc_clang::utils::apint::toString(is_int_lit->getValue());
490 }
491
492 if (is_dref_expr) {
493 str += "caller_array_subscripts: " +
494 is_dref_expr->getNameInfo().getName().getAsString() + "\n";
495 }
496 }
497
498 str += "caller_port_name: " + binding->getCallerPortName() + "\n";
499
500 for (const auto &sub : binding->getCallerPortArraySubscripts()) {
501 auto is_int_lit{clang::dyn_cast<clang::IntegerLiteral>(sub)};
502 auto is_dref_expr{clang::dyn_cast<clang::DeclRefExpr>(sub)};
503
504 str += "caller_port_array_subscripts: ";
505 if (is_int_lit) {
506 str += " " + systemc_clang::utils::apint::toString(is_int_lit->getValue());
507 }
508
509 if (is_dref_expr) {
510 str += "caller_port_array_subscripts: " +
511 is_dref_expr->getNameInfo().getName().getAsString() + "\n";
512 }
513 }
514
515 str += "callee_instance_name:: " + binding->getCalleeInstanceName() + "\n";
516
517 for (const auto &sub : binding->getCalleeArraySubscripts()) {
518 auto is_int_lit{clang::dyn_cast<clang::IntegerLiteral>(sub)};
519 auto is_dref_expr{clang::dyn_cast<clang::DeclRefExpr>(sub)};
520
521 str += "callee_port_array_subscripts: ";
522 if (is_int_lit) {
523 str += " " + systemc_clang::utils::apint::toString(is_int_lit->getValue());
524 }
525
526 if (is_dref_expr) {
527 str += "callee_port_array_subscripts: " +
528 is_dref_expr->getNameInfo().getName().getAsString() + "\n";
529 }
530 }
531
532 str += "callee_port_name: " + binding->getCalleePortName() + "\n";
533
534 str += "\n\n";
535 binding->dump();
536 }
537 LLVM_DEBUG(llvm::dbgs() << str;);
538 return str;
539}
540
541void ModuleInstance::dumpSignalBinding(raw_ostream &os, int tabn) {
542 if (port_signal_map_.empty()) {
543 os << " none\n";
544 return;
545 }
546
547 for (auto it : port_signal_map_) {
548 os << "\nPort : " << it.first << " bound to signal : " << it.second;
549 }
550}
551
552void ModuleInstance::dumpProcesses(raw_ostream &os, int tabn) {
553 std::string str{};
554
555 str += "number_of_processes: " + std::to_string(process_map_.size()) + "\n";
556
557 for (auto pit : process_map_) {
558 ProcessDecl *pd{pit.second};
559 str += pit.first + ": " + pd->asString() + "\n";
560 pd ->dump();
561 }
562 str += "\n";
563
564 os << "Processes\n";
565 os << str;
566}
567
568void ModuleInstance::dumpInterfaces(raw_ostream &os, int tabn) {
569 os << "Input interfaces: " << iinterfaces_.size() << "\n";
570
571 if (iinterfaces_.size() == 0) {
572 os << " none\n";
573 } else {
574 for (auto mit : iinterfaces_) {
575 mit.second->dump(os, tabn);
576 os << "\n ";
577 }
578 os << "\n";
579 }
580
581 os << "Output interfaces: " << ointerfaces_.size() << "\n";
582 if (ointerfaces_.size() == 0) {
583 os << "none \n";
584 } else {
585 for (auto mit : ointerfaces_) {
586 mit.second->dump(os, tabn);
587 os << "\n ";
588 }
589 os << "\n";
590 }
591
592 os << "Inout interfaces: " << iointerfaces_.size() << "\n";
593 if (iointerfaces_.size() == 0) {
594 os << "none \n";
595 } else {
596 for (auto mit : iointerfaces_) {
597 mit.second->dump(os, tabn);
598 os << "\n ";
599 }
600 os << "\n";
601 }
602}
603
604void ModuleInstance::dumpPorts(raw_ostream &os) {
605 std::string str{};
606
607 str += "number_of_input_ports: " + std::to_string(in_ports_.size()) + "\n";
608 for (auto mit : in_ports_) {
609 auto name = get<0>(mit);
610 auto pd = get<1>(mit);
611 str += "name: " + name + " " + pd->asString() + "\n";
612 }
613
614 str += "number_of_output_ports: " + std::to_string(out_ports_.size()) + "\n";
615 for (auto mit : out_ports_) {
616 auto name = get<0>(mit);
617 auto pd = get<1>(mit);
618 str += "name: " + name + " " + pd->asString() + "\n";
619 }
620
621 str += "number_of_inout_ports: " + std::to_string(inout_ports_.size()) + "\n";
622 for (auto mit : inout_ports_) {
623 auto name = get<0>(mit);
624 auto pd = get<1>(mit);
625 str += "name: " + name + " " + pd->asString() + "\n";
626 }
627
628 str += "number_of_instream_ports: " + std::to_string(istreamports_.size()) +
629 "\n";
630 for (auto mit : istreamports_) {
631 auto name = get<0>(mit);
632 auto pd = get<1>(mit);
633 str += "name: " + name + " " + pd->asString() + "\n";
634 }
635
636 str += "number_of_outstream_ports: " + std::to_string(ostreamports_.size()) +
637 "\n";
638 for (auto mit : ostreamports_) {
639 auto name = get<0>(mit);
640 auto pd = get<1>(mit);
641 str += "name: " + name + " " + pd->asString() + "\n";
642 }
643
644 str += "number_of_other_vars: " + std::to_string(other_fields_.size()) + "\n";
645 for (auto mit : other_fields_) {
646 auto name = get<0>(mit);
647 auto pd = get<1>(mit);
648 str += "name: " + name + " " + pd->asString() + "\n";
649 }
650
651 str += "number_of_nested_modules: " + std::to_string(nested_modules_.size()) +
652 "\n";
653 for (auto mit : nested_modules_) {
654 auto module_name{mit->getName()};
655
656 auto instance_info{mit->getInstanceInfo()};
657 str += "module_name: " + module_name;
658 for (auto const &inst_name : instance_info.getInstanceNames()) {
659 str += " " + inst_name;
660 }
661 }
662 str += "\n";
663
664 os << "Dump ports\n";
665 os << str;
666}
667
668void ModuleInstance::dumpSignals(raw_ostream &os, int tabn) {
669 std::string str{};
670
671 str += "number_of_signals: " + std::to_string(signals_.size()) + "\n";
672
673 for (auto sit : signals_) {
674 SignalDecl *s{sit.second};
675 str += sit.first + " " + s->asString() + "\n";
676 }
677 str += "\n";
678
679 os << "Signals\n";
680 os << str;
681}
682
683void ModuleInstance::dump_base_instances(llvm::raw_ostream &os) {
684 os << "Dump base instances: " << base_instances_.size() << "\n";
685 for (const auto base : base_instances_) {
686 base->dump(os);
687 }
688}
689
690void ModuleInstance::dump(llvm::raw_ostream &os) {
691 os << "Name: " << module_name_;
692 os << "\n# Instances:\n";
693 dumpInstances(os, 4);
694 os << "# Port Declaration:\n";
695 dumpPorts(os);
696 os << "\n# Signal Declaration:\n";
697 dumpSignals(os, 4);
698 os << "\n# Processes:\n";
699 dumpProcesses(os, 4);
700 os << "# Port binding:" << port_bindings_.size() << "\n";
701 dumpPortBinding(); //(os, 4);
702 os << "# Signal binding:\n";
703 dumpSignalBinding(os, 4);
704//
705 // for (auto entry_function: vef_) {
706 // entry_function->dump();
707 // }
708
709 dump_json();
710
712 os << "\n=======================================================\n";
713}
714
716 std::string str{};
717
718 str += "module_name: " + module_name_ + " " +
719 "instance_name: " + instance_info_.instance_name + "\n";
721 str += "is_array: true\n";
722 str += "array_sizes: ";
723 // Write out all the sizes.
724 for (auto const &size : instance_info_.getArraySizes()) {
726 }
727 }
728
729 str += "\n";
730
731 // Template parameters.
732 str += "template_parameters: " + std::to_string(template_parameters_.size()) +
733 "\n";
734 for (auto const &parm : template_parameters_) {
735 str += " " + parm;
736 }
737
738 str += "template_args: " + std::to_string(template_args_.size()) + "\n";
739 for (auto const &parm : template_args_) {
740 str += " " + parm;
741 }
742
743 str += "nested_modules: " + std::to_string(nested_modules_.size()) + "\n";
744 for (auto const &submod : nested_modules_) {
745 for (auto const &name : submod->getInstanceInfo().getInstanceNames()) {
746 str += " " + name;
747 }
748 }
749
750 str += "\n";
751 LLVM_DEBUG(llvm::outs() << str;);
752 return str;
753}
clang::CXXConstructorDecl * getConstructorDecl() const
clang::Stmt * getConstructorStmt() const
std::vector< EntryFunctionContainer * > entryFunctionVectorType
Typedefs.
std::map< string, FindTemplateTypes * > interfaceType
Forward declarations.
void addConstructor(FindConstructor *)
std::vector< std::string > template_parameters_
Class template parameters.
const portMapType & getOtherVars()
std::pair< std::string, SignalDecl * > signalPairType
void addInputInterfaces(FindTLMInterfaces::interfaceType)
void addNestedModule(ModuleInstance *submodule)
void addInstances(const std::vector< std::string > &)
const std::vector< std::string > & getTemplateParameters() const
ModuleInstance & operator=(const ModuleInstance &from)
portSignalMapType port_signal_map_
const clang::CXXRecordDecl * getModuleClassDecl()
std::vector< std::tuple< std::string, PortDecl * > > PortType
void dumpSignals(llvm::raw_ostream &, int)
void addBaseInstance(ModuleInstance *)
std::vector< ModuleInstance * > base_instances_
void setTemplateArgs(const vector< std::string > &)
void dumpPorts(llvm::raw_ostream &)
const portMapType & getIOPorts()
const portMapType & getOPorts()
std::pair< std::string, ProcessDecl * > processPairType
void addPortBinding(const std::string &port_name, PortBinding *pb)
const clang::Stmt * getConstructorStmt() const
clang::CXXRecordDecl * class_decl_
void dumpInterfaces(llvm::raw_ostream &, int)
const std::vector< ModuleInstance * > & getNestedModuleInstances() const
void setTemplateParameters(const vector< std::string > &)
void addProcess(FindEntryFunctions::entryFunctionVectorType *)
std::pair< std::string, PortBinding * > portBindingPairType
const portMapType & getOutputStreamPorts()
void addInputOutputInterfaces(FindTLMInterfaces::interfaceType)
void addOutputInterfaces(FindTLMInterfaces::interfaceType)
portBindingMapType port_bindings_
std::vector< ModuleInstance * > nested_modules_
Nested modules.
const portMapType & getInputStreamPorts()
const portBindingMapType & getPortBindings()
void dump(llvm::raw_ostream &)
const interfaceMapType & getOInterfaces()
void setModuleName(const std::string &)
std::map< std::string, InterfaceDecl * > interfaceMapType
std::pair< std::string, InterfaceDecl * > interfacePairType
const interfaceMapType & getIInterfaces()
ModuleInstanceType getInstanceInfo()
std::string getInstanceName() const
void dump_base_instances(llvm::raw_ostream &os)
std::map< std::string, PortBinding * > portBindingMapType
const clang::Decl * getInstanceDecl()
void dumpSignalBinding(llvm::raw_ostream &, int)
const clang::CXXConstructorDecl * getConstructorDecl() const
void dumpProcesses(llvm::raw_ostream &, int)
const std::vector< ModuleInstance * > & getBaseInstances()
std::map< std::string, ProcessDecl * > processMapType
const std::vector< std::string > & getInstanceList()
std::vector< std::string > instance_list_
void setInstanceInfo(const sc_ast_matchers::ModuleInstanceType &info)
void addSignalBinding(std::map< std::string, std::string >)
std::vector< std::string > template_args_
clang::CXXConstructorDecl * constructor_decl_
std::vector< EntryFunctionContainer * > vef_
ModuleInstance()
Default constructor.
const interfaceMapType & getIOInterfaces()
std::map< std::string, SignalDecl * > signalMapType
void dumpInstances(llvm::raw_ostream &, int)
const processMapType & getProcessMap()
std::vector< std::tuple< std::string, PortDecl * > > portMapType
ModuleInstanceType instance_info_
const std::vector< EntryFunctionContainer * > & getEntryFunctionContainer()
void addPorts(const PortType &found_ports, const std::string &port_type)
const portMapType & getIPorts()
const signalMapType & getSignals() const
std::string asString() const
Dump.
std::string asString() const
Dump to string.
Clang forward declarations.
Definition FindArgument.h:6
std::string toString(const T &i)
Definition APIntUtils.h:14
std::vector< llvm::APInt > getArraySizes()