OpenASIP 2.2
Loading...
Searching...
No Matches
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
51using namespace TTAProgram;
52
53/**
54 * Constructor.
55 *
56 * Sets the name of the command to the base class.
57 */
61
62/**
63 * Destructor.
64 *
65 * Does nothing.
66 */
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 */
83bool
84DisassembleCommand::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 =
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 {
109 arguments[1].stringValue());
110
111 if (arguments.size() == 3) {
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 */
184std::string
#define assert(condition)
bool checkArgumentCount(int argumentCount, int minimum, int maximum)
ScriptInterpreter * interpreter() const
virtual std::string helpText() const
virtual bool execute(const std::vector< DataObject > &arguments)
virtual InterpreterContext & context() const =0
virtual void setError(bool state)
InstructionAddress parseInstructionAddressExpression(const std::string &expression)
virtual std::ostream & outputStream()
const TTAProgram::Procedure & currentProcedure() const
std::string disassembleInstruction(UIntWord instructionAddress) const
const TTAProgram::Program & program() const
static SimulatorTextGenerator & textGenerator()
InstructionAddress location() const
virtual Address endAddress() const
virtual Address startAddress() const
virtual Instruction & lastInstruction() const
Address address() const
CodeSnippet & parent() const
Instruction & instructionAt(InstructionAddress address) const
Definition Program.cc:374
Procedure & lastProcedure() const
Definition Program.cc:230
virtual boost::format text(int textId)
@ TXT_INTERP_HELP_DISASSEMBLE
Help text for command "disassemble" of the CLI.