HPCToolkit
Insn.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #ifndef BinUtil_Insn_hpp
61 #define BinUtil_Insn_hpp
62 
63 //************************* System Include Files ****************************
64 
65 #include <iostream>
66 
67 //*************************** User Include Files ****************************
68 
69 #include <include/gcc-attr.h>
70 #include <include/uint.h>
71 
72 #include "LM.hpp"
73 
74 #include <lib/isa/ISA.hpp>
75 
76 //*************************** Forward Declarations **************************
77 
78 //***************************************************************************
79 // Insn
80 //***************************************************************************
81 
82 namespace BinUtil {
83 
84 // 'Insn' is an abstract class that represents one single
85 // processor instruction instruction in the 'TextSection' of a
86 // 'LoadModule'. For the sake of generality, all instructions are
87 // viewed as (potentially) variable sized instruction packets.
88 //
89 // VLIW instructions (or 'packets') that consist of multiple
90 // operations, are "unpacked" or "flattened" into their component
91 // operations; thus each operation is an 'Insn'.
92 
93 class Insn {
94 public:
95  Insn(MachInsn* minsn)
96  : m_minsn(minsn), m_vma(0)
97  { }
98 
99  Insn(MachInsn* minsn, VMA vma)
100  : m_minsn(minsn), m_vma(vma)
101  { }
102 
103  virtual
105  { }
106 
107  // Returns a classification of the instruction
109  { return LM::isa->getInsnDesc(m_minsn, opIndex(), size()); }
110 
111  // 'bits' returns a pointer to the bits of the instruction;
112  // 'size' returns the size of the machine instruction.
113  // In the case of VLIW instructions, returns the {bits, size} not of
114  // the individual operation but the whole "packet".
115  virtual MachInsn*
116  bits() const
117  { return m_minsn; }
118 
119  virtual ushort
120  size() const = 0;
121 
122  // Returns the VMA for the beginning of this instruction.
123  // WARNING: this is unrelocated
124  VMA
125  vma() const
126  { return m_vma; }
127 
128  void
130  { m_vma = vma; }
131 
132  // Returns the end vma, i.e. the vma immediately following this instruction
133  VMA
134  endVMA() const
135  { return vma() + size(); }
136 
137  // Returns the operation VMA for the beginning of this instruction
138  VMA
139  opVMA() const
140  { return LM::isa->convertVMAToOpVMA(vma(), opIndex()); }
141 
142  // Viewing each object code instruction as an instruction packet,
143  // and recalling that all packets are "unpacked" in a 'LM',
144  // 'GetOpIndex' retuns the (0 based) index of this instruction in
145  // the original packet. 'GetNumOps' returns the number of
146  // operations in this packet. For RISC and CISC machines these
147  // values will be 0 and 1, respectively.
148  virtual ushort
149  opIndex() const = 0;
150 
151  virtual ushort
152  numOps() const = 0;
153 
154  // Returns the target address of a jump or branch instruction. If a
155  // target cannot be computed, return 0. Note that a target is not
156  // computed when it depends on values in registers (e.g. indirect
157  // jumps). 'vma' is used only to calculate PC-relative targets.
158  virtual VMA
160  { return LM::isa->getInsnTargetVMA(m_minsn, vma, opIndex(), size()); }
161 
162  // Returns the number of delay slots that must be observed by
163  // schedulers before the effect of instruction 'mi' can be
164  // assumed to be fully obtained (e.g., RISC braches).
165  virtual ushort
167  { return LM::isa->getInsnNumDelaySlots(m_minsn, opIndex(), size()); }
168 
169  // Returns whether or not the instruction "explicitly" executes in
170  // parallel with its successor 'mi2' (successor in the sequential
171  // sense). IOW, this has special reference to "explicitly parallel"
172  // architecture, not superscalar design.
173  virtual bool
175  {
177  x->bits(), x->opIndex(),
178  x->size());
179  }
180 
181  // decode:
182  virtual void
183  decode(std::ostream& os)
184  { return LM::isa->decode(os, m_minsn, vma(), opIndex()); }
185 
186  // -------------------------------------------------------
187  // debugging
188  // -------------------------------------------------------
189  // Current meaning of 'flags'
190  // 0 : short dump (without instructions)
191  // 1 : full dump
192 
193  std::string
194  toString(int flags = LM::DUMP_Short, const char* pre = "") const;
195 
196  virtual void
197  dump(std::ostream& o = std::cerr,
198  int flags = LM::DUMP_Short, const char* pre = "") const;
199 
200  void
201  ddump() const;
202 
203  virtual void
204  dumpme(std::ostream& o = std::cerr, const char* pre = "") const;
205 
206 private:
207  // Should not be used
209  { }
210 
212  { }
213 
214  Insn&
216  { return *this; }
217 
218 protected:
219  MachInsn* m_minsn; // pointer to machine instruction [lives in Section]
220 
221 private:
222  VMA m_vma; // vma of the beginning of this instruction packet
223 };
224 
225 } // namespace BinUtil
226 
227 
228 //***************************************************************************
229 // CISCInsn
230 //***************************************************************************
231 
232 namespace BinUtil {
233 
234 class CISCInsn : public Insn {
235 public:
237  : Insn(minsn, vma), m_size(sz)
238  { }
239 
240  virtual ~CISCInsn() { }
241 
242  virtual ushort
243  size() const
244  { return m_size; }
245 
246  virtual ushort
247  opIndex() const
248  { return 0; }
249 
250  virtual ushort
251  numOps() const
252  { return 1; }
253 
254  // Given a target or branch instruction, returns the target address.
255  virtual VMA
257  { return LM::isa->getInsnTargetVMA(m_minsn, vma, m_size); }
258 
259  virtual ushort
261  { return LM::isa->getInsnNumDelaySlots(m_minsn, m_size); }
262 
263  // -------------------------------------------------------
264  // debugging
265  // -------------------------------------------------------
266  virtual void
267  dump(std::ostream& o = std::cerr,
268  int flags = LM::DUMP_Short, const char* pre = "") const;
269 
270  virtual void
271  dumpme(std::ostream& o = std::cerr, const char* pre = "") const;
272 
273 private:
274  // Should not be used
275  CISCInsn();
276 
278 
279  CISCInsn&
281  { return *this; }
282 
283 protected:
284 private:
286 };
287 
288 } // namespace BinUtil
289 
290 
291 //***************************************************************************
292 // RISCInsn
293 //***************************************************************************
294 
295 namespace BinUtil {
296 
297 class RISCInsn : public Insn {
298 public:
300  : Insn(minsn, vma)
301  { }
302 
303  virtual
305  { }
306 
307  virtual ushort
308  size() const
309  { return LM::isa->getInsnSize(m_minsn); }
310 
311  virtual ushort
312  opIndex() const
313  { return 0; }
314 
315  virtual ushort
316  numOps() const
317  { return 1; }
318 
319  // -------------------------------------------------------
320  // debugging
321  // -------------------------------------------------------
322  virtual void
323  dump(std::ostream& o = std::cerr,
324  int flags = LM::DUMP_Short, const char* pre = "") const;
325 
326  virtual void
327  dumpme(std::ostream& o = std::cerr, const char* pre = "") const;
328 
329 private:
330  // Should not be used
331  RISCInsn();
332 
334 
335  RISCInsn&
337  { return *this; }
338 
339 protected:
340 private:
341 };
342 
343 } // namespace BinUtil
344 
345 
346 //***************************************************************************
347 // VLIWInsn
348 //***************************************************************************
349 
350 namespace BinUtil {
351 
352 class VLIWInsn : public Insn {
353 public:
355  : Insn(minsn, vma), m_opIndex(opIndex)
356  { }
357 
358  virtual ~VLIWInsn()
359  { }
360 
361  virtual ushort
362  size() const
363  { return LM::isa->getInsnSize(m_minsn); }
364 
365  virtual ushort
366  opIndex() const
367  { return m_opIndex; }
368 
369  virtual ushort
370  numOps() const
371  { return LM::isa->getInsnNumOps(m_minsn); }
372 
373  // -------------------------------------------------------
374  // debugging
375  // -------------------------------------------------------
376  virtual void
377  dump(std::ostream& o = std::cerr,
378  int flags = LM::DUMP_Short, const char* pre = "") const;
379 
380  virtual void
381  dumpme(std::ostream& o = std::cerr, const char* pre = "") const;
382 
383 private:
384  // Should not be used
385  VLIWInsn();
386 
388 
389  VLIWInsn&
391  { return *this; }
392 
393 protected:
394 private:
396 };
397 
398 } // namespace BinUtil
399 
400 //***************************************************************************
401 
402 #endif
ISA::InsnDesc desc() const
Definition: Insn.hpp:108
CISCInsn & operator=(const CISCInsn &GCC_ATTR_UNUSED i)
Definition: Insn.hpp:280
virtual VMA convertVMAToOpVMA(VMA vma, ushort GCC_ATTR_UNUSED opIndex) const
Definition: ISA.hpp:471
virtual ushort numOps() const
Definition: Insn.hpp:316
virtual InsnDesc getInsnDesc(MachInsn *mi, ushort opIndex, ushort sz=0)=0
virtual ushort opIndex() const
Definition: Insn.hpp:366
virtual VMA getTargetVMA(VMA vma) const
Definition: Insn.hpp:256
virtual ushort numDelaySlots() const
Definition: Insn.hpp:166
RISCInsn & operator=(const RISCInsn &GCC_ATTR_UNUSED i)
Definition: Insn.hpp:336
VMA endVMA() const
Definition: Insn.hpp:134
bfd_vma VMA
Definition: ISATypes.hpp:79
Insn(MachInsn *minsn, VMA vma)
Definition: Insn.hpp:99
virtual void decode(std::ostream &os, MachInsn *mi, VMA vma, ushort opIndex)=0
virtual ~Insn()
Definition: Insn.hpp:104
Insn & operator=(const Insn &GCC_ATTR_UNUSED i)
Definition: Insn.hpp:215
virtual ushort numOps() const =0
Insn(const Insn &GCC_ATTR_UNUSED i)
Definition: Insn.hpp:211
virtual ~VLIWInsn()
Definition: Insn.hpp:358
ushort m_size
Definition: Insn.hpp:285
virtual ushort opIndex() const =0
virtual VMA getInsnTargetVMA(MachInsn *mi, VMA vma, ushort opIndex, ushort sz=0)=0
virtual ushort opIndex() const
Definition: Insn.hpp:312
virtual ushort size() const
Definition: Insn.hpp:243
virtual ushort getNumDelaySlots() const
Definition: Insn.hpp:260
virtual void decode(std::ostream &os)
Definition: Insn.hpp:183
virtual ushort numOps() const
Definition: Insn.hpp:370
virtual VMA targetVMA(VMA vma) const
Definition: Insn.hpp:159
virtual ushort getInsnNumOps(MachInsn *mi)=0
virtual bool isParallelWithSuccessor(MachInsn *mi1, ushort opIndex1, ushort sz1, MachInsn *mi2, ushort opIndex2, ushort sz2) const =0
unsigned short int ushort
Definition: uint.h:120
virtual MachInsn * bits() const
Definition: Insn.hpp:116
VLIWInsn & operator=(const VLIWInsn &GCC_ATTR_UNUSED i)
Definition: Insn.hpp:390
virtual ushort getInsnNumDelaySlots(MachInsn *mi, ushort opIndex, ushort sz=0)=0
virtual void dump(std::ostream &o=std::cerr, int flags=LM::DUMP_Short, const char *pre="") const
Definition: Insn.cpp:87
virtual ushort size() const =0
virtual ~RISCInsn()
Definition: Insn.hpp:304
void vma(VMA vma)
Definition: Insn.hpp:129
virtual bool isParallelWithSuccessor(Insn *x) const
Definition: Insn.hpp:174
virtual ~CISCInsn()
Definition: Insn.hpp:240
Insn(MachInsn *minsn)
Definition: Insn.hpp:95
ushort m_opIndex
Definition: Insn.hpp:395
virtual ushort getInsnSize(MachInsn *mi)=0
virtual ushort opIndex() const
Definition: Insn.hpp:247
void ddump() const
Definition: Insn.cpp:118
VLIWInsn(MachInsn *minsn, VMA vma, ushort opIndex)
Definition: Insn.hpp:354
VMA vma() const
Definition: Insn.hpp:125
void MachInsn
Definition: ISATypes.hpp:87
VMA opVMA() const
Definition: Insn.hpp:139
std::string toString(int flags=LM::DUMP_Short, const char *pre="") const
Definition: Insn.cpp:109
MachInsn * m_minsn
Definition: Insn.hpp:219
virtual ushort numOps() const
Definition: Insn.hpp:251
CISCInsn(MachInsn *minsn, VMA vma, ushort sz)
Definition: Insn.hpp:236
virtual ushort size() const
Definition: Insn.hpp:308
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80
virtual ushort size() const
Definition: Insn.hpp:362
static ISA * isa
Definition: LM.hpp:493
RISCInsn(MachInsn *minsn, VMA vma)
Definition: Insn.hpp:299
virtual void dumpme(std::ostream &o=std::cerr, const char *pre="") const
Definition: Insn.cpp:125