SystemCClang.cpp 7.54 KB
Newer Older
Anirudh Kaushik's avatar
Anirudh Kaushik committed
1
2
3
4
5
#include "SystemCClang.h"
using namespace scpar;
using namespace clang;
using namespace std;

rmrf's avatar
rmrf committed
6
Model *SystemCConsumer::getSystemCModel() { return _systemcModel; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
7

rmrf's avatar
rmrf committed
8
bool SystemCConsumer::preFire() { return true; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
9

rmrf's avatar
rmrf committed
10
bool SystemCConsumer::postFire() { return true; }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
11

rmrf's avatar
rmrf committed
12
bool SystemCConsumer::fire() {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
13

rmrf's avatar
rmrf committed
14
  TranslationUnitDecl *tu{_context.getTranslationUnitDecl()};
15
  // Reflection database.
rmrf's avatar
rmrf committed
16
  _systemcModel = new Model{} ;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
17

rmrf's avatar
rmrf committed
18
  // ANI : Do we need FindGlobalEvents?
rmrf's avatar
rmrf committed
19
  FindGlobalEvents globals{tu, os_};
rmrf's avatar
rmrf committed
20
21
22
23
  FindGlobalEvents::globalEventMapType eventMap{globals.getEventMap()};
  globals.dump_json();
  _systemcModel->addGlobalEvents(eventMap);

rmrf's avatar
rmrf committed
24
  // Find the sc_modules
rmrf's avatar
rmrf committed
25
  FindSCModules scmod{tu, os_};
Anirudh Kaushik's avatar
Anirudh Kaushik committed
26

rmrf's avatar
rmrf committed
27
  FindSCModules::moduleMapType scmodules{scmod.getSystemCModulesMap()};
28

rmrf's avatar
rmrf committed
29
  for (FindSCModules::moduleMapType::iterator mit = scmodules.begin(),
30
         mitend = scmodules.end();  mit != mitend; ++mit) {
rmrf's avatar
rmrf committed
31
    ModuleDecl *md = new ModuleDecl{mit->first, mit->second};
32
    //md->setTemplateParameters( scmod.getTemplateParameters() );
rmrf's avatar
rmrf committed
33
    //       os_ << "SIZE: " << scmod.getTemplateParameters().size() << "\n";
34
35
36
    _systemcModel->addModuleDecl(md);
  }

rmrf's avatar
rmrf committed
37
  ////////////////////////////////////////////////////////////////
38
39
  // Find the sc_main
  ////////////////////////////////////////////////////////////////
rmrf's avatar
rmrf committed
40
  FindSCMain scmain{tu, os_};
Anirudh Kaushik's avatar
Anirudh Kaushik committed
41

rmrf's avatar
rmrf committed
42
43
  if (scmain.isSCMainFound()) {
    FunctionDecl *fnDecl{scmain.getSCMainFunctionDecl()};
Anirudh's avatar
Anirudh committed
44

rmrf's avatar
rmrf committed
45
    FindSimTime scstart{fnDecl, os_};
rmrf's avatar
rmrf committed
46
47
    _systemcModel->addSimulationTime(scstart.returnSimTime());
  } else {
rmrf's avatar
rmrf committed
48
    os_ << "\n Could not find SCMain";
rmrf's avatar
rmrf committed
49
  }
50
51
52
53

  ////////////////////////////////////////////////////////////////
  // Find the netlist.
  ////////////////////////////////////////////////////////////////
rmrf's avatar
rmrf committed
54
55
56
  FindNetlist findNetlist{scmain.getSCMainFunctionDecl()};
  findNetlist.dump();
  _systemcModel->addNetlist(findNetlist);
57

rmrf's avatar
rmrf committed
58
  ////////////////////////////////////////////////////////////////
59
60
  // Figure out the module map.
  ////////////////////////////////////////////////////////////////
rmrf's avatar
rmrf committed
61
  Model::moduleMapType moduleMap{_systemcModel->getModuleDecl()};
62

rmrf's avatar
rmrf committed
63
  for (Model::moduleMapType::iterator mit = moduleMap.begin(),
64
         mitend = moduleMap.end();   mit != mitend; mit++) {
rmrf's avatar
rmrf committed
65
66
67
    ModuleDecl *mainmd{mit->second};
    int numInstances{mainmd->getNumInstances()};
    vector<ModuleDecl *> moduleDeclVec;
68

rmrf's avatar
rmrf committed
69
70
    os_ << "\n";
    os_ << "For module: " << mit->first << " num instance : " << numInstances;
rmrf's avatar
rmrf committed
71
72
73

    for (unsigned int num{0}; num < numInstances; ++num) {
      ModuleDecl *md = new ModuleDecl{};
74
75

      // Find the template arguments for the class.
rmrf's avatar
rmrf committed
76
      FindTemplateParameters tparms{mainmd->getModuleClassDecl(), os_};
77
78

      md->setTemplateParameters( tparms.getTemplateParameters() );
rmrf's avatar
rmrf committed
79
      os_ << "@@# " << mainmd->getTemplateParameters().size() << "\n";
80
81
82
      md->dump_json();


rmrf's avatar
rmrf committed
83
      vector<EntryFunctionContainer *> _entryFunctionContainerVector;
rmrf's avatar
rmrf committed
84
      FindConstructor constructor{mainmd->getModuleClassDecl(), os_};
rmrf's avatar
rmrf committed
85
86
      md->addConstructor(constructor.returnConstructorStmt());

rmrf's avatar
rmrf committed
87
      FindPorts ports{mainmd->getModuleClassDecl(), os_};
rmrf's avatar
rmrf committed
88
89
90
91
      md->addInputPorts(ports.getInputPorts());
      md->addOutputPorts(ports.getOutputPorts());
      md->addInputOutputPorts(ports.getInputOutputPorts());

rmrf's avatar
rmrf committed
92
      FindTLMInterfaces findTLMInterfaces{mainmd->getModuleClassDecl(), os_};
rmrf's avatar
rmrf committed
93
94
95
96
97
      md->addInputInterfaces(findTLMInterfaces.getInputInterfaces());
      md->addOutputInterfaces(findTLMInterfaces.getOutputInterfaces());
      md->addInputOutputInterfaces(
          findTLMInterfaces.getInputOutputInterfaces());

rmrf's avatar
rmrf committed
98
      FindSignals signals{mainmd->getModuleClassDecl(), os_};
rmrf's avatar
rmrf committed
99
100
      md->addSignals(signals.getSignals());

rmrf's avatar
rmrf committed
101
      FindEntryFunctions findEntries{mainmd->getModuleClassDecl(), os_};
rmrf's avatar
rmrf committed
102
103
104
105
106
107
      FindEntryFunctions::entryFunctionVectorType *entryFunctions{
          findEntries.getEntryFunctions()};
      md->addProcess(entryFunctions);

      for (size_t i = 0; i < entryFunctions->size(); i++) {
        EntryFunctionContainer *ef{(*entryFunctions)[i]};
rmrf's avatar
rmrf committed
108
109
        FindSensitivity findSensitivity{constructor.returnConstructorStmt(),
                                        os_};
rmrf's avatar
rmrf committed
110
        ef->addSensitivityInfo(findSensitivity);
111

rmrf's avatar
rmrf committed
112
        if (ef->getEntryMethod() == nullptr) {
rmrf's avatar
rmrf committed
113
          os_ << "ERROR";
rmrf's avatar
rmrf committed
114
115
116
          continue;
        }

rmrf's avatar
rmrf committed
117
        FindWait findWaits{ef->getEntryMethod(), os_};
rmrf's avatar
rmrf committed
118
119
        ef->addWaits(findWaits);

rmrf's avatar
rmrf committed
120
        FindNotify findNotify{ef->_entryMethodDecl, os_};
rmrf's avatar
rmrf committed
121
122
123
124
125
126
127
128
129
130
131
132
        ef->addNotifys(findNotify);

        /// Does not compile
        SuspensionAutomata suspensionAutomata(findWaits.getWaitCalls(),
                                              ef->getEntryMethod(), &_context,
                                              llvm::errs());
        if (suspensionAutomata.initialize()) {
          suspensionAutomata.genSusCFG();
          // suspensionAutomata.dumpSusCFG();
          suspensionAutomata.genSauto();
          // suspensionAutomata.dumpSauto();
          ef->addSusCFGAuto(suspensionAutomata);
rmrf's avatar
rmrf committed
133
        }
rmrf's avatar
rmrf committed
134
135
136
137
138

        _entryFunctionContainerVector.push_back(ef);
      }
      moduleDeclVec.push_back(md);
    }
rmrf's avatar
rmrf committed
139
    _systemcModel->addModuleDeclInstances(mainmd, moduleDeclVec);
rmrf's avatar
rmrf committed
140
  }
rmrf's avatar
rmrf committed
141

rmrf's avatar
rmrf committed
142
  /*
rmrf's avatar
rmrf committed
143
FindSCMain scmain(tu, os_);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
144

rmrf's avatar
rmrf committed
145
146
147
if (scmain.isSCMainFound())
{
          FunctionDecl *fnDecl = scmain.getSCMainFunctionDecl();
Anirudh Kaushik's avatar
Anirudh Kaushik committed
148

rmrf's avatar
rmrf committed
149
          FindSimTime scstart(fnDecl, os_);
rmrf's avatar
rmrf committed
150
          _systemcModel->addSimulationTime(scstart.returnSimTime());
Anirudh Kaushik's avatar
Anirudh Kaushik committed
151

rmrf's avatar
rmrf committed
152
153
}
else {
rmrf's avatar
rmrf committed
154
          os_ <<"\n Could not find SCMain";
rmrf's avatar
rmrf committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
}

FindNetlist findNetlist(scmain.getSCMainFunctionDecl());
findNetlist.dump();
_systemcModel->addNetlist(findNetlist);
*/

  // Generate SAUTO
  // Placing it here so that unique SAUTO for each instance
  // Model::moduleMapType moduleMap = _systemcModel->getModuleDecl();
  Model::moduleInstanceMapType moduleInstanceMap =
      _systemcModel->getModuleInstanceMap();

  for (Model::moduleInstanceMapType::iterator it = moduleInstanceMap.begin(),
                                              eit = moduleInstanceMap.end();
       it != eit; it++) {
    vector<ModuleDecl *> moduleDeclVec = it->second;
    for (size_t i = 0; i < moduleDeclVec.size(); i++) {
      ModuleDecl *moduleDecl = moduleDeclVec.at(i);
      vector<EntryFunctionContainer *> entryFunctionContainer =
          moduleDecl->getEntryFunctionContainer();
      for (size_t j = 0; j < entryFunctionContainer.size(); j++) {

        SuspensionAutomata suspensionAutomata(
            entryFunctionContainer.at(j)->getWaitCalls(),
            entryFunctionContainer.at(j)->getEntryMethod(), &_context,
            llvm::errs());
        if (suspensionAutomata.initialize()) {
          suspensionAutomata.genSusCFG();
rmrf's avatar
rmrf committed
184
          //suspensionAutomata.dumpSusCFG();
rmrf's avatar
rmrf committed
185
          suspensionAutomata.genSauto();
rmrf's avatar
rmrf committed
186
          //suspensionAutomata.dumpSauto();
rmrf's avatar
rmrf committed
187
188
189
          entryFunctionContainer.at(j)->addSusCFGAuto(suspensionAutomata);
        }
      }
rmrf's avatar
rmrf committed
190
    }
rmrf's avatar
rmrf committed
191
192
  }

rmrf's avatar
rmrf committed
193
194
195
  os_ << "\n";
  os_ << "\n## SystemC model\n";
  _systemcModel->dump(os_);
rmrf's avatar
rmrf committed
196
  return true;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
197
198
}

rmrf's avatar
rmrf committed
199
200
201
202
void SystemCConsumer::HandleTranslationUnit(ASTContext &context) {
  // ///////////////////////////////////////////////////////////////
  // / Pass 1: Find the necessary information.
  // ///////////////////////////////////////////////////////////////
Anirudh Kaushik's avatar
Anirudh Kaushik committed
203

rmrf's avatar
rmrf committed
204
  bool pre = false;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
205

rmrf's avatar
rmrf committed
206
  pre = preFire();
Anirudh Kaushik's avatar
Anirudh Kaushik committed
207

rmrf's avatar
rmrf committed
208
209
210
  if (!pre) {
    return;
  }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
211

rmrf's avatar
rmrf committed
212
  bool f = false;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
213

rmrf's avatar
rmrf committed
214
  f = fire();
Anirudh Kaushik's avatar
Anirudh Kaushik committed
215

rmrf's avatar
rmrf committed
216
217
218
219
  if (!f) {
    return;
  }
  postFire();
Anirudh Kaushik's avatar
Anirudh Kaushik committed
220
221
}

rmrf's avatar
rmrf committed
222
SystemCConsumer::SystemCConsumer(CompilerInstance &ci)
rmrf's avatar
rmrf committed
223
224
225
226
227
    : os_{llvm::errs()},
      _sm{ci.getSourceManager()},
      _context{ci.getASTContext()},
      _ci{ci},
      _systemcModel{nullptr} {}
Anirudh Kaushik's avatar
Anirudh Kaushik committed
228

rmrf's avatar
rmrf committed
229
SystemCConsumer::~SystemCConsumer() {
rmrf's avatar
rmrf committed
230
231
  if (_systemcModel != nullptr) {
    delete _systemcModel;
rmrf's avatar
rmrf committed
232
    _systemcModel = nullptr;
rmrf's avatar
rmrf committed
233
  }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
234
}