SystemCClang.cpp 6.81 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

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

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

	// ANI : Do we need FindGlobalEvents?
rmrf's avatar
rmrf committed
28
29
	FindGlobalEvents fglobals{ tu, _os };
	FindGlobalEvents::globalEventMapType eventMap { fglobals.getEventMap() };
Anirudh Kaushik's avatar
Anirudh Kaushik committed
30
31
	_systemcModel->addGlobalEvents(eventMap);

32

rmrf's avatar
rmrf committed
33
	SCModules::moduleMapType scmodules{ scmod.getSystemCModulesMap() };
34
35
36

  for ( SCModules::moduleMapType::iterator mit = scmodules.begin(),
          mitend = scmodules.end(); mit != mitend; ++mit )  {
rmrf's avatar
rmrf committed
37
    ModuleDecl *md = new ModuleDecl{ mit->first, mit->second };
38
39
40
    _systemcModel->addModuleDecl(md);
  }

Anirudh's avatar
Anirudh committed
41
	////////////////////////////////////////////////////////////////
42
43
  // Find the sc_main
  ////////////////////////////////////////////////////////////////
rmrf's avatar
rmrf committed
44
	FindSCMain scmain{ tu, _os };
Anirudh Kaushik's avatar
Anirudh Kaushik committed
45

46
	if ( scmain.isSCMainFound() ) {
rmrf's avatar
rmrf committed
47
    FunctionDecl *fnDecl{ scmain.getSCMainFunctionDecl() };
Anirudh's avatar
Anirudh committed
48

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

  ////////////////////////////////////////////////////////////////
  // Find the netlist.
  ////////////////////////////////////////////////////////////////
rmrf's avatar
rmrf committed
58
	FindNetlist findNetlist{ scmain.getSCMainFunctionDecl() };
Anirudh's avatar
Anirudh committed
59
 	findNetlist.dump();
60
61
	_systemcModel->addNetlist(findNetlist);

Anirudh's avatar
Anirudh committed
62
	////////////////////////////////////////////////////////////////
63
64
  // Figure out the module map.
  ////////////////////////////////////////////////////////////////
rmrf's avatar
rmrf committed
65
	Model::moduleMapType moduleMap{ _systemcModel->getModuleDecl() };
66
67

	for (Model::moduleMapType::iterator mit = moduleMap.begin(), mitend = moduleMap.end();   mit != mitend;   mit++) {
rmrf's avatar
rmrf committed
68
69
		ModuleDecl *mainmd{ mit->second };
		int numInstances{ mainmd->getNumInstances() };
70
71
72
73
74
		vector<ModuleDecl*> moduleDeclVec;

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

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

rmrf's avatar
rmrf committed
81
82
83
84
			FindPorts ports{ mainmd->getModuleClassDecl(), _os };
			md->addInputPorts( ports.getInputPorts() );
			md->addOutputPorts( ports.getOutputPorts() );
			md->addInputOutputPorts( ports.getInputOutputPorts() );
85

rmrf's avatar
rmrf committed
86
87
88
89
	  	FindTLMInterfaces findTLMInterfaces{ mainmd->getModuleClassDecl(), _os };
	  	md->addInputInterfaces( findTLMInterfaces.getInputInterfaces() );
	  	md->addOutputInterfaces( findTLMInterfaces.getOutputInterfaces() );
	  	md->addInputOutputInterfaces( findTLMInterfaces.getInputOutputInterfaces() );
90

rmrf's avatar
rmrf committed
91
92
			FindSignals signals{ mainmd->getModuleClassDecl(), _os };
			md->addSignals( signals.getSignals() );
93

rmrf's avatar
rmrf committed
94
95
96
			FindEntryFunctions findEntries{ mainmd->getModuleClassDecl(), _os };
			FindEntryFunctions::entryFunctionVectorType *entryFunctions{ findEntries.getEntryFunctions() };
	  	md->addProcess( entryFunctions );
97
98

			for (size_t i = 0; i < entryFunctions->size(); i++)  {
rmrf's avatar
rmrf committed
99
100
101
        EntryFunctionContainer *ef{ (*entryFunctions)[i] };
        FindSensitivity findSensitivity{ constructor.returnConstructorStmt(), _os };
        ef->addSensitivityInfo(findSensitivity);
102

rmrf's avatar
rmrf committed
103
          if ( ef->getEntryMethod() == nullptr )  {
104
105
106
            _os << "ERROR";
            continue;
          }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
107

rmrf's avatar
rmrf committed
108
          FindWait findWaits{ ef->getEntryMethod(), _os };
rmrf's avatar
rmrf committed
109
          ef->addWaits(findWaits);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
110

rmrf's avatar
rmrf committed
111
          FindNotify findNotify{ ef->_entryMethodDecl, _os };
rmrf's avatar
rmrf committed
112
          ef->addNotifys(findNotify);
113

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

Anirudh's avatar
Anirudh committed
131
	/*
rmrf's avatar
rmrf committed
132
    FindSCMain scmain(tu, _os);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
133

rmrf's avatar
rmrf committed
134
135
    if (scmain.isSCMainFound())
    {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
136
137
138
139
140
		FunctionDecl *fnDecl = scmain.getSCMainFunctionDecl();

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

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

Anirudh's avatar
Anirudh committed
154
	for (Model::moduleInstanceMapType::iterator it = moduleInstanceMap.begin(), eit = moduleInstanceMap.end();
155
       it != eit;  it++) {
Anirudh's avatar
Anirudh committed
156
		vector<ModuleDecl*> moduleDeclVec = it->second;
157
		for (size_t i = 0; i < moduleDeclVec.size(); i++) {
Anirudh's avatar
Anirudh committed
158
			ModuleDecl *moduleDecl = moduleDeclVec.at(i);
159
			vector<EntryFunctionContainer*> entryFunctionContainer = moduleDecl->getEntryFunctionContainer();
160
			for (size_t j = 0; j < entryFunctionContainer.size(); j++) {
161
162
163
164
165
166
167
		
   			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
168
    			entryFunctionContainer.at(j)->addSusCFGAuto(suspensionAutomata); 
169
170
171
172
173
   			}		
			}
		}
	}

174
175
  _os <<"\n";
  _os << "\n## SystemC model\n";
rmrf's avatar
rmrf committed
176
177
  _systemcModel->dump(_os); 
  return true;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
178
179
}

rmrf's avatar
rmrf committed
180
void SystemCConsumer::HandleTranslationUnit(ASTContext & context) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
181
182
183
184
185
186
187
188
	// ///////////////////////////////////////////////////////////////
	// / Pass 1: Find the necessary information.
	// ///////////////////////////////////////////////////////////////

	bool pre = false;

	pre = preFire();

rmrf's avatar
rmrf committed
189
	if (!pre) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
190
191
192
193
194
195
196
		return;
	}

	bool f = false;

	f = fire();

rmrf's avatar
rmrf committed
197
	if (!f) 	{
Anirudh Kaushik's avatar
Anirudh Kaushik committed
198
199
200
201
202
		return;
	}
	postFire();
}

rmrf's avatar
rmrf committed
203
SystemCConsumer::SystemCConsumer(CompilerInstance & ci) :
rmrf's avatar
rmrf committed
204
205
206
207
208
  _os{ llvm::errs() },
  _sm{ ci.getSourceManager() },
  _context{ ci.getASTContext() },
  _ci{ ci },
  _systemcModel{ nullptr } {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
209
210
211

}

rmrf's avatar
rmrf committed
212
213
SystemCConsumer::~SystemCConsumer() {
	if ( _systemcModel != nullptr )	{
Anirudh Kaushik's avatar
Anirudh Kaushik committed
214
		delete _systemcModel;
rmrf's avatar
rmrf committed
215
    _systemcModel = nullptr;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
216
217
	}
}