OpenASIP 2.2
Loading...
Searching...
No Matches
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"
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
53using namespace IDF;
54using namespace HDB;
55using std::endl;
56using std::string;
57using std::vector;
58
59static const std::string UTILITY_VHDL_FILE = "tce_util_pkg.vhdl";
60static const std::string UTILITY_VERILOG_FILE = "tce_util_pkg.vh";
61
62namespace ProGe {
63
64/**
65 * The constructor.
66 */
69 TCEString entityStr,
70 const HDL language):
71 implementation_(implementation), entityStr_(entityStr), language_(language){
73}
74
75
76/**
77 * The destructor.
78 */
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 */
95void
96BlockSourceCopier::copyShared(const std::string& dstDirectory) {
97 // copy FU files
98 for (int i = 0; i < implementation_.fuImplementationCount(); i++) {
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++) {
115 copyBaseRFFiles(rfImpl, dstDirectory);
116 }
117
118 // copy IU files
119 for (int i = 0; i < implementation_.iuImplementationCount(); i++) {
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 */
144void
145BlockSourceCopier::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 */
201void
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 */
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 */
250void
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 */
275void
278 const std::string& hdbFile, const std::string& dstDirectory) {
279 for (int i = 0; i < implementation.implementationFileCount(); i++) {
281 vector<string> modulePaths = Environment::vhdlPaths(hdbFile);
282
283 string absoluteFile;
284 try {
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 =
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 */
343void
344BlockSourceCopier::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 */
356bool
357BlockSourceCopier::isCopied(const std::string& file) const {
359}
360}
#define __func__
#define assert(condition)
static const std::string UTILITY_VERILOG_FILE
static const std::string UTILITY_VHDL_FILE
IDF::MachineImplementation * implementation
the implementation definition of the estimated processor
#define DS
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
static std::string dataDirPath(const std::string &prog)
static std::vector< std::string > vhdlPaths(const std::string &hdbPath)
std::string errorMessage() const
Definition Exception.cc:123
static bool createDirectory(const std::string &path)
static const std::string DIRECTORY_SEPARATOR
static std::string fileOfPath(const std::string pathName)
static void copy(const std::string &source, const std::string &target)
static std::string findFileInSearchPaths(const std::vector< std::string > &searchPaths, const std::string &file)
static bool fileExists(const std::string fileName)
FUImplementation & implementation() const
Definition FUEntry.cc:86
virtual bool hasImplementation() const
Definition FUEntry.cc:74
FUEntry * fuByEntryID(RowID id) const
RFEntry * rfByEntryID(RowID id) const
static HDBRegistry & instance()
CachedHDBManager & hdb(const std::string fileName)
virtual bool hasImplementation() const
Definition RFEntry.cc:74
RFImplementation & implementation() const
Definition RFEntry.cc:102
void instantiateTemplateFile(const std::string &templateFile, const std::string &dstFile)
void setEntityString(const TCEString &entityStr)
RFImplementationLocation & iuImplementation(const std::string &iu) const
RFImplementationLocation & rfImplementation(const std::string &rf) const
FUImplementationLocation & fuImplementation(const std::string &fu) const
void copyShared(const std::string &dstDirectory)
const IDF::MachineImplementation & implementation_
The IDF object model.
void setCopied(const std::string &file)
HDLTemplateInstantiator instantiator_
Object that instantiates templates.
HDLTemplateInstantiator & getTemplateInstatiator()
bool isCopied(const std::string &file) const
void instantiateHDLTemplate(const std::string &srcFile, const std::string &dstDirectory, std::string newName="0")
void copyBaseRFFiles(const IDF::RFImplementationLocation &implementation, const std::string &dstDirectory)
std::set< std::string > copiedFiles_
Copied files.
void copyFiles(const HDB::HWBlockImplementation &implementation, const std::string &hdbFile, const std::string &dstDirectory)
BlockSourceCopier(const IDF::MachineImplementation &implementation, TCEString entityStr, const ProGe::HDL language)
void copyProcessorSpecific(const std::string &dstDirectory)
Definition FUGen.hh:54
HDL
HDLs supported by ProGe.
Definition ProGeTypes.hh:40
@ Verilog
Verilog.
Definition ProGeTypes.hh:42
@ VHDL
VHDL.
Definition ProGeTypes.hh:41