ModuleDecl.cpp 8.91 KB
Newer Older
Anirudh Kaushik's avatar
Anirudh Kaushik committed
1
#include "ModuleDecl.h"
rmrf's avatar
rmrf committed
2
#include <string>
Anirudh Kaushik's avatar
Anirudh Kaushik committed
3 4 5 6 7

using namespace scpar;

using std::string;

rmrf's avatar
rmrf committed
8 9 10
ModuleDecl::ModuleDecl() : module_name_{"NONE"},
                           class_decl_{nullptr},
                           constructor_stmt_{nullptr} {}
Anirudh Kaushik's avatar
Anirudh Kaushik committed
11

rmrf's avatar
rmrf committed
12
ModuleDecl::ModuleDecl(const string &name, CXXRecordDecl *decl)
rmrf's avatar
rmrf committed
13
    : module_name_{name}, class_decl_{decl} {}
Anirudh Kaushik's avatar
Anirudh Kaushik committed
14

15
ModuleDecl::~ModuleDecl() {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
16

rmrf's avatar
rmrf committed
17 18 19
  class_decl_ = nullptr;
  constructor_stmt_ = nullptr;

Anirudh Kaushik's avatar
Anirudh Kaushik committed
20
  // Delete all pointers in ports.
rmrf's avatar
rmrf committed
21
  for (auto input_port : _iports) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
22
    // Second is the PortDecl*.
23
    delete input_port.second;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
24 25 26
  }
  _iports.clear();

rmrf's avatar
rmrf committed
27
  for (auto output_port : _oports) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
28
    // Second is the PortDecl*.
29
    delete output_port.second;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
30 31 32
  }
  _oports.clear();

rmrf's avatar
rmrf committed
33
  for (auto io_port : _ioports) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
34
    // Second is the PortDecl*.
rmrf's avatar
rmrf committed
35
    delete io_port.second;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
36 37 38 39
  }
  _ioports.clear();
}

40 41 42 43 44 45 46
void ModuleDecl::setTemplateParameters(const vector<string> & parm_list) {
    template_parameters_ = parm_list;
}

vector<string> ModuleDecl::getTemplateParameters() const {
    return template_parameters_; }

rmrf's avatar
rmrf committed
47
void ModuleDecl::setModuleName(const string &name) { module_name_ = name; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
48

49
void ModuleDecl::addInstances(const vector<string> & instanceList) {
50
  _instanceList = instanceList;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
51 52
}

rmrf's avatar
rmrf committed
53
void ModuleDecl::addSignalBinding(map<string, string> portSignalMap) {
54
  _portSignalMap.insert(portSignalMap.begin(), portSignalMap.end());
Anirudh Kaushik's avatar
Anirudh Kaushik committed
55 56
}

57 58
void ModuleDecl::addSignals(const FindSignals::signalMapType & signal_map) {
  for (auto sit : signal_map) {
59
    string name = sit.first;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
60

rmrf's avatar
rmrf committed
61
    // It is important to create new objects.
Anirudh Kaushik's avatar
Anirudh Kaushik committed
62 63
    // This is because the objects created during Find*
    // may go outside scope, and free up allocated memory.
64 65
    SignalContainer *sc{ new SignalContainer(*sit.second) };
    Signal *sig { new Signal(name, sc) };
Anirudh Kaushik's avatar
Anirudh Kaushik committed
66

67
    _signals.insert( ModuleDecl::signalPairType(name, sig) );
Anirudh Kaushik's avatar
Anirudh Kaushik committed
68 69 70
  }
}

rmrf's avatar
rmrf committed
71 72
void ModuleDecl::addInputPorts(FindPorts::PortType p) {
  for (auto mit : p) {
73 74
    string name = mit.first;
    FindTemplateTypes *template_type = new FindTemplateTypes(mit.second);
rmrf's avatar
rmrf committed
75
    PortDecl *pd = new PortDecl(name, template_type);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
76

rmrf's avatar
rmrf committed
77
    _iports.insert(portPairType(mit.first, pd));
Anirudh Kaushik's avatar
Anirudh Kaushik committed
78 79 80
  }
}

rmrf's avatar
rmrf committed
81
void ModuleDecl::addOutputPorts(FindPorts::PortType p) {
rmrf's avatar
rmrf committed
82
  for (auto mit : p) {
83
    string n = mit.first;
rmrf's avatar
rmrf committed
84 85
    FindTemplateTypes *tt = new FindTemplateTypes(mit.second);
    PortDecl *pd = new PortDecl(n, tt);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
86 87 88 89 90

    _oports.insert(portPairType(n, pd));
  }
}

rmrf's avatar
rmrf committed
91 92 93
void ModuleDecl::addInputOutputPorts(FindPorts::PortType p) {
  for (auto mit : p) {
    _ioports.insert(
rmrf's avatar
rmrf committed
94
                    portPairType(mit.first, new PortDecl(mit.first, mit.second)));
Anirudh Kaushik's avatar
Anirudh Kaushik committed
95 96 97
  }
}

rmrf's avatar
rmrf committed
98 99
void ModuleDecl::addInputInterfaces(FindTLMInterfaces::interfaceType p) {
  for (auto mit : p) {
100
    string n = mit.first;
rmrf's avatar
rmrf committed
101 102
    FindTemplateTypes *tt = new FindTemplateTypes(mit.second);
    InterfaceDecl *pd = new InterfaceDecl(n, tt);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
103

rmrf's avatar
rmrf committed
104
    _iinterfaces.insert(interfacePairType(mit.first, pd));
Anirudh Kaushik's avatar
Anirudh Kaushik committed
105 106 107
  }
}

rmrf's avatar
rmrf committed
108 109
void ModuleDecl::addOutputInterfaces(FindTLMInterfaces::interfaceType p) {
  for (auto mit : p) {
110 111 112
    string name = mit.first;
    FindTemplateTypes *tt = new FindTemplateTypes(*mit.second);
    InterfaceDecl *pd = new InterfaceDecl(name, tt);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
113

114
    _ointerfaces.insert(interfacePairType(name, pd));
Anirudh Kaushik's avatar
Anirudh Kaushik committed
115 116 117
  }
}

rmrf's avatar
rmrf committed
118 119 120 121 122
void ModuleDecl::addInputOutputInterfaces(FindTLMInterfaces::interfaceType p) {
  //  for (FindTLMInterfaces::interfaceType::iterator mit = p.begin(), mite =
  //  p.end();    mit != mite; mit++) {
  for (auto mit : p) {
    _iointerfaces.insert(
rmrf's avatar
rmrf committed
123
                         interfacePairType(mit.first, new InterfaceDecl(mit.first, mit.second)));
Anirudh Kaushik's avatar
Anirudh Kaushik committed
124 125 126
  }
}

rmrf's avatar
rmrf committed
127
void ModuleDecl::addConstructor(Stmt *constructor) {
rmrf's avatar
rmrf committed
128
  constructor_stmt_ = constructor;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
129 130
}

rmrf's avatar
rmrf committed
131
void ModuleDecl::addProcess(FindEntryFunctions::entryFunctionVectorType *efv) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
132
  _vef = *efv;
rmrf's avatar
rmrf committed
133
  for (unsigned int i = 0; i < efv->size(); ++i) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
134 135 136 137 138 139 140 141 142

    EntryFunctionContainer *ef = (*efv)[i];

    // Set the entry name.
    string entryName = ef->_entryName;
    string entryType = "";

    // Set the process type
    switch (ef->_procType) {
Twiga's avatar
Twiga committed
143
    case PROCESS_TYPE::THREAD: {
144 145 146
      entryType = "SC_THREAD";
      break;
    }
Twiga's avatar
Twiga committed
147
    case PROCESS_TYPE::METHOD: {
148 149 150
      entryType = "SC_METHOD";
      break;
    }
Twiga's avatar
Twiga committed
151
    case PROCESS_TYPE::CTHREAD: {
152 153
      entryType = "SC_CTHREAD";
      break;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
154
    }
rmrf's avatar
rmrf committed
155
    default: {
156 157 158 159
      entryType = "ERROR";
      break;
    }
    }
rmrf's avatar
rmrf committed
160 161 162
    process_map_.insert(
                        processPairType(entryName, new ProcessDecl(entryType, entryName,
                                                                   ef->_entryMethodDecl, ef)));
Anirudh Kaushik's avatar
Anirudh Kaushik committed
163 164 165
  }
}

rmrf's avatar
rmrf committed
166
vector<string> ModuleDecl::getInstanceList() { return _instanceList; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
167

rmrf's avatar
rmrf committed
168
vector<EntryFunctionContainer *> ModuleDecl::getEntryFunctionContainer() {
169
  return _vef;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
170 171
}

rmrf's avatar
rmrf committed
172
int ModuleDecl::getNumInstances() { return _instanceList.size(); }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
173

174 175
ModuleDecl::signalMapType ModuleDecl::getSignals() { return _signals; }

rmrf's avatar
rmrf committed
176
ModuleDecl::processMapType ModuleDecl::getProcessMap() { return process_map_; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
177

rmrf's avatar
rmrf committed
178
ModuleDecl::portMapType ModuleDecl::getOPorts() { return _oports; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
179

rmrf's avatar
rmrf committed
180
ModuleDecl::portMapType ModuleDecl::getIPorts() { return _iports; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
181

rmrf's avatar
rmrf committed
182
ModuleDecl::portMapType ModuleDecl::getIOPorts() { return _ioports; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
183

184
ModuleDecl::interfaceMapType ModuleDecl::getOInterfaces() {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
185 186 187
  return _ointerfaces;
}

188
ModuleDecl::interfaceMapType ModuleDecl::getIInterfaces() {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
189 190 191
  return _iinterfaces;
}

192
ModuleDecl::interfaceMapType ModuleDecl::getIOInterfaces() {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
193 194 195
  return _iointerfaces;
}

rmrf's avatar
rmrf committed
196
string ModuleDecl::getName() { return module_name_; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
197

rmrf's avatar
rmrf committed
198
bool ModuleDecl::isModuleClassDeclNull() { return (class_decl_ == nullptr); }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
199

200
CXXRecordDecl *ModuleDecl::getModuleClassDecl() {
rmrf's avatar
rmrf committed
201 202
  assert(!(class_decl_ == nullptr));
  return class_decl_;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
203 204
}

rmrf's avatar
rmrf committed
205
void ModuleDecl::dumpInstances(raw_ostream &os, int tabn) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
206

rmrf's avatar
rmrf committed
207
  if (_instanceList.empty()) {
208 209 210
    os << " none \n";
  }

211
  for (size_t i = 0; i < _instanceList.size(); i++) {
rmrf's avatar
rmrf committed
212
    os << _instanceList.at(i) << " ";
213
  }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
214 215
}

rmrf's avatar
rmrf committed
216 217
void ModuleDecl::dumpSignalBinding(raw_ostream &os, int tabn) {
  if (_portSignalMap.empty()) {
218 219 220 221
    os << " none\n";
    return;
  }

rmrf's avatar
rmrf committed
222 223
  for (auto it : _portSignalMap) {
    os << "\nPort : " << it.first << " bound to signal : " << it.second;
224
  }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
225 226
}

rmrf's avatar
rmrf committed
227
void ModuleDecl::dumpProcesses(raw_ostream &os, int tabn) {
rmrf's avatar
rmrf committed
228

229 230 231 232 233
  json process_j;
  process_j["number_of_processes"] = process_map_.size();
  for (auto pit : process_map_) {
    ProcessDecl *pd { pit.second };
    process_j[pit.first] = pd->dump_json( os );
Anirudh Kaushik's avatar
Anirudh Kaushik committed
234
    }
235 236 237

  os << "Processes\n";
  os << process_j.dump(4) << "\n";
238
}
Anirudh Kaushik's avatar
Anirudh Kaushik committed
239

rmrf's avatar
rmrf committed
240
void ModuleDecl::dumpInterfaces(raw_ostream &os, int tabn) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
241

242
  os << "Input interfaces: " << _iinterfaces.size() << "\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
243 244

  if (_iinterfaces.size() == 0) {
245
    os << " none\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
246
  } else {
rmrf's avatar
rmrf committed
247
    for (auto mit : _iinterfaces) {
248
      mit.second->dump(os, tabn);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
249 250 251 252 253
      os << "\n ";
    }
    os << "\n";
  }

254
  os << "Output interfaces: " << _ointerfaces.size() << "\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
255
  if (_ointerfaces.size() == 0) {
256
    os << "none \n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
257
  } else {
rmrf's avatar
rmrf committed
258
    for (auto mit : _ointerfaces) {
259
      mit.second->dump(os, tabn);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
260 261
      os << "\n ";
    }
262
    os << "\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
263 264
  }

265
  os << "Inout interfaces: " << _iointerfaces.size() << "\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
266
  if (_iointerfaces.size() == 0) {
267
    os << "none \n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
268
  } else {
rmrf's avatar
rmrf committed
269
    for (auto mit : _iointerfaces) {
270
      mit.second->dump(os, tabn);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
271 272
      os << "\n ";
    }
273
    os << "\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
274 275 276
  }
}

rmrf's avatar
rmrf committed
277
void ModuleDecl::dumpPorts(raw_ostream &os, int tabn) {
278 279 280 281
  //  os << "\nInput ports: " << _iports.size() << "\n";

  json iport_j, oport_j, ioport_j;
  iport_j["number_of_in_ports"] = _iports.size();
Anirudh Kaushik's avatar
Anirudh Kaushik committed
282

rmrf's avatar
rmrf committed
283
    for (auto mit : _iports) {
284
      iport_j[mit.first] = mit.second->dump_json(os);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
285 286
    }

287 288
    //    os << "\nOutput ports: " << _oports.size() << "\n";
    oport_j["number_of_output_ports"] = _oports.size();
rmrf's avatar
rmrf committed
289
    for (auto mit : _oports) {
290 291
      oport_j[mit.first] = mit.second->dump_json(os);

Anirudh Kaushik's avatar
Anirudh Kaushik committed
292 293
    }

294 295 296 297
    //    os << "\nInout ports: " << _ioports.size() << "\n";
    ioport_j["number_of_inout_ports"] = _ioports.size();
    for (auto mit : _ioports) {
      ioport_j[mit.first] = mit.second->dump_json(os);
298
    }
299 300 301 302

    os << "Ports\n";
    os << iport_j.dump(4) << "\n" << oport_j.dump(4) << "\n"
       << ioport_j.dump(4)<< "\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
303 304
}

rmrf's avatar
rmrf committed
305
void ModuleDecl::dumpSignals(raw_ostream &os, int tabn) {
306 307 308 309 310
  json signal_j;
  signal_j["number_of_signals"] = _signals.size();
  for (auto sit : _signals) {
    Signal *s = sit.second;
    signal_j[sit.first] = s->dump_json(os);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
311
  }
312 313 314

  os << "Signals\n";
  os << signal_j.dump(4) << "\n";
315
}
Anirudh Kaushik's avatar
Anirudh Kaushik committed
316

rmrf's avatar
rmrf committed
317
void ModuleDecl::dump(raw_ostream &os) {
318
  os << "\n";
319 320
  os << "\n# Instances:\n";
  dumpInstances(os, 4);
321
  os << "# Port Declaration:\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
322
  dumpPorts(os, 4);
323
  os << "\n# Signal Declaration:\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
324
  dumpSignals(os, 4);
325
  os << "\n# Processes:\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
326
  dumpProcesses(os, 4);
rmrf's avatar
rmrf committed
327
  os << "# Signal binding:\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
328
  dumpSignalBinding(os, 4);
329 330

  dump_json();
Anirudh Kaushik's avatar
Anirudh Kaushik committed
331
  os << "\n=======================================================\n";
332 333 334 335 336 337 338 339

}

json ModuleDecl::dump_json() {

  json module_j;

  module_j["module_name"] = module_name_;
340 341 342 343 344
  // Template parameters.
  std::cout << "\nTEMPPARM: " << template_parameters_.size() << std::endl;
  for (const auto & parm: template_parameters_) {
      module_j["template_parameters"].push_back( parm );
  }
345 346 347 348


  std::cout << module_j.dump(4);
  return module_j;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
349
}