OpenASIP  2.0
DisassembleCommand.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 DisassembleCommand.cc
26  *
27  * Implementation of DisassembleCommand class
28  *
29  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
30  * @authoe Henry Linjamäki 2017 (henry.linjamaki-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #include "DisassembleCommand.hh"
35 #include "Application.hh"
36 #include "FileSystem.hh"
37 #include "SimulatorFrontend.hh"
40 #include "Exception.hh"
41 #include "SimulatorToolbox.hh"
43 #include "SimValue.hh"
44 #include "Program.hh"
45 #include "Procedure.hh"
46 #include "Address.hh"
47 #include "Instruction.hh"
48 
49 #include <iostream>
50 
51 using namespace TTAProgram;
52 
53 /**
54  * Constructor.
55  *
56  * Sets the name of the command to the base class.
57  */
59  SimControlLanguageCommand("disassemble") {
60 }
61 
62 /**
63  * Destructor.
64  *
65  * Does nothing.
66  */
68 }
69 
70 /**
71  * Executes the "disassemble" command.
72  *
73  * Prints a range of meory addresses as machine instructions. When two
74  * arguments addr1, addr2 are given, addr1 specifies the first address of the
75  * range to display, and addr2 specifies the last address (not displayed). If
76  * only one argument, addr1, is given, then the function that contains addr1
77  * is disassembled. If no argument is given, the default memory range is the
78  * function surrounding the program counter of the selected frame.
79  *
80  * @param arguments The range of instruction memory to disassemble.
81  * @return Always true if arguments are valid.
82  */
83 bool
84 DisassembleCommand::execute(const std::vector<DataObject>& arguments) {
85  assert(interpreter() != NULL);
86 
87  if (!checkArgumentCount(arguments.size() - 1, 0, 2)) {
88  return false;
89  }
90 
91  if (!checkProgramLoaded()) {
92  return false;
93  }
94 
95  SimulatorInterpreterContext& interpreterContext =
96  dynamic_cast<SimulatorInterpreterContext&>(interpreter()->context());
97 
98  SimulatorFrontend& simFront = interpreterContext.simulatorFrontend();
99 
100  int firstAddress = -1;
101  int lastAddress = -1;
102 
103  const int programLastAddress =
104  simFront.program().lastProcedure().endAddress().location() - 1;
105 
106  if (arguments.size() > 1) {
107  try {
108  firstAddress = parseInstructionAddressExpression(
109  arguments[1].stringValue());
110 
111  if (arguments.size() == 3) {
112  lastAddress = parseInstructionAddressExpression(
113  arguments[2].stringValue());
114  }
115  } catch (const IllegalParameters&) {
116  return false;
117  }
118  }
119 
120  if (firstAddress > programLastAddress) {
121  firstAddress = programLastAddress;
122  }
123 
124 
125  for (auto addr : { firstAddress, lastAddress }) {
126  try {
127  if (addr != -1)
128  simFront.program().instructionAt(addr);
129  } catch (KeyNotFound& e) {
130  interpreter()->setError("No instruction at address "
131  + std::to_string(addr) + ".");
132  return false;
133  }
134  }
135 
136  if (firstAddress == -1 && lastAddress == -1) {
137  firstAddress = simFront.currentProcedure().startAddress().location();
138  lastAddress =
140  } else if (firstAddress != -1 && lastAddress == -1) {
141  const Procedure& procedureAtAddress =
142  dynamic_cast<const Procedure&>(
143  simFront.program().instructionAt(firstAddress).parent());
144  firstAddress = procedureAtAddress.startAddress().location();
145  lastAddress =
146  procedureAtAddress.lastInstruction().address().location();
147  }
148 
149  if (lastAddress < 0) {
150  lastAddress = 0;
151  }
152 
153  if (lastAddress > programLastAddress) {
154  lastAddress = programLastAddress;
155  }
156 
157  if (lastAddress < firstAddress) {
158  lastAddress = firstAddress;
159  }
160 
161  try {
162  for (; firstAddress <= lastAddress; firstAddress +=
163  simFront.program().instructionAt(firstAddress).size()) {
164  outputStream() << simFront.disassembleInstruction(firstAddress)
165  << std::endl;
166  }
167  } catch (KeyNotFound& e) {
168  interpreter()->setError("No instruction at address "
169  + std::to_string(firstAddress) + ".");
170  return false;
171  }
172 
173  return true;
174 }
175 
176 /**
177  * Returns the help text for this command.
178  *
179  * Help text is searched from SimulatorTextGenerator.
180  *
181  * @return The help text.
182  * @todo Use SimulatorTextGenerator to get the help text.
183  */
184 std::string
188 }
TTAProgram
Definition: Estimator.hh:65
SimulatorInterpreterContext
Definition: SimulatorInterpreterContext.hh:45
CustomCommand::checkArgumentCount
bool checkArgumentCount(int argumentCount, int minimum, int maximum)
Definition: CustomCommand.cc:82
SimControlLanguageCommand::outputStream
virtual std::ostream & outputStream()
Definition: SimControlLanguageCommand.cc:351
FileSystem.hh
DisassembleCommand::helpText
virtual std::string helpText() const
Definition: DisassembleCommand.cc:185
SimulatorFrontend::program
const TTAProgram::Program & program() const
Definition: SimulatorFrontend.cc:276
SimulatorInterpreterContext.hh
Exception.hh
Texts::TXT_INTERP_HELP_DISASSEMBLE
@ TXT_INTERP_HELP_DISASSEMBLE
Help text for command "disassemble" of the CLI.
Definition: SimulatorTextGenerator.hh:67
Procedure.hh
SimControlLanguageCommand::checkProgramLoaded
bool checkProgramLoaded()
Definition: SimControlLanguageCommand.cc:180
SimulatorInterpreterContext::simulatorFrontend
SimulatorFrontend & simulatorFrontend()
Definition: SimulatorInterpreterContext.cc:61
TTAProgram::CodeSnippet::startAddress
virtual Address startAddress() const
Definition: CodeSnippet.cc:780
DisassembleCommand::~DisassembleCommand
virtual ~DisassembleCommand()
Definition: DisassembleCommand.cc:67
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
assert
#define assert(condition)
Definition: Application.hh:86
TTAProgram::Program::instructionAt
Instruction & instructionAt(InstructionAddress address) const
Definition: Program.cc:374
SimulatorToolbox.hh
IllegalParameters
Definition: Exception.hh:113
Instruction.hh
SimControlLanguageCommand::parseInstructionAddressExpression
InstructionAddress parseInstructionAddressExpression(const std::string &expression)
Definition: SimControlLanguageCommand.cc:378
Application.hh
SimulatorToolbox::textGenerator
static SimulatorTextGenerator & textGenerator()
Definition: SimulatorToolbox.cc:75
TTAProgram::Instruction::parent
CodeSnippet & parent() const
Definition: Instruction.cc:109
SimulatorTextGenerator.hh
TTAProgram::CodeSnippet::lastInstruction
virtual Instruction & lastInstruction() const
Definition: CodeSnippet.cc:387
TTAProgram::Address::location
InstructionAddress location() const
SimulatorFrontend.hh
CustomCommand::interpreter
ScriptInterpreter * interpreter() const
SimulatorFrontend::currentProcedure
const TTAProgram::Procedure & currentProcedure() const
Definition: SimulatorFrontend.cc:1210
DisassembleCommand.hh
Program.hh
Address.hh
SimValue.hh
TTAProgram::Instruction::size
short size() const
Definition: Instruction.cc:365
SimulatorFrontend::disassembleInstruction
std::string disassembleInstruction(UIntWord instructionAddress) const
Definition: SimulatorFrontend.cc:1073
DisassembleCommand::DisassembleCommand
DisassembleCommand()
Definition: DisassembleCommand.cc:58
TTAProgram::CodeSnippet::endAddress
virtual Address endAddress() const
Definition: CodeSnippet.cc:788
ScriptInterpreter::setError
virtual void setError(bool state)
Definition: ScriptInterpreter.cc:205
KeyNotFound
Definition: Exception.hh:285
TTAProgram::Program::lastProcedure
Procedure & lastProcedure() const
Definition: Program.cc:230
SimControlLanguageCommand
Definition: SimControlLanguageCommand.hh:63
SimControlLanguageCommand.hh
TTAProgram::Procedure
Definition: Procedure.hh:55
SimulatorFrontend
Definition: SimulatorFrontend.hh:89
DisassembleCommand::execute
virtual bool execute(const std::vector< DataObject > &arguments)
Definition: DisassembleCommand.cc:84
ScriptInterpreter::context
virtual InterpreterContext & context() const =0
TTAProgram::Instruction::address
Address address() const
Definition: Instruction.cc:327