OpenASIP 2.2
Loading...
Searching...
No Matches
tcedisasm.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 tcedisasm.cc
26 *
27 * TCE parallel disassembler.
28 *
29 * @author Veli-Pekka Jääskeläinen 2006 (vjaaskel-no.spam-cs.tut.fi)
30 * @author Pekka Jääskeläinen 2009,2011 (pjaaskel-no.spam-cs.tut.fi)
31 * @note rating: red
32 */
33
34#include <iostream>
35#include <fstream>
36#include <map>
37#include "CmdLineOptions.hh"
38#include "BinaryReader.hh"
39#include "Binary.hh"
40#include "TPEFProgramFactory.hh"
41#include "ADFSerializer.hh"
42#include "POMDisassembler.hh"
44#include "BinaryStream.hh"
45#include "BinaryReader.hh"
46#include "DataMemory.hh"
47#include "DataDefinition.hh"
48#include "FileSystem.hh"
49#include "UniversalMachine.hh"
50#include "tce_config.h"
51#include "Program.hh"
52
53using std::cout;
54using std::cerr;
55using std::endl;
56
57/**
58 * Commandline options.
59 */
61public:
63 CmdLineOptions("Usage: tcedisasm [options] adffile tpeffile") {
64
67 "outputfile", "Name of the output file.", "o");
68
71 "linenumbers", "Print line numbers","n");
72
75 "flat", "The instructions only with file line numbers "
76 "matching instruction indices","F");
77
78 BoolCmdLineOptionParser* toStdout =
80 "stdout", "Print to standard output","s");
81
85 addOption(toStdout);
86 }
87
88 std::string outputFile() {
89 return findOption("outputfile")->String();
90 }
91
93 return findOption("outputfile")->isDefined();
94 }
95
96 bool lineNumbers() {
97 return findOption("linenumbers")->isFlagOn();
98 }
99
100 bool flatFile() {
101 return findOption("flat")->isFlagOn();
102 }
103
105 return findOption("stdout")->isFlagOn();
106 }
107
108 void printVersion() const {
109 std::cout << "tcedisasm - OpenASIP TTA parallel disassembler "
110 << Application::TCEVersionString() << std::endl;
111 }
112
114 }
115};
116
117
118int main(int argc, char *argv[]) {
119
121
122 try {
123 options.parse(argv, argc);
124 } catch (ParserStopRequest const&) {
125 return 0;
126 } catch (IllegalCommandLine& e) {
127 std::cerr << "Error: Illegal commandline: "
128 << e.errorMessage() << endl << endl;
129
131 return 1;
132 }
133
134 if (options.numberOfArguments() != 2) {
135 std::cerr << "Error: Illegal number of parameters." << endl << endl;
137 return 1;
138 }
139
140 std::string inputFileName = options.argument(2);
141 // Load TPEF.
142 TPEF::BinaryStream stream(inputFileName);
143 TPEF::Binary* tpef = NULL;
144 try {
145 tpef = TPEF::BinaryReader::readBinary(stream);
146 } catch (Exception& e) {
147 cerr << "Can't read TPEF binary file: " << options.argument(2) << endl
148 << e.errorMessage() << endl;
149 return 1;
150 }
151
152 // Load machine.
154 ADFSerializer machineReader;
155 machineReader.setSourceFile(options.argument(1));
156 try {
157 machine = machineReader.readMachine();
158 } catch (Exception& e) {
159 cerr << "Error: " << options.argument(1) << " is not a valid ADF File"
160 << endl << e.errorMessage() << endl;
161
162 delete tpef;
163 return 1;
164 }
165
166 // Create POM.
168
169 try {
171 program = factory.build();
172 } catch (Exception& e) {
173
174 // Try if mixed code
175 try {
178 program = factory.build();
179 } catch (Exception& e1) {
180 cerr << "Error: " << e.errorMessage() << " and "
181 << e1.errorMessage() << endl;
182 delete tpef;
183 delete machine;
184 return 1;
185 }
186 }
187
188 std::ostream* output = &std::cout;
189 std::fstream file;
190 std::string outputFileName =
191 options.outputFileDefined() ?
192 options.outputFile() : inputFileName + ".S";
193 if (!options.printToStdout()) {
197 file.open(
198 outputFileName.c_str(),
199 std::fstream::trunc | std::fstream::out);
200 output = &file;
202 file.open(
203 outputFileName.c_str(),
204 std::fstream::trunc | std::fstream::out);
205 if (file.is_open()) {
206 output = &file;
207 }
208 }
209 }
210
211 // Write code section disassembly.
212 POMDisassembler disassembler(*program);
213 Word first = disassembler.startAddress();
214 if (!options.flatFile()) {
215 *output << "CODE " << first << " ;" << endl << endl;
216 try {
218 *program, options.lineNumbers())
219 << endl << endl;
220
221 } catch (Exception& e) {
222 std::cerr << "Disassebly failed because of exception: " <<
223 e.errorMessage() << std::endl;
224 }
225
226 // Write data memory initializations.
227 for (int i = 0; i < program->dataMemoryCount(); i++) {
228
229 const TTAProgram::DataMemory& mem = program->dataMemory(i);
230 const TTAMachine::AddressSpace& aSpace = mem.addressSpace();
231
232 if (mem.dataDefinitionCount() == 0) continue;
233
234 *output << "DATA " << aSpace.name() << " "
236 << " ;" << endl;
237
238 // Definitions are put in a map to order them.
239 // TODO: the indexing API of DataMemory could be used for this?
240 // TODO: does this handle "holes" correctly or assume fully
241 // "connected regions" always?
242 // TODO: this does not handle UDATA at all!
243 std::map<Word, const TTAProgram::DataDefinition*> definitions;
244 for (int d = 0; d < mem.dataDefinitionCount(); d++) {
245 const TTAProgram::DataDefinition& def = mem.dataDefinition(d);
246 definitions[def.startAddress().location()] = &def;
247 }
248
249 std::map<Word, const TTAProgram::DataDefinition*>::iterator iter =
250 definitions.begin();
251
252 for (; iter != definitions.end(); iter++) {
253 const TTAProgram::DataDefinition* def = (*iter).second;
254 *output << endl << "DA " << std::dec << def->size();
255 if (def->isInitialized()) {
256 for (int mau = 0; mau < def->size(); mau++) {
257 *output << endl << "1:0x" << std::hex << def->MAU(mau);
258 }
259 }
260 *output << " ;" << endl;
261 }
262 }
263 *output << endl;
264 } else {
265 // assumes instruction addresses always start from 0
267 program->instructionVector();
268 for (TTAProgram::Program::InstructionVector::const_iterator i =
269 instr.begin(); i != instr.end(); ++i) {
270 *output
271 << POMDisassembler::disassemble(**i, options.lineNumbers())
272 << std::endl;
273 }
274 }
275
276 return 0;
277}
278
static std::string outputFileName(const std::string &adfFile)
Definition CreateBEM.cc:77
TTAMachine::Machine * machine
the architecture definition of the estimated processor
find Finds info of the inner loops in the program
static MachInfoCmdLineOptions options
Definition MachInfo.cc:46
TTAMachine::Machine * readMachine()
static std::string TCEVersionString()
virtual bool isFlagOn() 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)
void printVersion() const
Definition tcedisasm.cc:108
virtual ~DisasmCmdLineOptions()
Definition tcedisasm.cc:113
std::string outputFile()
Definition tpef2pasm.cc:94
std::string errorMessage() const
Definition Exception.cc:123
static bool createFile(const std::string &file)
static bool fileIsWritable(const std::string fileName)
static bool fileIsCreatable(const std::string fileName)
static bool fileExists(const std::string fileName)
virtual void printHelp() const
static std::string disassemble(const TTAProgram::Move &move)
virtual Word startAddress() const
static Binary * readBinary(BinaryStream &stream)
virtual TCEString name() const
InstructionAddress location() const
virtual Address startAddress() const
virtual bool isInitialized() const
virtual MinimumAddressableUnit MAU(int index) const
DataDefinition & dataDefinition(Address address) const
Definition DataMemory.cc:79
int dataDefinitionCount() const
const TTAMachine::AddressSpace & addressSpace() const
std::vector< Instruction * > InstructionVector
Vector for instructions.
Definition Program.hh:66
static UniversalMachine & instance()
void setSourceFile(const std::string &fileName)
int main(int argc, char *argv[])
Definition tcedisasm.cc:118