OpenASIP  2.0
tceasm.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 tceasm.cc
26  *
27  * TCE paraller assembler commandline client.
28  *
29  * @author Mikael Lepist� 2005 (tmlepist-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <iostream>
34 
35 #include "tce_config.h"
36 
37 #include "CmdLineOptions.hh"
38 #include "Assembler.hh"
39 #include "ADFSerializer.hh"
40 #include "Machine.hh"
41 #include "Binary.hh"
42 #include "TPEFWriter.hh"
43 #include "FileSystem.hh"
44 #include "BinaryStream.hh"
45 
46 using namespace TTAMachine;
47 using namespace TPEF;
48 
49 /**
50  * Commandline options.
51  */
53 public:
55  CmdLineOptions("Usage: tceasm [options] adffile assemblerfile") {
56 
57  StringCmdLineOptionParser* outputFile =
58  new StringCmdLineOptionParser("outputfile", "Name of the output file.", "o");
59 
60  addOption(outputFile);
61 
62  BoolCmdLineOptionParser* quietMode =
63  new BoolCmdLineOptionParser("quiet", "Don't print warnings.", "q");
64 
65  addOption(quietMode);
66  }
67 
68  std::string outputFile() {
69  return findOption("outputfile")->String();
70  }
71 
72  bool printWarnings() {
73  return findOption("quiet")->isFlagOff();
74  }
75 
76  void printVersion() const {
77  std::cout << "tceasm - OpenASIP TTA parallel assembler "
79  << std::endl;
80  }
81 };
82 
83 int main(int argc, char *argv[]) {
84 
86  Machine* machine = NULL;
87 
88  try {
89  options.parse(argv, argc);
90  } catch (const ParserStopRequest& e) {
91  return EXIT_SUCCESS;
92  } catch (IllegalCommandLine &e) {
93  std::cerr << "Error: Illegal commandline: "
94  << e.errorMessage() << "\n\n";
96  return 1;
97  }
98 
99  if (options.numberOfArguments() != 2) {
100  std::cerr << "Error: Illegal number of parameters.\n\n";
101  options.printHelp();
102  return 1;
103  }
104 
105  // find out which parameter was adf file
106  ADFSerializer machineReader;
107  machineReader.setSourceFile(options.argument(1));
108 
109  std::string adfFileName = options.argument(1);
110  std::string asmFileName = options.argument(2);
111 
112  try {
113  machine = machineReader.readMachine();
114  } catch ( ... ) {
115  std::cerr << "Error: " << adfFileName << " is not valid ADF file.\n\n";
116  options.printHelp();
117  return 1;
118  }
119 
120  if (!FileSystem::fileIsReadable(asmFileName)) {
121  std::cerr << "Error: cannot read " << asmFileName << std::endl;
122  return 2;
123  }
124 
125  // generate default output file name:
126  // file.name.ending -> file.name.tpef
127  // justafile -> justafile.tpef
128  std::string::size_type dotPosition = asmFileName.rfind('.');
129  std::string genOutputFileName;
130 
131  std::string outputFileName;
132 
133  if (dotPosition != std::string::npos) {
134  genOutputFileName = asmFileName.substr(0, dotPosition);
135  } else {
136  genOutputFileName = asmFileName;
137  }
138 
139  genOutputFileName += ".tpef";
140 
141  outputFileName = options.outputFile();
142 
143  if (outputFileName == "") {
144  outputFileName = genOutputFileName;
145  };
146 
147  BinaryStream asmFile(asmFileName);
148 
149  Assembler assembler(asmFile, *machine);
150 
151  // if there was failure during compilation
152  bool failure = false;
153 
154  try {
155  Binary* compiledTPEF = assembler.compile();
156 
157  if (options.printWarnings()) {
158  for (const CompilerMessage& message : assembler.warnings()) {
159  std::cerr << message.toString() << std::endl;
160  }
161  }
162 
163  try {
164  BinaryStream tpefStream(outputFileName);
165 
166  TPEFWriter::instance().writeBinary(tpefStream, compiledTPEF);
167 
168  } catch (Exception& e) {
169  // if error during the TPEF writing to file.
170  delete compiledTPEF;
171  compiledTPEF = NULL;
172 
173  CompileError error(
174  __FILE__, __LINE__, __func__,
175  "Problems while writing Binary to file: " +
177 
178  error.setCause(e);
179 
180  throw error;
181  }
182 
183  } catch (CompileError& e) {
184  std::cerr << "Error while compiling file: " << asmFileName << std::endl
185  << e.errorMessage() << std::endl;
186  failure = true;
187 
188  } catch (Exception& e) {
189  std::cerr << "Strange exception while compiling file: " << asmFileName << std::endl
190  << e.errorMessage() << std::endl;
191  failure = true;
192 
193  } catch ( ... ) {
194  std::cerr << "Unknown exception!\n";
195  failure = true;
196  }
197 
198  // back to the nature!
199  delete machine;
200  machine = NULL;
201 
202  if (failure) {
203  return 1;
204  } else {
205  return 0;
206  }
207 }
208 
MachInfoCmdLineOptions::printHelp
virtual void printHelp() const
Definition: MachInfoCmdLineOptions.cc:89
Assembler.hh
FileSystem.hh
ParserStopRequest
Definition: Exception.hh:491
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
outputFileName
static std::string outputFileName(const std::string &adfFile)
Definition: CreateBEM.cc:77
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
Assembler::warnings
const std::set< CompilerMessage > & warnings() const
Definition: Assembler.cc:155
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
TPEF::Binary
Definition: Binary.hh:49
TPEF::BinaryStream
Definition: BinaryStream.hh:59
Exception::setCause
void setCause(const Exception &cause)
Definition: Exception.cc:75
CmdLineOptions.hh
CompileError
Definition: Exception.hh:1019
IllegalCommandLine
Definition: Exception.hh:438
AsmCmdLineOptions
Definition: tceasm.cc:52
AsmCmdLineOptions::printWarnings
bool printWarnings()
Definition: tceasm.cc:72
CompilerMessage
Definition: AssemblyParserDiagnostic.hh:46
AsmCmdLineOptions::printVersion
void printVersion() const
Definition: tceasm.cc:76
CmdLineOptions
Definition: CmdLineOptions.hh:54
ADFSerializer
Definition: ADFSerializer.hh:49
__func__
#define __func__
Definition: Application.hh:67
BoolCmdLineOptionParser
Definition: CmdLineOptionParser.hh:278
Machine.hh
Exception
Definition: Exception.hh:54
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
AsmCmdLineOptions::outputFile
std::string outputFile()
Definition: tceasm.cc:68
CmdLineOptions::parse
void parse(char *argv[], int argc)
Definition: CmdLineOptions.cc:107
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
ADFSerializer.hh
main
int main(int argc, char *argv[])
Definition: tceasm.cc:83
AsmCmdLineOptions::AsmCmdLineOptions
AsmCmdLineOptions()
Definition: tceasm.cc:54
Assembler::compile
TPEF::Binary * compile()
Definition: Assembler.cc:68
BinaryStream.hh
ADFSerializer::readMachine
TTAMachine::Machine * readMachine()
Definition: ADFSerializer.cc:275
TPEFWriter.hh
Assembler
Definition: Assembler.hh:55
TTAMachine
Definition: Assembler.hh:48
FileSystem::fileIsReadable
static bool fileIsReadable(const std::string fileName)
CmdLineParser::argument
virtual std::string argument(int index) const
Application::TCEVersionString
static std::string TCEVersionString()
Definition: Application.cc:510
StringCmdLineOptionParser
Definition: CmdLineOptionParser.hh:180
TPEF
Definition: Assembler.hh:43
TTAMachine::Machine
Definition: Machine.hh:73
Binary.hh