OpenASIP  2.0
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 
53 using std::cout;
54 using std::cerr;
55 using std::endl;
56 
57 /**
58  * Commandline options.
59  */
60 class DisasmCmdLineOptions : public CmdLineOptions {
61 public:
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 
104  bool printToStdout() {
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 
118 int main(int argc, char *argv[]) {
119 
121 
122  try {
123  options.parse(argv, argc);
124  } catch (ParserStopRequest) {
125  return 0;
126  } catch (IllegalCommandLine& e) {
127  std::cerr << "Error: Illegal commandline: "
128  << e.errorMessage() << endl << endl;
129 
130  options.printHelp();
131  return 1;
132  }
133 
134  if (options.numberOfArguments() != 2) {
135  std::cerr << "Error: Illegal number of parameters." << endl << endl;
136  options.printHelp();
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 {
170  TTAProgram::TPEFProgramFactory factory(*tpef, *machine);
171  program = factory.build();
172  } catch (Exception& e) {
173 
174  // Try if mixed code
175  try {
177  *tpef, *machine, &UniversalMachine::instance());
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 {
217  *output << POMDisassembler::disassemble(
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() << " "
235  << mem.dataDefinition(0).startAddress().location()
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 
DisasmCmdLineOptions::printToStdout
bool printToStdout()
Definition: tcedisasm.cc:104
TTAProgram::Program
Definition: Program.hh:63
MachInfoCmdLineOptions::printHelp
virtual void printHelp() const
Definition: MachInfoCmdLineOptions.cc:89
BinaryReader.hh
FileSystem.hh
TTAProgram::DataDefinition
Definition: DataDefinition.hh:52
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
TTAProgram::DataDefinition::startAddress
virtual Address startAddress() const
Definition: DataDefinition.cc:129
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
TTAProgram::DataDefinition::size
virtual int size() const
Definition: DataDefinition.cc:211
TTAMachine::AddressSpace
Definition: AddressSpace.hh:51
POMDisassembler::startAddress
virtual Word startAddress() const
Definition: POMDisassembler.cc:483
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
CmdLineOptionParser::isDefined
bool isDefined()
Definition: CmdLineOptionParser.cc:169
TPEF::Binary
Definition: Binary.hh:49
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TTAProgram::DataMemory::addressSpace
const TTAMachine::AddressSpace & addressSpace() const
Definition: DataMemory.cc:182
UniversalMachine::instance
static UniversalMachine & instance()
Definition: UniversalMachine.cc:73
TTAProgram::DataMemory::dataDefinitionCount
int dataDefinitionCount() const
Definition: DataMemory.cc:129
CmdLineOptions.hh
IllegalCommandLine
Definition: Exception.hh:438
CmdLineOptionParser::isFlagOn
virtual bool isFlagOn() const
Definition: CmdLineOptionParser.cc:126
TTAProgram::Program::InstructionVector
std::vector< Instruction * > InstructionVector
Vector for instructions.
Definition: Program.hh:66
FileSystem::fileIsCreatable
static bool fileIsCreatable(const std::string fileName)
Definition: FileSystem.cc:123
POMDisassembler.hh
TTAProgram::TPEFProgramFactory
Definition: TPEFProgramFactory.hh:87
DisasmCmdLineOptions::~DisasmCmdLineOptions
virtual ~DisasmCmdLineOptions()
Definition: tcedisasm.cc:113
UniversalMachine.hh
DisassemblyInstruction.hh
DisasmCmdLineOptions::flatFile
bool flatFile()
Definition: tcedisasm.cc:100
CmdLineParser::addOption
void addOption(CmdLineOptionParser *opt)
FileSystem::fileIsWritable
static bool fileIsWritable(const std::string fileName)
DataMemory.hh
CmdLineOptions
Definition: CmdLineOptions.hh:54
ADFSerializer
Definition: ADFSerializer.hh:49
TTAProgram::DataDefinition::isInitialized
virtual bool isInitialized() const
Definition: DataDefinition.cc:144
POMDisassembler
Definition: POMDisassembler.hh:70
TTAProgram::TPEFProgramFactory::build
Program * build()
Definition: TPEFProgramFactory.cc:200
BoolCmdLineOptionParser
Definition: CmdLineOptionParser.hh:278
TTAProgram::Address::location
InstructionAddress location() const
main
int main(int argc, char *argv[])
Definition: tcedisasm.cc:118
Exception
Definition: Exception.hh:54
FileSystem::createFile
static bool createFile(const std::string &file)
Definition: FileSystem.cc:468
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
CmdLineOptions::parse
void parse(char *argv[], int argc)
Definition: CmdLineOptions.cc:107
DisasmCmdLineOptions
Definition: tpef2pasm.cc:79
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
CmdLineOptionParser::String
virtual std::string String(int index=0) const
Definition: CmdLineOptionParser.cc:102
DisasmCmdLineOptions::lineNumbers
bool lineNumbers()
Definition: tcedisasm.cc:96
DisasmCmdLineOptions::printVersion
void printVersion() const
Definition: tcedisasm.cc:108
POMDisassembler::disassemble
static std::string disassemble(const TTAProgram::Move &move)
Definition: POMDisassembler.cc:629
TPEFProgramFactory.hh
ADFSerializer.hh
Program.hh
DisasmCmdLineOptions::outputFile
std::string outputFile()
Definition: tpef2pasm.cc:94
FileSystem::fileExists
static bool fileExists(const std::string fileName)
TTAProgram::DataMemory::dataDefinition
DataDefinition & dataDefinition(Address address) const
Definition: DataMemory.cc:79
BinaryStream.hh
ADFSerializer::readMachine
TTAMachine::Machine * readMachine()
Definition: ADFSerializer.cc:275
program
find Finds info of the inner loops in the program
Definition: InnerLoopFinder.cc:80
TTAProgram::DataMemory
Definition: DataMemory.hh:56
CmdLineParser::findOption
CmdLineOptionParser * findOption(std::string name) const
Definition: CmdLineParser.cc:160
DisasmCmdLineOptions::DisasmCmdLineOptions
DisasmCmdLineOptions()
Definition: tcedisasm.cc:62
CmdLineParser::argument
virtual std::string argument(int index) const
Application::TCEVersionString
static std::string TCEVersionString()
Definition: Application.cc:510
DataDefinition.hh
DisasmCmdLineOptions::outputFileDefined
bool outputFileDefined()
Definition: tcedisasm.cc:92
StringCmdLineOptionParser
Definition: CmdLineOptionParser.hh:180
TPEF::BinaryReader::readBinary
static Binary * readBinary(BinaryStream &stream)
Definition: BinaryReader.cc:88
TTAMachine::Machine
Definition: Machine.hh:73
Binary.hh
TTAProgram::DataDefinition::MAU
virtual MinimumAddressableUnit MAU(int index) const
Definition: DataDefinition.cc:155