Model.cpp 5.94 KB
Newer Older
Anirudh Kaushik's avatar
Anirudh Kaushik committed
1
2
3
#include <string>
#include "Model.h"
#include "SCModules.h"
rmrf's avatar
rmrf committed
4

Anirudh Kaushik's avatar
Anirudh Kaushik committed
5
6
7
using namespace scpar;
using namespace std;

8
Model::Model() {
rmrf's avatar
rmrf committed
9

Anirudh Kaushik's avatar
Anirudh Kaushik committed
10
11
}

12
Model::~Model() {
13
  //  llvm::errs() << "\n[[ Destructor Model ]]\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
14
  // Delete all ModuleDecl pointers.
Twiga's avatar
Twiga committed
15
16
  for (Model::moduleMapType::iterator mit = modules_.begin();
       mit != modules_.end(); mit++) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
17
18
19
    // Second is the ModuleDecl type.
    delete mit->second;
  }
Twiga's avatar
Twiga committed
20
  modules_.clear();
Anirudh Kaushik's avatar
Anirudh Kaushik committed
21
22
}

23
Model::Model( const Model & from ) {
Twiga's avatar
Twiga committed
24
  modules_ = from.modules_;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
25
26
}

27
void Model::addModuleDecl( ModuleDecl * md ) {
Twiga's avatar
Twiga committed
28
  modules_.insert( Model::modulePairType( md->getName(), md ) );
Anirudh Kaushik's avatar
Anirudh Kaushik committed
29
30
}

31
void Model::addModuleDeclInstances( ModuleDecl* md, vector<ModuleDecl*> mdVec ) {
Twiga's avatar
Twiga committed
32
	module_instance_map_.insert(moduleInstancePairType(md, mdVec));
Anirudh's avatar
Anirudh committed
33
34
}

35
void Model::addSimulationTime(FindSimTime::simulationTimeMapType simTime) {
Twiga's avatar
Twiga committed
36
  simulation_time_ = simTime;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
37
38
}

Anirudh's avatar
Anirudh committed
39
void Model::addEntryFunctionGPUMacroMap(entryFunctionGPUMacroMapType e) {
Twiga's avatar
Twiga committed
40
41
42
	//entry_function_gpu_macro_map_.insert(e.begin(), e.end());
	entry_function_gpu_macro_map_ = e;
	llvm::errs()<<" \n Size : " <<entry_function_gpu_macro_map_.size()<<" " <<e.size();
Anirudh's avatar
Anirudh committed
43
44
45
}


46
void Model::addGlobalEvents(FindGlobalEvents::globalEventMapType eventMap) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
47
48
49
50
51
  for (FindGlobalEvents::globalEventMapType::iterator it = eventMap.begin();
       it != eventMap.end(); it++) {
    string eventName = it->first;
    EventContainer *event = new EventContainer(eventName, it->second);

Twiga's avatar
Twiga committed
52
    event_map_.insert(eventPairType(eventName, event));
Anirudh Kaushik's avatar
Anirudh Kaushik committed
53
54
55
  }
}

56
void Model::addSCMain(FunctionDecl *fnDecl) {
Twiga's avatar
Twiga committed
57
  scmain_function_decl_ = fnDecl;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
58
59
}

60
void Model::addNetlist( FindNetlist &n ) {
Twiga's avatar
Twiga committed
61
62
63
64
  instance_module_map_ = n.getInstanceModuleMap();
  port_signal_map_ = n.getPortSignalMap();
  port_signal_instance_map_ = n.getInstancePortSignalMap();
  module_instance_list_ = n.getInstanceListModuleMap();
65

Anirudh Kaushik's avatar
Anirudh Kaushik committed
66
67
68
69
70
  updateModuleDecl();
}

void Model::updateModuleDecl() {

Twiga's avatar
Twiga committed
71
  for (moduleMapType::iterator it = modules_.begin(), eit = modules_.end();
72
73
74
75
76
77
       it != eit;  it++) {
    string moduleName = it->first;
    ModuleDecl *md = it->second;
    vector<string> instanceList;

    llvm::errs() << "Finding instances for " << moduleName << " declaration: ";
Twiga's avatar
Twiga committed
78
79
    if ( module_instance_list_.find(moduleName) != module_instance_list_.end() ) {
      FindNetlist::instanceListModuleMapType::iterator instanceListModuleMapFind = module_instance_list_.find(moduleName);
80
81
82
83
84
85
86
87
88
      md->addInstances(instanceListModuleMapFind->second);

      // Print the names of all the instances
      for ( auto instance : instanceListModuleMapFind->second ) {
        llvm::errs() << instance << " ";
      }
      llvm::errs() << "\n";

      for (size_t i = 0 ; i < instanceListModuleMapFind->second.size(); i++) {
Twiga's avatar
Twiga committed
89
90
        if (port_signal_instance_map_.find(instanceListModuleMapFind->second.at(i)) != port_signal_instance_map_.end()) {
          FindNetlist::instancePortSignalMapType::iterator portSignalMapFound = port_signal_instance_map_.find(instanceListModuleMapFind->second.at(i));
91
92
93
94
95
96
97
98
99
100
          FindNetlist::portSignalMapType portSignalMap = portSignalMapFound->second;

          md->addSignalBinding(portSignalMap);
        }  else {
          llvm::errs() <<"\n Could not find instance and signal";
        }
      }
    } else {
      llvm::errs() <<"NONE.";
    }
101
  }
Anirudh Kaushik's avatar
Anirudh Kaushik committed
102
103
}

Twiga's avatar
Update.    
Twiga committed
104
void Model::addSCModules(SCModules * m) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
105
106
107
108
109
110
111
112
  SCModules::moduleMapType mods = m->getSystemCModulesMap();

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

Twiga's avatar
Update.    
Twiga committed
113
Model::moduleMapType Model::getModuleDecl() {
Twiga's avatar
Twiga committed
114
  return modules_;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
115
116
}

Anirudh's avatar
Anirudh committed
117
Model::entryFunctionGPUMacroMapType Model::getEntryFunctionGPUMacroMap() {
Twiga's avatar
Twiga committed
118
119
	llvm::errs()<<"\n return Size : " <<entry_function_gpu_macro_map_.size();
	return entry_function_gpu_macro_map_;
Anirudh's avatar
Anirudh committed
120
}
Anirudh's avatar
Anirudh committed
121

Anirudh's avatar
Anirudh committed
122
Model::moduleInstanceMapType Model::getModuleInstanceMap() {
Twiga's avatar
Twiga committed
123
	return module_instance_map_;
Anirudh's avatar
Anirudh committed
124
125
}

Twiga's avatar
Update.    
Twiga committed
126
Model::eventMapType Model::getEventMapType() {
Twiga's avatar
Twiga committed
127
  return event_map_;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
128
129
}

130
unsigned int Model::getNumEvents() {
Twiga's avatar
Twiga committed
131
  return (event_map_.size() - 3);
Anirudh Kaushik's avatar
Anirudh Kaushik committed
132
133
}

134
void Model::dump( llvm::raw_ostream & os ) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
135

Twiga's avatar
Twiga committed
136
  os << "\n# Number of modules : " << modules_.size();
Anirudh Kaushik's avatar
Anirudh Kaushik committed
137

Twiga's avatar
Twiga committed
138
139
  for (Model::moduleMapType::iterator mit = modules_.begin();
       mit != modules_.end(); mit++) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
140
141
    // Second is the ModuleDecl type.

Twiga's avatar
Twiga committed
142
    vector<ModuleDecl*> instanceVec = module_instance_map_[mit->second];
143
    os << "\n# Module " << mit->first << ": " << instanceVec.size() << " instances.";
144
		for (size_t i = 0; i < instanceVec.size(); i++) {
145
      //			os <<", instance: " << i + 1 << " ";
Anirudh's avatar
Anirudh committed
146
147
			instanceVec.at(i)->dump(os);
		}
Anirudh Kaushik's avatar
Anirudh Kaushik committed
148
  }
149
150
  os << "\n\n";
  os << "# Global events:\n";
Twiga's avatar
Twiga committed
151
152
  for (Model::eventMapType::iterator it = event_map_.begin(), ite =
         event_map_.end(); it != ite; it++) {
Anirudh Kaushik's avatar
Anirudh Kaushik committed
153
154
155
    os << "   Event: " << it->first << "  VarDecl: " << it->second << "\n";
  }

156
157
  os << "\n";
  os << "# Simulation time: ";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
158
  for (FindSimTime::simulationTimeMapType::iterator it =
Twiga's avatar
Twiga committed
159
         simulation_time_.begin(), eit = simulation_time_.end(); it != eit; it++) {
160
    os << it->first << " " << it->second;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
161
162
  }

163
164
  os << "\n\n";
  os <<"# Netlist: " ;
Twiga's avatar
Twiga committed
165
  for (FindNetlist::instanceModuleMapType::iterator it = instance_module_map_.begin(), eit = instance_module_map_.end(); it != eit; it++) {
166
167
    os << "\n";
    os << "Instance Name: " << it->first << ", module name : " << it->second;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
168
		string instanceName = it->first;
Twiga's avatar
Twiga committed
169
170
		if (port_signal_instance_map_.find(instanceName) !=
        port_signal_instance_map_.end())	{
171
      FindNetlist::instancePortSignalMapType::iterator instancePortSignalMapFound =
Twiga's avatar
Twiga committed
172
				port_signal_instance_map_.find(instanceName);
173
      FindNetlist::portSignalMapType portSignalMap =
Anirudh Kaushik's avatar
Anirudh Kaushik committed
174
175
				instancePortSignalMapFound->second;
			for (FindNetlist::portSignalMapType::iterator pit =
176
177
178
179
             portSignalMap.begin(), pite = portSignalMap.end();
           pit != pite; pit++)	{
        os << "\n";
        os << "Port: " << pit->first << " bound to signal " << pit->second;
Anirudh Kaushik's avatar
Anirudh Kaushik committed
180
			}
181
182
		}	else	{
      os << "\n No instance name found ////// weird.";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
183
		}
184
    os << "\n ------------------------------------------------------\n";
Anirudh Kaushik's avatar
Anirudh Kaushik committed
185
186
	}
}