OpenASIP  2.0
DictionaryTool.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 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 DictionaryTool.cc
26  *
27  * DictionaryTool main application.
28  *
29  * @author Jari M�ntyneva 2006 (jari.mantyneva-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <cstdlib>
34 #include <iostream>
35 #include <fstream>
36 #include <string>
37 #include <vector>
38 
39 #include "ADFSerializer.hh"
40 #include "Application.hh"
41 #include "Machine.hh"
44 #include "CmdLineOptions.hh"
45 #include "Exception.hh"
46 #include "Binary.hh"
47 #include "TPEFWriter.hh"
48 #include "TPEFProgramFactory.hh"
49 #include "ProgramWriter.hh"
50 #include "BinaryStream.hh"
51 #include "MoveElement.hh"
52 #include "tce_config.h"
53 
54 using std::cout;
55 using std::cerr;
56 using std::endl;
57 using std::string;
58 using std::ofstream;
59 using std::vector;
60 using namespace TTAMachine;
61 using namespace TTAProgram;
62 using namespace TPEF;
63 
64 /**
65  * DictionaryTool commandline options.
66  */
68 public:
70  CmdLineOptions("Usage: dictionary_tool [options] adffile") {
71 
72  BoolCmdLineOptionParser* printPrimitive =
74  "primitive",
75  "Print missing operations from primitive operation set",
76  "p");
77 
78  addOption(printPrimitive);
79 
80  BoolCmdLineOptionParser* printConnections =
81  new BoolCmdLineOptionParser("connections",
82  "Print all connections in adf.",
83  "c");
84 
85  addOption(printConnections);
86 
87  BoolCmdLineOptionParser* printGlobalRegister =
89  "global_register",
90  "Print the name of global connection register", "g");
91 
92  addOption(printGlobalRegister);
93 
94  BoolCmdLineOptionParser* printMissingConnections =
96  "missing",
97  "Print missing connections to connection register",
98  "m");
99 
100  addOption(printMissingConnections);
101 
102  BoolCmdLineOptionParser* printRFConnections =
104  "reg_connections",
105  "Print all connections to the connection register.", "r");
106 
107  addOption(printRFConnections);
108 
109  BoolCmdLineOptionParser* doNotPrintErrors =
111  "no_errors",
112  "Don't print any error messages.", "e");
113 
114  addOption(doNotPrintErrors);
115 
116  BoolCmdLineOptionParser* writeBinary =
118  "binary",
119  "Write binary program as output.", "w");
120 
121  addOption(writeBinary);
122  }
123 
124  bool printPrimitive() {
125  return findOption("primitive")->isFlagOn();
126  }
127 
129  return findOption("connections")->isFlagOn();
130  }
131 
133  return findOption("global_register")->isFlagOn();
134  }
135 
137  return findOption("missing")->isFlagOn();
138  }
139 
141  return findOption("reg_connections")->isFlagOn();
142  }
143 
145  return findOption("no_errors")->isFlagOn();
146  }
147 
148  bool writeBinary() {
149  return findOption("binary")->isFlagOn();
150  }
151 
152  void printVersion() const {
153  cout << "dictionary_tool - Dictionary Tool "
154  << Application::TCEVersionString() << endl;
155  }
156 };
157 
158 
159 /**
160  * The dictionary_tool main function.
161  */
162 int main(int argc, char* argv[]) {
163 
165 
166  try {
167  options.parse(argv, argc);
168  } catch (ParserStopRequest) {
169  return EXIT_SUCCESS;
170  } catch (const IllegalCommandLine& e) {
171  cerr << e.errorMessage() << endl;
172  return EXIT_FAILURE;
173  }
174 
175  if (options.numberOfArguments() == 0) {
176  options.printHelp();
177  return EXIT_SUCCESS;
178  } else if (options.numberOfArguments() > 1) {
179  cerr << "Illegal number of arguments" << endl;
180  return EXIT_FAILURE;
181  }
182 
183  string adfFileName = options.argument(1);
184 
185  ADFSerializer adfSerializer;
186  adfSerializer.setSourceFile(adfFileName);
187 
188  TTAMachine::Machine* mach;
189 
190  try {
191  mach = adfSerializer.readMachine();
192  } catch (...) {
193  cout.flush();
194  cerr << "Error opening ADF file:" << endl
195  << "- Is the filename correct?" << endl
196  << "- Is the machine legal?" << endl;
197  return EXIT_FAILURE;
198  }
199 
200  ProgrammabilityValidator* validator;
201 
202  try {
203  validator = new ProgrammabilityValidator(*mach);
204  } catch (IllegalMachine& e) {
205  cerr << e.errorMessage() << endl;
206  return EXIT_FAILURE;
207  }
208 
209  ProgrammabilityValidatorResults booleanResults;
210  validator->checkBooleanRegister(booleanResults);
211  if (booleanResults.errorCount() != 0) {
212  for (int i = 0; i < booleanResults.errorCount(); i++) {
214  booleanResults.error(i);
215 
216  cerr << error.second << endl;
217  }
218  cerr << "Execution stopped." << endl;
219  return EXIT_SUCCESS;
220  }
221 
222  bool gcrError = false;
223 
225  TPEF::Binary* binaryProgram = NULL;
226  try {
227  binaryProgram = validator->profile(results);
228  } catch (NotAvailable& e) {
229  cerr << e.errorMessage() << endl;
230  cerr << "Check that all operations are found in OSAL." << endl;
231  cerr << "Create new ones with OSEd." << endl;
232  cerr << "Execution stopped." << endl;
233  return EXIT_FAILURE;
234  } catch (InstanceNotFound& e) {
235  cerr << e.errorMessage() << endl;
236  }
237 
238  if (options.writeBinary()) {
239  string outputFile = adfFileName.append("_profiled.tpef");
240  BinaryStream tpefOut(outputFile);
241  TPEFWriter::instance().writeBinary(tpefOut, binaryProgram);
242  }
243 
244  if (!options.doNotPrintErrors()) {
245  for (int i = 0; i < results.errorCount(); i++) {
247  if (error.first ==
249  OPERATION_MISSING_FROM_THE_PRIMITIVE_OPERATION_SET) {
250  if (options.printPrimitive()) {
251  cerr << error.second << endl;
252  }
253  }
254  else if (error.first ==
256  GLOBAL_CONNECTION_REGISTER_NOT_FOUND) {
257  if (options.printGlobalRegister()) {
258  gcrError = true;
259  cerr << error.second << endl;
260  }
261  }
262  else if (error.first ==
264  MISSING_CONNECTION) {
265  if (options.printMissingConnections()) {
266  cerr << error.second << endl;
267  }
268  }
269  else {
270  cerr << error.second << endl;
271  }
272  }
273  }
274 
275  if (options.printGlobalRegister() && !gcrError) {
276  cout << "* The global connection register in the machine is: "
277  << validator->findGlobalConnectionRegister()->name()
278  << endl;
279  }
280 
281  if (options.printConnections()) {
282  validator->printConnections();
283  }
284 
285  if (options.printRFConnections()) {
286  validator->printRegisterConnections();
287  }
288 
289  return EXIT_SUCCESS;
290 }
TTAProgram
Definition: Estimator.hh:65
ProgrammabilityValidatorResults.hh
MachInfoCmdLineOptions::printHelp
virtual void printHelp() const
Definition: MachInfoCmdLineOptions.cc:89
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
ParserStopRequest
Definition: Exception.hh:491
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
ProgrammabilityValidator::printConnections
void printConnections()
Definition: ProgrammabilityValidator.cc:1766
Exception.hh
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
ProgrammabilityValidatorResults
Definition: ProgrammabilityValidatorResults.hh:46
TPEF::Binary
Definition: Binary.hh:49
TPEF::BinaryStream
Definition: BinaryStream.hh:59
ProgramWriter.hh
DictionaryToolCmdLineOptions::doNotPrintErrors
bool doNotPrintErrors()
Definition: DictionaryTool.cc:144
CmdLineOptions.hh
DictionaryToolCmdLineOptions::printVersion
void printVersion() const
Definition: DictionaryTool.cc:152
IllegalCommandLine
Definition: Exception.hh:438
ProgrammabilityValidator::profile
TPEF::Binary * profile(ProgrammabilityValidatorResults &results)
Definition: ProgrammabilityValidator.cc:269
ProgrammabilityValidatorResults::error
Error error(int index) const
Definition: ProgrammabilityValidatorResults.cc:70
NotAvailable
Definition: Exception.hh:728
ProgrammabilityValidator::findGlobalConnectionRegister
const RegisterFile * findGlobalConnectionRegister()
Definition: ProgrammabilityValidator.cc:1789
ProgrammabilityValidator::checkBooleanRegister
bool checkBooleanRegister(ProgrammabilityValidatorResults &results)
Definition: ProgrammabilityValidator.cc:361
DictionaryToolCmdLineOptions::printMissingConnections
bool printMissingConnections()
Definition: DictionaryTool.cc:136
DictionaryToolCmdLineOptions::printConnections
bool printConnections()
Definition: DictionaryTool.cc:128
CmdLineOptions
Definition: CmdLineOptions.hh:54
ADFSerializer
Definition: ADFSerializer.hh:49
Application.hh
BoolCmdLineOptionParser
Definition: CmdLineOptionParser.hh:278
ProgrammabilityValidator
Definition: ProgrammabilityValidator.hh:75
Machine.hh
DictionaryToolCmdLineOptions::printPrimitive
bool printPrimitive()
Definition: DictionaryTool.cc:124
ProgrammabilityValidator.hh
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
CmdLineOptions::parse
void parse(char *argv[], int argc)
Definition: CmdLineOptions.cc:107
DictionaryToolCmdLineOptions::printRFConnections
bool printRFConnections()
Definition: DictionaryTool.cc:140
DictionaryToolCmdLineOptions
Definition: DictionaryTool.cc:67
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
TPEFProgramFactory.hh
IllegalMachine
Definition: Exception.hh:878
ADFSerializer.hh
BinaryStream.hh
ADFSerializer::readMachine
TTAMachine::Machine * readMachine()
Definition: ADFSerializer.cc:275
TPEFWriter.hh
ProgrammabilityValidatorResults::errorCount
int errorCount() const
Definition: ProgrammabilityValidatorResults.cc:56
DictionaryToolCmdLineOptions::DictionaryToolCmdLineOptions
DictionaryToolCmdLineOptions()
Definition: DictionaryTool.cc:69
DictionaryToolCmdLineOptions::printGlobalRegister
bool printGlobalRegister()
Definition: DictionaryTool.cc:132
TTAMachine
Definition: Assembler.hh:48
main
int main(int argc, char *argv[])
Definition: DictionaryTool.cc:162
CmdLineParser::argument
virtual std::string argument(int index) const
Application::TCEVersionString
static std::string TCEVersionString()
Definition: Application.cc:510
MoveElement.hh
DictionaryToolCmdLineOptions::writeBinary
bool writeBinary()
Definition: DictionaryTool.cc:148
InstanceNotFound
Definition: Exception.hh:304
TPEF
Definition: Assembler.hh:43
TTAMachine::Machine
Definition: Machine.hh:73
Binary.hh
ProgrammabilityValidatorResults::Error
std::pair< ProgrammabilityValidator::ErrorCode, std::string > Error
Typedef for an error (error code + error message).
Definition: ProgrammabilityValidatorResults.hh:50
ProgrammabilityValidator::printRegisterConnections
void printRegisterConnections()
Definition: ProgrammabilityValidator.cc:1356