OpenASIP 2.2
Loading...
Searching...
No Matches
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
46using namespace TTAMachine;
47using namespace TPEF;
48
49/**
50 * Commandline options.
51 */
53public:
55 CmdLineOptions("Usage: tceasm [options] adffile assemblerfile") {
56
58 new StringCmdLineOptionParser("outputfile", "Name of the output file.", "o");
59
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
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
83int 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";
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";
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
#define __func__
static std::string outputFileName(const std::string &adfFile)
Definition CreateBEM.cc:77
TTAMachine::Machine * machine
the architecture definition of the estimated processor
static MachInfoCmdLineOptions options
Definition MachInfo.cc:46
TTAMachine::Machine * readMachine()
static std::string TCEVersionString()
void printVersion() const
Definition tceasm.cc:76
bool printWarnings()
Definition tceasm.cc:72
std::string outputFile()
Definition tceasm.cc:68
TPEF::Binary * compile()
Definition Assembler.cc:68
const std::set< CompilerMessage > & warnings() const
Definition Assembler.cc:155
virtual bool isFlagOff() const
virtual std::string String(int index=0) const
void parse(char *argv[], int argc)
CmdLineOptionParser * findOption(std::string name) const
virtual std::string argument(int index) const
virtual int numberOfArguments() const
void addOption(CmdLineOptionParser *opt)
std::string errorMessage() const
Definition Exception.cc:123
void setCause(const Exception &cause)
Definition Exception.cc:75
static bool fileIsReadable(const std::string fileName)
virtual void printHelp() const
void writeBinary(BinaryStream &stream, const Binary *bin) const
static const BinaryWriter & instance()
Definition TPEFWriter.cc:70
void setSourceFile(const std::string &fileName)
int main(int argc, char *argv[])
Definition tceasm.cc:83