OpenASIP  2.0
BlockSourceCopier.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2011 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file BlockSourceCopier.cc
26  *
27  * Implementation of BlockSourceCopier class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @author Pekka Jääskeläinen 2011
31  * @author Vinogradov Viacheslav(added Verilog generating) 2012
32  * @note rating: red
33  */
34 
35 #include <iostream>
36 #include <string>
37 #include <vector>
38 #include <fstream>
39 #include <boost/format.hpp>
40 
41 #include "BlockSourceCopier.hh"
42 #include "MachineImplementation.hh"
43 #include "HDBManager.hh"
44 #include "HDBRegistry.hh"
45 #include "FUEntry.hh"
46 #include "RFEntry.hh"
47 #include "FUImplementation.hh"
48 #include "RFImplementation.hh"
49 #include "FileSystem.hh"
50 #include "AssocTools.hh"
52 
53 using namespace IDF;
54 using namespace HDB;
55 using std::endl;
56 using std::string;
57 using std::vector;
58 
59 static const std::string UTILITY_VHDL_FILE = "tce_util_pkg.vhdl";
60 static const std::string UTILITY_VERILOG_FILE = "tce_util_pkg.vh";
61 
62 namespace ProGe {
63 
64 /**
65  * The constructor.
66  */
67 BlockSourceCopier::BlockSourceCopier(
69  TCEString entityStr,
70  const HDL language):
71  implementation_(implementation), entityStr_(entityStr), language_(language){
73 }
74 
75 
76 /**
77  * The destructor.
78  */
80 }
81 
82 
83 /**
84  * Copies the block definition files of the blocks given in IDF to
85  * proper subdirectories of the given directory.
86  *
87  * This method copies the files that can and are potentially shared by
88  * multiple TTAs in the same system design. That is, the FU, RF and IU
89  * implementations.
90  *
91  * @param dstDirectory The destination "root" directory.
92  * @exception IOException If some file cannot be copied or HDB cannot be
93  * opened.
94  */
95 void
96 BlockSourceCopier::copyShared(const std::string& dstDirectory) {
97  // copy FU files
98  for (int i = 0; i < implementation_.fuImplementationCount(); i++) {
99  FUImplementationLocation& fuImpl =
101  string hdbFile = fuImpl.hdbFile();
102  int id = fuImpl.id();
103  HDBManager& manager = HDBRegistry::instance().hdb(hdbFile);
104  FUEntry* entry = manager.fuByEntryID(id);
105  assert(entry->hasImplementation());
106  FUImplementation& impl = entry->implementation();
107  copyFiles(impl, hdbFile, dstDirectory);
108  delete entry;
109  }
110 
111  // copy RF files
112  for (int i = 0; i < implementation_.rfImplementationCount(); i++) {
113  RFImplementationLocation& rfImpl =
115  copyBaseRFFiles(rfImpl, dstDirectory);
116  }
117 
118  // copy IU files
119  for (int i = 0; i < implementation_.iuImplementationCount(); i++) {
120  RFImplementationLocation& rfImpl =
122  copyBaseRFFiles(rfImpl, dstDirectory);
123  }
124 
125  const string DS = FileSystem::DIRECTORY_SEPARATOR;
126  string sourceDir = Environment::dataDirPath("ProGe");
127  // copy the utility VHDL or Verilog files
130  dstDirectory + DS + ((language_==VHDL)?"vhdl":"verilog") + DS + ((language_==VHDL)?UTILITY_VHDL_FILE:UTILITY_VERILOG_FILE));
131 }
132 
133 /**
134  * Copies the block definition files of the blocks given in IDF to
135  * proper subdirectories of the given directory.
136  *
137  * This method copies the processor-specific files that are not reused
138  * between multiple TTAs.
139  *
140  * @param dstDirectory The destination "root" directory.
141  * @exception IOException If some file cannot be copied or HDB cannot be
142  * opened.
143  */
144 void
145 BlockSourceCopier::copyProcessorSpecific(const std::string& dstDirectory) {
146  // copy decompressor file
147  const string DS = FileSystem::DIRECTORY_SEPARATOR;
148  string decompressorTargetDir = dstDirectory + DS + "gcu_ic";
149  if (!FileSystem::fileExists(decompressorTargetDir)) {
150  if (!FileSystem::createDirectory(decompressorTargetDir)) {
151  string errorMsg = "Unable to create directory " +
152  decompressorTargetDir;
153  throw IOException(__FILE__, __LINE__, __func__, errorMsg);
154  }
155  }
156 
157  string sourceFile;
158  string dstFile;
160  sourceFile = implementation_.decompressorFile();
161  string file = FileSystem::fileOfPath(sourceFile);
162  dstFile = decompressorTargetDir + DS + file;
163  FileSystem::copy(sourceFile, dstFile);
164  } else {
165  sourceFile = Environment::dataDirPath("ProGe") + DS +
166  ((language_ == Verilog) ? "idecompressor.v.tmpl"
167  : "idecompressor.vhdl.tmpl");
168  string file =
169  ((language_ == Verilog) ? "idecompressor.v"
170  : "idecompressor.vhdl");
171  dstFile = decompressorTargetDir + DS + file;
172 
173  if (!FileSystem::fileExists(dstFile)) {
174  instantiator_.instantiateTemplateFile(sourceFile, dstFile);
175  }
176  }
177 
178  string ifetchSrcFile;
179  ifetchSrcFile =
180  Environment::dataDirPath("ProGe") + DS +
181  ((language_ == Verilog) ? "ifetch.v.tmpl" : "ifetch.vhdl.tmpl");
182  string ifetchTargetDir = decompressorTargetDir;
183  assert(FileSystem::fileExists(ifetchTargetDir));
184  string ifetchDstFile =
185  ifetchTargetDir + DS + ((language_==Verilog)?"ifetch.v":"ifetch.vhdl");
186 
187  if (!FileSystem::fileExists(ifetchDstFile)) {
188  instantiator_.instantiateTemplateFile(ifetchSrcFile, ifetchDstFile);
189  }
190 }
191 
192 /**
193  * Copies given template file to given directory and instantiates it, ie.
194  * removes the .tmpl from the filename and converts it to .vhdl while
195  * replacing occurances of "ENTITY_STR" with entityStr_.
196  *
197  * @param srcFile The location and name of the .tmpl file to copy
198  * @param dstDirectory The directory to copy to and instantiate in.
199  * @param newName New name for the file. If "0", only ".tmpl" is removed.
200  */
201 void
203  const std::string& srcFile, const std::string& dstDirectory,
204  std::string newName) {
205  const string DS = FileSystem::DIRECTORY_SEPARATOR;
206 
207  if (!FileSystem::fileExists(srcFile)) {
208  string errorMsg = "Source file " + srcFile + " not found.";
209  throw IOException(__FILE__, __LINE__, __func__, errorMsg);
210  }
211 
212  if (!FileSystem::fileExists(dstDirectory)) {
213  if (!FileSystem::createDirectory(dstDirectory)) {
214  string errorMsg = "Unable to create directory " +
215  dstDirectory;
216  throw IOException(__FILE__, __LINE__, __func__, errorMsg);
217  }
218  }
219 
220  string source = FileSystem::fileOfPath(srcFile);
221  string dstFile;
222 
223  if (newName == "0") {
224  dstFile = source.erase(source.find(".tmpl", 0), string::npos);
225  } else {
226  dstFile = newName;
227  }
228 
230  srcFile, dstDirectory + DS + dstFile);
231 }
232 
233 /**
234  * Returns reference to template instantiator instance used by this.
235  */
238  return instantiator_;
239 }
240 
241 /**
242  * Copies the block definition files of the given RF implementation to the
243  * proper subdirectories of the given directory.
244  *
245  * @param implementation The location of the RF implementation.
246  * @param dstDirectory The destination "root" directory.
247  * @exception IOException If some file cannot be copied or HDB cannot be
248  * opened.
249  */
250 void
253  const std::string& dstDirectory) {
254  string hdbFile = implementation.hdbFile();
255  int id = implementation.id();
256  HDBManager& manager = HDBRegistry::instance().hdb(hdbFile);
257  RFEntry* entry = manager.rfByEntryID(id);
258  assert(entry->hasImplementation());
259  RFImplementation& impl = entry->implementation();
260  copyFiles(impl, hdbFile, dstDirectory);
261  delete entry;
262 }
263 
264 /**
265  * Copies the block definition files of the given HW block implementation to
266  * the proper subdirectories of the given directory.
267  *
268  * @param implementation The block implementation.
269  * @param hdbFile The HDB file that contains the block.
270  * @param dstDirectory The destination "root" directory.
271  * @exception UnreachableStream If some file cannot be copied to the
272  * destination directory.
273  * @exception FileNotFound If the file referred to in HDB is not found.
274  */
275 void
278  const std::string& hdbFile, const std::string& dstDirectory) {
279  for (int i = 0; i < implementation.implementationFileCount(); i++) {
280  BlockImplementationFile& file = implementation.file(i);
281  vector<string> modulePaths = Environment::vhdlPaths(hdbFile);
282 
283  string absoluteFile;
284  try {
285  absoluteFile = FileSystem::findFileInSearchPaths(
286  modulePaths, file.pathToFile());
287  } catch (const Exception& e) {
288  string errorMsg =
289  "Problem with " + implementation.moduleName() + " in HDB " +
290  hdbFile + ".\n" +
291  "Unable to find file mentioned in HDB: " + file.pathToFile() +
292  ":\n";
293  errorMsg += e.errorMessage();
294  throw FileNotFound(__FILE__, __LINE__, __func__, errorMsg);
295  }
296 
297  if (!isCopied(absoluteFile)) {
298  string fileName = FileSystem::fileOfPath(absoluteFile);
299  string targetDir, targetFile;
301 
302  if(language_== VHDL && (ProGe::HDL)file.format()==VHDL) {
303  targetDir = dstDirectory + DS + "vhdl";
304  } else
305  if(language_== Verilog && (ProGe::HDL) file.format()==Verilog) {
306  targetDir = dstDirectory + DS + "verilog";
307  } else {
308  setCopied(absoluteFile);
309  continue;//next file for check
310  }
311 
312  targetFile = targetDir + DS + fileName;
313 
314  if (!FileSystem::fileExists(targetDir)) {
315  bool directoryCreated =
316  FileSystem::createDirectory(targetDir);
317  if (!directoryCreated) {
318  string errorMsg = "Unable to create directory " +
319  targetDir + ".";
320  throw IOException(__FILE__, __LINE__, __func__, errorMsg);
321  }
322  }
323 
324  try {
325  FileSystem::copy(absoluteFile, targetFile);
326  } catch (const Exception& e) {
327  string errorMsg = "Unable to copy file " + targetFile + ":";
328  errorMsg += e.errorMessage();
329 
330  throw UnreachableStream(
331  __FILE__, __LINE__, __func__, errorMsg);
332  }
333  setCopied(absoluteFile);
334  }
335  }
336 }
337 
338 /**
339  * Marks the file as copied.
340  *
341  * @param file The file.
342  */
343 void
344 BlockSourceCopier::setCopied(const std::string& file) {
345  copiedFiles_.insert(file);
346 }
347 
348 
349 
350 /**
351  * Tells whether the given file is copied already.
352  *
353  * @param file The file.
354  * @return True if the file is copied, otherwise false.
355  */
356 bool
357 BlockSourceCopier::isCopied(const std::string& file) const {
359 }
360 }
IDF::UnitImplementationLocation
Definition: UnitImplementationLocation.hh:48
HDB::FUEntry
Definition: FUEntry.hh:49
ProGe::BlockSourceCopier::copiedFiles_
std::set< std::string > copiedFiles_
Copied files.
Definition: BlockSourceCopier.hh:99
FileSystem.hh
FileNotFound
Definition: Exception.hh:224
HDB::RFEntry::hasImplementation
virtual bool hasImplementation() const
Definition: RFEntry.cc:74
FileSystem::createDirectory
static bool createDirectory(const std::string &path)
Definition: FileSystem.cc:400
HDB
Definition: CostDatabase.hh:49
ProGe::Verilog
@ Verilog
Verilog.
Definition: ProGeTypes.hh:42
UnreachableStream
Definition: Exception.hh:171
HDLTemplateInstantiator
Definition: HDLTemplateInstantiator.hh:45
implementation
IDF::MachineImplementation * implementation
the implementation definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:61
ProGe::BlockSourceCopier::copyFiles
void copyFiles(const HDB::HWBlockImplementation &implementation, const std::string &hdbFile, const std::string &dstDirectory)
Definition: BlockSourceCopier.cc:276
AssocTools::containsKey
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
HDB::RFEntry
Definition: RFEntry.hh:47
IDF::MachineImplementation::iuImplementation
RFImplementationLocation & iuImplementation(const std::string &iu) const
Definition: MachineImplementation.cc:399
HDLTemplateInstantiator.hh
ProGe::BlockSourceCopier::instantiator_
HDLTemplateInstantiator instantiator_
Object that instantiates templates.
Definition: BlockSourceCopier.hh:101
ProGe::BlockSourceCopier::copyBaseRFFiles
void copyBaseRFFiles(const IDF::RFImplementationLocation &implementation, const std::string &dstDirectory)
Definition: BlockSourceCopier.cc:251
HDB::BlockImplementationFile
Definition: BlockImplementationFile.hh:44
IDF::MachineImplementation::hasDecompressorFile
bool hasDecompressorFile() const
Definition: MachineImplementation.cc:219
FileSystem::fileOfPath
static std::string fileOfPath(const std::string pathName)
Definition: FileSystem.cc:101
UTILITY_VHDL_FILE
static const std::string UTILITY_VHDL_FILE
Definition: BlockSourceCopier.cc:59
ProGe::BlockSourceCopier::entityStr_
TCEString entityStr_
Definition: BlockSourceCopier.hh:96
IDF::MachineImplementation::rfImplementation
RFImplementationLocation & rfImplementation(const std::string &rf) const
Definition: MachineImplementation.cc:377
assert
#define assert(condition)
Definition: Application.hh:86
HDB::RFImplementation
Definition: RFImplementation.hh:50
ProGe::BlockSourceCopier::~BlockSourceCopier
virtual ~BlockSourceCopier()
Definition: BlockSourceCopier.cc:79
ProGe::VHDL
@ VHDL
VHDL.
Definition: ProGeTypes.hh:41
RFImplementation.hh
ProGe::BlockSourceCopier::instantiateHDLTemplate
void instantiateHDLTemplate(const std::string &srcFile, const std::string &dstDirectory, std::string newName="0")
Definition: BlockSourceCopier.cc:202
ProGe::BlockSourceCopier::copyProcessorSpecific
void copyProcessorSpecific(const std::string &dstDirectory)
Definition: BlockSourceCopier.cc:145
FUEntry.hh
__func__
#define __func__
Definition: Application.hh:67
FileSystem::copy
static void copy(const std::string &source, const std::string &target)
Definition: FileSystem.cc:524
HDB::HDBManager
Definition: HDBManager.hh:82
IDF::MachineImplementation::decompressorFile
std::string decompressorFile() const
Definition: MachineImplementation.cc:205
IDF::MachineImplementation::rfImplementationCount
int rfImplementationCount() const
Definition: MachineImplementation.cc:310
ProGe::BlockSourceCopier::setCopied
void setCopied(const std::string &file)
Definition: BlockSourceCopier.cc:344
HDB::RFEntry::implementation
RFImplementation & implementation() const
Definition: RFEntry.cc:102
Exception
Definition: Exception.hh:54
ProGe::BlockSourceCopier::language_
const HDL language_
Definition: BlockSourceCopier.hh:97
HDB::BlockImplementationFile::pathToFile
std::string pathToFile() const
Definition: BlockImplementationFile.cc:61
HDB::HWBlockImplementation
Definition: HWBlockImplementation.hh:49
FUImplementation.hh
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
IDF::MachineImplementation::iuImplementationCount
int iuImplementationCount() const
Definition: MachineImplementation.cc:321
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
UTILITY_VERILOG_FILE
static const std::string UTILITY_VERILOG_FILE
Definition: BlockSourceCopier.cc:60
HDB::FUImplementation
Definition: FUImplementation.hh:53
BlockSourceCopier.hh
ProGe::BlockSourceCopier::isCopied
bool isCopied(const std::string &file) const
Definition: BlockSourceCopier.cc:357
Environment::vhdlPaths
static std::vector< std::string > vhdlPaths(const std::string &hdbPath)
Definition: Environment.cc:760
IDF::MachineImplementation::fuImplementationCount
int fuImplementationCount() const
Definition: MachineImplementation.cc:299
IDF::UnitImplementationLocation::id
virtual int id() const
Definition: UnitImplementationLocation.cc:127
ProGe
Definition: FUGen.hh:54
FileSystem::fileExists
static bool fileExists(const std::string fileName)
AssocTools.hh
HDLTemplateInstantiator::setEntityString
void setEntityString(const TCEString &entityStr)
Definition: HDLTemplateInstantiator.hh:51
TCEString
Definition: TCEString.hh:53
HDB::BlockImplementationFile::format
Format format() const
Definition: BlockImplementationFile.cc:70
ProGe::HDL
HDL
HDLs supported by ProGe.
Definition: ProGeTypes.hh:40
ProGe::BlockSourceCopier::getTemplateInstatiator
HDLTemplateInstantiator & getTemplateInstatiator()
Definition: BlockSourceCopier.cc:237
ProGe::BlockSourceCopier::copyShared
void copyShared(const std::string &dstDirectory)
Definition: BlockSourceCopier.cc:96
RFEntry.hh
IDF::UnitImplementationLocation::hdbFile
virtual std::string hdbFile() const
Definition: UnitImplementationLocation.cc:99
IOException
Definition: Exception.hh:130
HDLTemplateInstantiator::instantiateTemplateFile
void instantiateTemplateFile(const std::string &templateFile, const std::string &dstFile)
Definition: HDLTemplateInstantiator.cc:113
HDB::FUEntry::hasImplementation
virtual bool hasImplementation() const
Definition: FUEntry.cc:74
IDF::MachineImplementation::fuImplementation
FUImplementationLocation & fuImplementation(const std::string &fu) const
Definition: MachineImplementation.cc:355
HDB::FUEntry::implementation
FUImplementation & implementation() const
Definition: FUEntry.cc:86
HDBManager.hh
DS
#define DS
Definition: LLVMBackend.cc:124
HDBRegistry.hh
FileSystem::findFileInSearchPaths
static std::string findFileInSearchPaths(const std::vector< std::string > &searchPaths, const std::string &file)
Definition: FileSystem.cc:562
HDB::HDBManager::fuByEntryID
FUEntry * fuByEntryID(RowID id) const
Definition: HDBManager.cc:2828
ProGe::BlockSourceCopier::implementation_
const IDF::MachineImplementation & implementation_
The IDF object model.
Definition: BlockSourceCopier.hh:95
IDF::MachineImplementation
Definition: MachineImplementation.hh:54
IDF
Definition: DSDBManager.hh:54
MachineImplementation.hh
Environment::dataDirPath
static std::string dataDirPath(const std::string &prog)
Definition: Environment.cc:176
HDB::HDBManager::rfByEntryID
RFEntry * rfByEntryID(RowID id) const
Definition: HDBManager.cc:2885