SystemCClang.cpp 7.48 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
        FindSensitivity findSensitivity{constructor.returnConstructorStmt(), os_};
rmrf's avatar
rmrf committed
109
        ef->addSensitivityInfo(findSensitivity);
110

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

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

rmrf's avatar
rmrf committed
119
        FindNotify findNotify{ef->_entryMethodDecl, os_};
rmrf's avatar
rmrf committed
120
121
122
123
124
125
126
127
128
129
130
131
        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
132
        }
rmrf's avatar
rmrf committed
133
134
135
136
137

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

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

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

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

rmrf's avatar
rmrf committed
151
152
}
else {
rmrf's avatar
rmrf committed
153
          os_ <<"\n Could not find SCMain";
rmrf's avatar
rmrf committed
154
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
}

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
183
          //suspensionAutomata.dumpSusCFG();
rmrf's avatar
rmrf committed
184
          suspensionAutomata.genSauto();
rmrf's avatar
rmrf committed
185
          //suspensionAutomata.dumpSauto();
rmrf's avatar
rmrf committed
186
187
188
          entryFunctionContainer.at(j)->addSusCFGAuto(suspensionAutomata);
        }
      }
rmrf's avatar
rmrf committed
189
    }
rmrf's avatar
rmrf committed
190
191
  }

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

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

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

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

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

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

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

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

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

rmrf's avatar
rmrf committed
225
SystemCConsumer::~SystemCConsumer() {
rmrf's avatar
rmrf committed
226
227
  if (_systemcModel != nullptr) {
    delete _systemcModel;
rmrf's avatar
rmrf committed
228
    _systemcModel = nullptr;
rmrf's avatar
rmrf committed
229
  }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
230
}