SystemCClang.cpp 6.76 KB
Newer Older
Anirudh Kaushik's avatar
Anirudh Kaushik committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "SystemCClang.h"
using namespace scpar;
using namespace clang;
using namespace std;

Model *SystemCConsumer::getSystemCModel() {
	return _systemcModel;
}

bool SystemCConsumer::preFire() {
	return true;
}

bool SystemCConsumer::postFire() {
	return true;
}

rmrf's avatar
rmrf committed
18
bool SystemCConsumer::fire() {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
19
20

	TranslationUnitDecl *tu = _context.getTranslationUnitDecl();
21
  // Reflection database.
Anirudh Kaushik's avatar
Anirudh Kaushik committed
22
23
	_systemcModel = new Model();

24
  // Find the sc_modules 
Anirudh Kaushik's avatar
Anirudh Kaushik committed
25
26
27
28
29
30
31
	SCModules scmod(tu, _os);

	// ANI : Do we need FindGlobalEvents?
	FindGlobalEvents fglobals(tu, _os);
	FindGlobalEvents::globalEventMapType eventMap = fglobals.getEventMap();
	_systemcModel->addGlobalEvents(eventMap);

32

Anirudh Kaushik's avatar
Anirudh Kaushik committed
33
	SCModules::moduleMapType scmodules = scmod.getSystemCModulesMap();
34
35
36
37
38
39
40
41
42

  for ( SCModules::moduleMapType::iterator mit = scmodules.begin(),
          mitend = scmodules.end(); mit != mitend; ++mit )  {
    ModuleDecl *md = new ModuleDecl(mit->first, mit->second);
    _systemcModel->addModuleDecl(md);
  }



Anirudh's avatar
Anirudh committed
43
	////////////////////////////////////////////////////////////////
44
45
  // Find the sc_main
  ////////////////////////////////////////////////////////////////
Anirudh's avatar
Anirudh committed
46
	FindSCMain scmain(tu, _os);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
47

48
	if ( scmain.isSCMainFound() ) {
rmrf's avatar
rmrf committed
49
      FunctionDecl *fnDecl = scmain.getSCMainFunctionDecl();
Anirudh's avatar
Anirudh committed
50

rmrf's avatar
rmrf committed
51
52
      FindSimTime scstart(fnDecl, _os);
      _systemcModel->addSimulationTime(scstart.returnSimTime());
53
  }	else {
Anirudh's avatar
Anirudh committed
54
55
		_os <<"\n Could not find SCMain";
	}
56
57
58
59

  ////////////////////////////////////////////////////////////////
  // Find the netlist.
  ////////////////////////////////////////////////////////////////
Anirudh's avatar
Anirudh committed
60
61
	FindNetlist findNetlist(scmain.getSCMainFunctionDecl());
 	findNetlist.dump();
62
63
64
	_systemcModel->addNetlist(findNetlist);


Anirudh's avatar
Anirudh committed
65
	////////////////////////////////////////////////////////////////
66
67
  // Figure out the module map.
  ////////////////////////////////////////////////////////////////
Anirudh's avatar
Anirudh committed
68
	Model::moduleMapType moduleMap = _systemcModel->getModuleDecl();
69
70

	for (Model::moduleMapType::iterator mit = moduleMap.begin(), mitend = moduleMap.end();   mit != mitend;   mit++) {
Anirudh's avatar
Anirudh committed
71
72
		ModuleDecl *mainmd = mit->second;
		int numInstances = mainmd->getNumInstances();
73
74
75
76
77
78
		vector<ModuleDecl*> moduleDeclVec;

    _os << "\n";
		_os <<"For module: " << mit->first <<" num instance : " << numInstances;

		for (unsigned int num {0}; num < numInstances; ++num ) {
Anirudh's avatar
Anirudh committed
79
80
81
82
			ModuleDecl *md = new ModuleDecl();
			vector < EntryFunctionContainer * >_entryFunctionContainerVector;
	  	FindConstructor constructor(mainmd->getModuleClassDecl(), _os);
			md->addConstructor(constructor.returnConstructorStmt());
83

Anirudh's avatar
Anirudh committed
84
85
86
87
			FindPorts ports(mainmd->getModuleClassDecl(), _os);
			md->addInputPorts(ports.getInputPorts());
			md->addOutputPorts(ports.getOutputPorts());
			md->addInputOutputPorts(ports.getInputOutputPorts());
88

Anirudh's avatar
Anirudh committed
89
90
91
92
	  	FindTLMInterfaces findTLMInterfaces(mainmd->getModuleClassDecl(), _os);
	  	md->addInputInterfaces(findTLMInterfaces.getInputInterfaces());
	  	md->addOutputInterfaces(findTLMInterfaces.getOutputInterfaces());
	  	md->addInputOutputInterfaces(findTLMInterfaces.getInputOutputInterfaces());
93

Anirudh's avatar
Anirudh committed
94
95
			FindSignals signals(mainmd->getModuleClassDecl(), _os);
			md->addSignals(signals.getSignals());
96

Anirudh's avatar
Anirudh committed
97
			FindEntryFunctions findEntries(mainmd->getModuleClassDecl(), _os);
98
			FindEntryFunctions::entryFunctionVectorType * entryFunctions = findEntries.getEntryFunctions();
Anirudh's avatar
Anirudh committed
99
	  	md->addProcess(entryFunctions);
100
101

			for (size_t i = 0; i < entryFunctions->size(); i++)  {
rmrf's avatar
rmrf committed
102
          EntryFunctionContainer *ef = (*entryFunctions)[i];
103
          FindSensitivity findSensitivity(constructor.returnConstructorStmt(), _os);
rmrf's avatar
rmrf committed
104
          ef->addSensitivityInfo(findSensitivity);
105
106
107
108
109

          if (ef->getEntryMethod() == nullptr)  {
            _os << "ERROR";
            continue;
          }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
110

rmrf's avatar
rmrf committed
111
112
          FindWait findWaits(ef->getEntryMethod(), _os);
          ef->addWaits(findWaits);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
113

rmrf's avatar
rmrf committed
114
115
          FindNotify findNotify(ef->_entryMethodDecl, _os);
          ef->addNotifys(findNotify);
116

rmrf's avatar
rmrf committed
117
118
119
120
121
122
123
124
125
126
127
          /*
            SuspensionAutomata suspensionAutomata(findWaits.getWaitCalls(), ef->getEntryMethod(), &_context, llvm::errs());
            if (suspensionAutomata.initialize()) {
            suspensionAutomata.genSusCFG();
            //suspensionAutomata.dumpSusCFG();
            suspensionAutomata.genSauto();    
            //suspensionAutomata.dumpSauto();
            ef->addSusCFGAuto(suspensionAutomata); 
            }
          */ 
          _entryFunctionContainerVector.push_back(ef);
Anirudh's avatar
Anirudh committed
128
			
rmrf's avatar
rmrf committed
129
        }
Anirudh's avatar
Anirudh committed
130
131
			moduleDeclVec.push_back(md);
		}
rmrf's avatar
rmrf committed
132
    _systemcModel->addModuleDeclInstances(mainmd, moduleDeclVec);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
133
	}
Anirudh's avatar
Anirudh committed
134
135
	
	/*
rmrf's avatar
rmrf committed
136
    FindSCMain scmain(tu, _os);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
137

rmrf's avatar
rmrf committed
138
139
    if (scmain.isSCMainFound())
    {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
140
141
142
143
144
		FunctionDecl *fnDecl = scmain.getSCMainFunctionDecl();

		FindSimTime scstart(fnDecl, _os);
		_systemcModel->addSimulationTime(scstart.returnSimTime());

rmrf's avatar
rmrf committed
145
146
    }
    else {
Anirudh's avatar
Anirudh committed
147
		_os <<"\n Could not find SCMain";
rmrf's avatar
rmrf committed
148
149
150
151
    }
    FindNetlist findNetlist(scmain.getSCMainFunctionDecl());
    findNetlist.dump();
    _systemcModel->addNetlist(findNetlist); 
Anirudh's avatar
Anirudh committed
152
	*/
153
154
	// Generate SAUTO
	// Placing it here so that unique SAUTO for each instance
Anirudh's avatar
Anirudh committed
155
156
	//Model::moduleMapType moduleMap = _systemcModel->getModuleDecl();
	Model::moduleInstanceMapType moduleInstanceMap = _systemcModel->getModuleInstanceMap();
157

Anirudh's avatar
Anirudh committed
158
	for (Model::moduleInstanceMapType::iterator it = moduleInstanceMap.begin(), eit = moduleInstanceMap.end();
159
       it != eit;  it++) {
Anirudh's avatar
Anirudh committed
160
		vector<ModuleDecl*> moduleDeclVec = it->second;
161
		for (size_t i = 0; i < moduleDeclVec.size(); i++) {
Anirudh's avatar
Anirudh committed
162
			ModuleDecl *moduleDecl = moduleDeclVec.at(i);
163
			vector<EntryFunctionContainer*> entryFunctionContainer = moduleDecl->getEntryFunctionContainer();
164
			for (size_t j = 0; j < entryFunctionContainer.size(); j++) {
165
166
167
168
169
170
171
		
   			SuspensionAutomata suspensionAutomata(entryFunctionContainer.at(j)->getWaitCalls(), entryFunctionContainer.at(j)->getEntryMethod(), &_context, llvm::errs());
   			if (suspensionAutomata.initialize()) {
    			suspensionAutomata.genSusCFG();
    			//suspensionAutomata.dumpSusCFG();
    			suspensionAutomata.genSauto();    
    			//suspensionAutomata.dumpSauto();
Anirudh's avatar
Anirudh committed
172
    			entryFunctionContainer.at(j)->addSusCFGAuto(suspensionAutomata); 
173
174
175
176
177
   			}		
			}
		}
	}

178
179
  _os <<"\n";
  _os << "\n## SystemC model\n";
rmrf's avatar
rmrf committed
180
181
  _systemcModel->dump(_os); 
  return true;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
182
183
}

rmrf's avatar
rmrf committed
184
void SystemCConsumer::HandleTranslationUnit(ASTContext & context) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
185
186
187
188
189
190
191
192
	// ///////////////////////////////////////////////////////////////
	// / Pass 1: Find the necessary information.
	// ///////////////////////////////////////////////////////////////

	bool pre = false;

	pre = preFire();

rmrf's avatar
rmrf committed
193
	if (!pre) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
194
195
196
197
198
199
200
		return;
	}

	bool f = false;

	f = fire();

rmrf's avatar
rmrf committed
201
	if (!f) 	{
Anirudh Kaushik's avatar
Anirudh Kaushik committed
202
203
204
205
206
		return;
	}
	postFire();
}

rmrf's avatar
rmrf committed
207
SystemCConsumer::SystemCConsumer(CompilerInstance & ci) :
rmrf's avatar
rmrf committed
208
209
210
  _os(llvm::errs()),
  _sm(ci.getSourceManager()),
  _context(ci.getASTContext()),
Twiga's avatar
Update.    
Twiga committed
211
  _ci(ci),
rmrf's avatar
rmrf committed
212
  _systemcModel(nullptr) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
213
214
215

}

rmrf's avatar
rmrf committed
216
217
SystemCConsumer::~SystemCConsumer() {
	if ( _systemcModel != nullptr )	{
Anirudh Kaushik's avatar
Anirudh Kaushik committed
218
		delete _systemcModel;
rmrf's avatar
rmrf committed
219
    _systemcModel = nullptr;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
220
221
	}
}