OpenASIP  2.0
Classes | Functions
tcedisasm.cc File Reference
#include <iostream>
#include <fstream>
#include <map>
#include "CmdLineOptions.hh"
#include "BinaryReader.hh"
#include "Binary.hh"
#include "TPEFProgramFactory.hh"
#include "ADFSerializer.hh"
#include "POMDisassembler.hh"
#include "DisassemblyInstruction.hh"
#include "BinaryStream.hh"
#include "DataMemory.hh"
#include "DataDefinition.hh"
#include "FileSystem.hh"
#include "UniversalMachine.hh"
#include "tce_config.h"
#include "Program.hh"
Include dependency graph for tcedisasm.cc:

Go to the source code of this file.

Classes

class  DisasmCmdLineOptions
 

Functions

int main (int argc, char *argv[])
 

Detailed Description

TCE parallel disassembler.

Author
Veli-Pekka Jääskeläinen 2006 (vjaaskel-no.spam-cs.tut.fi)
Pekka Jääskeläinen 2009,2011 (pjaaskel-no.spam-cs.tut.fi)
Note
rating: red

Definition in file tcedisasm.cc.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 118 of file tcedisasm.cc.

118  {
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 }

References TTAProgram::DataMemory::addressSpace(), CmdLineParser::argument(), TTAProgram::TPEFProgramFactory::build(), FileSystem::createFile(), TTAProgram::DataMemory::dataDefinition(), TTAProgram::DataMemory::dataDefinitionCount(), POMDisassembler::disassemble(), Exception::errorMessage(), FileSystem::fileExists(), FileSystem::fileIsCreatable(), FileSystem::fileIsWritable(), UniversalMachine::instance(), TTAProgram::DataDefinition::isInitialized(), TTAProgram::Address::location(), machine, TTAProgram::DataDefinition::MAU(), TTAMachine::Component::name(), CmdLineParser::numberOfArguments(), options, outputFileName(), CmdLineOptions::parse(), MachInfoCmdLineOptions::printHelp(), program, TPEF::BinaryReader::readBinary(), ADFSerializer::readMachine(), XMLSerializer::setSourceFile(), TTAProgram::DataDefinition::size(), TTAProgram::DataDefinition::startAddress(), and POMDisassembler::startAddress().

Here is the call graph for this function:
TTAProgram::Program
Definition: Program.hh:63
MachInfoCmdLineOptions::printHelp
virtual void printHelp() const
Definition: MachInfoCmdLineOptions.cc:89
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
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
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
IllegalCommandLine
Definition: Exception.hh:438
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
TTAProgram::TPEFProgramFactory
Definition: TPEFProgramFactory.hh:87
FileSystem::fileIsWritable
static bool fileIsWritable(const std::string fileName)
ADFSerializer
Definition: ADFSerializer.hh:49
TTAProgram::DataDefinition::isInitialized
virtual bool isInitialized() const
Definition: DataDefinition.cc:144
POMDisassembler
Definition: POMDisassembler.hh:70
TTAProgram::Address::location
InstructionAddress location() const
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
POMDisassembler::disassemble
static std::string disassemble(const TTAProgram::Move &move)
Definition: POMDisassembler.cc:629
FileSystem::fileExists
static bool fileExists(const std::string fileName)
TTAProgram::DataMemory::dataDefinition
DataDefinition & dataDefinition(Address address) const
Definition: DataMemory.cc:79
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::argument
virtual std::string argument(int index) const
TPEF::BinaryReader::readBinary
static Binary * readBinary(BinaryStream &stream)
Definition: BinaryReader.cc:88
TTAMachine::Machine
Definition: Machine.hh:73
TTAProgram::DataDefinition::MAU
virtual MinimumAddressableUnit MAU(int index) const
Definition: DataDefinition.cc:155