HPCToolkit
MipsISA.cpp
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 //************************* System Include Files ****************************
61 
62 #include <iostream>
63 using std::ostream;
64 
65 //*************************** User Include Files ****************************
66 
67 #include <include/uint.h>
68 #include <include/gnu_bfd.h> // for bfd_getb32, bfd_getl32
69 #include <include/gnu_dis-asm.h>
70 
71 #include "MipsISA.hpp"
72 
73 #include <lib/isa-lean/mips/instruction-set.h>
74 
76 
77 //*************************** Forward Declarations ***************************
78 
79 #if defined(HOST_PLATFORM_MIPS64LE_LINUX)
80 # define BFD_GETX32 bfd_getl32
81 # define BFD_PRINT_INSN_MIPS print_insn_little_mips
82 #else /* PLATFORM_MIPS_IRIX64 */
83 # define BFD_GETX32 bfd_getb32
84 # define BFD_PRINT_INSN_MIPS print_insn_big_mips
85 #endif
86 
87 //****************************************************************************
88 
89 static VMA
90 GNUvma2vma(bfd_vma di_vma, MachInsn* insn_addr, VMA insn_vma)
91 {
92  // N.B.: The GNU decoders assume that the address of 'insn_addr' is
93  // the actual the VMA in order to calculate VMA-relative targets.
94  VMA x = (di_vma - PTR_TO_BFDVMA(insn_addr)) + insn_vma;
95  return x;
96 }
97 
98 
99 static void
100 GNUbu_print_addr(bfd_vma di_vma, struct disassemble_info* di)
101 {
102  GNUbu_disdata* data = (GNUbu_disdata*)di->application_data;
103 
104  VMA x = GNUvma2vma(di_vma, data->insn_addr, data->insn_vma);
105  ostream* os = (ostream*)di->stream;
106  *os << std::showbase << std::hex << x << std::dec;
107 }
108 
109 //****************************************************************************
110 // MipsISA
111 //****************************************************************************
112 
113 // See 'ISA.h' for comments on the interface
114 
116  : m_di(NULL), m_di_dis(NULL)
117 {
118  // See 'dis-asm.h'
119  m_di = new disassemble_info;
120  init_disassemble_info(m_di, stdout, GNUbu_fprintf_stub);
121  m_di->arch = bfd_arch_mips; // bfd_get_arch(abfd)
122  m_di->mach = bfd_mach_mipsisa64r2; // bfd_get_mach(abfd)
123 #if defined(HOST_PLATFORM_MIPS64LE_LINUX)
124  m_di->endian = BFD_ENDIAN_LITTLE;
125 #else
126  m_di->endian = BFD_ENDIAN_BIG;
127 #endif
128  m_di->read_memory_func = GNUbu_read_memory; // vs. 'buffer_read_memory'
129  m_di->print_address_func = GNUbu_print_addr_stub; // vs. 'generic_print_addr'
130 
131  m_di_dis = new disassemble_info;
132  init_disassemble_info(m_di_dis, stdout, GNUbu_fprintf);
133  m_di_dis->application_data = (void*)&m_dis_data;
134  m_di_dis->arch = m_di->arch;
135  m_di_dis->mach = m_di->mach;
136  m_di_dis->endian = m_di->endian;
137  m_di_dis->read_memory_func = GNUbu_read_memory;
138  m_di_dis->print_address_func = GNUbu_print_addr;
139 }
140 
141 
143 {
144  delete m_di;
145  delete m_di_dis;
146 }
147 
148 
152 {
153  // We know that instruction sizes are guaranteed to be 4 bytes, but
154  // the host may have a different byte order than the executable.
155  uint32_t insn = (uint32_t)BFD_GETX32((const unsigned char*)mi);
156 
157  switch (insn & MIPS_OPCODE_MASK)
158  {
159  case MIPS_OPClass_Special:
160  switch (insn & MIPS_OPClass_Special_MASK)
161  {
162  case MIPS_OP_JR: // Instructions from Table A-13
163  // JR $31 returns from a JAL call instruction.
164  if (MIPS_OPND_REG_S(insn) == MIPS_REG_RA) {
166  }
167  else {
169  }
170  case MIPS_OP_JALR:
172 
173  case MIPS_OP_SYSCALL: // Instructions from Table A-16,
174  case MIPS_OP_BREAK: // Table A-17
175  case MIPS_OP_TGE: // Trap-on-Condition...
176  case MIPS_OP_TGEU:
177  case MIPS_OP_TLT:
178  case MIPS_OP_TLTU:
179  case MIPS_OP_TEQ:
180  case MIPS_OP_TNE:
182 
183  case MIPS_OP_SYNC:
184  return InsnDesc(InsnDesc::OTHER);
185  }
186  break;
187  case MIPS_OPClass_RegImm:
188  switch (insn & MIPS_OPClass_RegImm_MASK)
189  {
190  case MIPS_OP_BLTZ: // Instructions from Table A-15
191  case MIPS_OP_BGEZ:
192  case MIPS_OP_BLTZL:
193  case MIPS_OP_BGEZL:
195 
196  case MIPS_OP_BLTZAL: // Link
197  case MIPS_OP_BGEZAL: // Link
198  case MIPS_OP_BLTZALL: // Link
199  case MIPS_OP_BGEZALL: // Link
201 
202  case MIPS_OP_TGEI: // Instructions from Table A-18
203  case MIPS_OP_TGEIU: // Trap-on-Condition...
204  case MIPS_OP_TLTI:
205  case MIPS_OP_TLTIU:
206  case MIPS_OP_TEQI:
207  case MIPS_OP_TNEI:
208  return InsnDesc(InsnDesc::OTHER);
209  }
210  break;
211  case MIPS_OPClass_Cop1x:
212  switch (insn & MIPS_OPClass_Cop1x_MASK)
213  {
214  case MIPS_OP_LWXC1: // Word // Instructions from Table A-7
216  case MIPS_OP_LDXC1: // Doubleword
218  case MIPS_OP_SWXC1: // Word
220  case MIPS_OP_SDXC1: // Doubleword
222  }
223  break;
224 
225  // Normal instruction class (OPNormal)
226  case MIPS_OP_LB: // Byte // Instructions from Table A-3
227  case MIPS_OP_LBU: // Byte Unsigned // Table A-4, Table A-5, Table A-6
228  case MIPS_OP_LH: // Halfword
229  case MIPS_OP_LHU: // Halfword Unsigned
230  case MIPS_OP_LW: // Word
231  case MIPS_OP_LWU: // Word Unsigned
232  case MIPS_OP_LWL: // Word Left
233  case MIPS_OP_LWR: // Word Right
234  case MIPS_OP_LL: // Linked Word
235  case MIPS_OP_LWC1: // Word
236  case MIPS_OP_LWC2: // Word
238 
239  case MIPS_OP_LD: // Doubleword
240  case MIPS_OP_LDL: // Doubleword Left
241  case MIPS_OP_LDR: // Doubleword Right
242  case MIPS_OP_LLD: // Linked Doubleword
243  case MIPS_OP_LDC1: // Doubleword
244  case MIPS_OP_LDC2: // Doubleword
245  return InsnDesc(InsnDesc::MEM_LOAD); // Doubleword
246 
247  case MIPS_OP_SB: // Byte
248  case MIPS_OP_SH: // Halfword
249  case MIPS_OP_SW: // Word
250  case MIPS_OP_SWL: // Word Left
251  case MIPS_OP_SWR: // Word Right
252  case MIPS_OP_SC: // Conditional Word
253  case MIPS_OP_SWC1: // Word
254  case MIPS_OP_SWC2: // Word
256 
257  case MIPS_OP_SD: // Doubleword
258  case MIPS_OP_SDL: // Doubleword Left
259  case MIPS_OP_SDR: // Doubleword Right
260  case MIPS_OP_SCD: // Conditional Doubleword
261  case MIPS_OP_SDC1: // Doubleword
262  case MIPS_OP_SDC2: // Doubleword
263  return InsnDesc(InsnDesc::MEM_STORE); // Doubleword
264 
265  // Instructions from Table A-12
266  // N.B.: These instructions are PC-region but we will treat them
267  // as PC-relative
268  case MIPS_OP_J:
270  case MIPS_OP_JAL:
272 
273  case MIPS_OP_BEQ: // Instructions from Table A-14
274  // If operands are the same then then there is no fall-thru branch.
275  // Test for general case.
276  if (MIPS_OPND_REG_S(insn) == MIPS_OPND_REG_T(insn))
278  else
280  case MIPS_OP_BNE:
281  case MIPS_OP_BLEZ:
282  case MIPS_OP_BGTZ:
283  case MIPS_OP_BEQL:
284  case MIPS_OP_BNEL:
285  case MIPS_OP_BLEZL:
286  case MIPS_OP_BGTZL:
288 
289  default:
290  break;
291  }
292 
293  return InsnDesc(InsnDesc::OTHER);
294 }
295 
296 
297 VMA
300 {
301  // We know that instruction sizes are guaranteed to be 4 bytes, but
302  // the host may have a different byte order than the executable.
303  uint32_t insn = (uint32_t)BFD_GETX32((const unsigned char*)mi);
304 
305  intptr_t offset;
306  switch (insn & MIPS_OPCODE_MASK)
307  {
308  case MIPS_OPClass_Special:
309  switch (insn & MIPS_OPClass_Special_MASK)
310  {
311  case MIPS_OP_JR: // Instructions from Table A-13
312  case MIPS_OP_JALR:
313  break; // branch content in register
314  }
315  break;
316  case MIPS_OPClass_RegImm:
317  switch (insn & MIPS_OPClass_RegImm_MASK)
318  {
319  case MIPS_OP_BLTZ: // Instructions from Table A-15
320  case MIPS_OP_BGEZ:
321  case MIPS_OP_BLTZAL:
322  case MIPS_OP_BGEZAL:
323  case MIPS_OP_BLTZL:
324  case MIPS_OP_BGEZL:
325  case MIPS_OP_BLTZALL:
326  case MIPS_OP_BGEZALL:
327  // Added to the address of the instruction *following* the branch
328  offset = MIPS_OPND_IMM(insn);
329  if (offset & MIPS_OPND_IMM_SIGN) {
330  offset |= ~MIPS_OPND_IMM_MASK; // sign extend
331  }
332  return ((pc + MINSN_SIZE) + (offset << 2));
333  }
334  break;
335 
336  // Normal instruction class
337  case MIPS_OP_J: // Instructions from Table A-12
338  case MIPS_OP_JAL:
339  // These are PC-Region and not PC-Relative instructions.
340  // Upper order bits come from the address of the delay-slot instruction
341  offset = MIPS_OPND_INSN_INDEX(insn) << 2;
342  return (((pc + MINSN_SIZE) & MIPS_OPND_INSN_INDEX_UPPER_MASK) | offset);
343 
344  case MIPS_OP_BEQ: // Instructions from Table A-14
345  case MIPS_OP_BNE:
346  case MIPS_OP_BLEZ:
347  case MIPS_OP_BGTZ:
348  case MIPS_OP_BEQL:
349  case MIPS_OP_BNEL:
350  case MIPS_OP_BLEZL:
351  case MIPS_OP_BGTZL:
352  // Added to the address of the instruction *following* the branch
353  offset = MIPS_OPND_IMM(insn);
354  if (offset & MIPS_OPND_IMM_SIGN) {
355  offset |= ~MIPS_OPND_IMM_MASK; // sign extend
356  }
357  return ((pc + MINSN_SIZE) + (offset << 2));
358 
359  default:
360  break;
361  }
362 
363  return (0);
364 }
365 
366 
367 ushort
369 {
370  // All branches have an architectural delay of one
371  // instruction. Treat branch-likely instructions as regular
372  // branches.
373 
374  // (We don't care about delays on instructions such as loads/stores)
375  InsnDesc d = getInsnDesc(mi, opIndex, sz);
376  if (d.isBr() || d.isSubr() || d.isSubrRet()) {
377  return 1;
378  }
379  else {
380  return 0;
381  }
382 }
383 
384 
385 void
386 MipsISA::decode(ostream& os, MachInsn* mi, VMA vma,
387  ushort GCC_ATTR_UNUSED opIndex)
388 {
390  m_dis_data.insn_vma = vma;
391 
392  m_di_dis->stream = (void*)&os;
394 }
395 
396 
397 //****************************************************************************
bfd_vma VMA
Definition: ISATypes.hpp:79
bool isSubr() const
Definition: ISA.hpp:273
#define PTR_TO_BFDVMA(x)
Definition: ISATypes.hpp:94
struct disassemble_info * m_di
Definition: MipsISA.hpp:134
int GNUbu_fprintf(void *stream, const char *format,...)
Definition: ISA.cpp:173
struct disassemble_info * m_di_dis
Definition: MipsISA.hpp:135
int GNUbu_fprintf_stub(void *GCC_ATTR_UNUSED stream, const char *GCC_ATTR_UNUSED format,...)
Definition: ISA.cpp:191
static void GNUbu_print_addr(bfd_vma di_vma, struct disassemble_info *di)
Definition: MipsISA.cpp:100
bool isSubrRet() const
Definition: ISA.hpp:288
virtual ushort getInsnNumDelaySlots(MachInsn *mi, ushort opIndex, ushort sz=0)
Definition: MipsISA.cpp:368
static const ushort MINSN_SIZE
Definition: MipsISA.hpp:133
GNUbu_disdata m_dis_data
Definition: MipsISA.hpp:136
unsigned short int ushort
Definition: uint.h:120
bool isBr() const
Definition: ISA.hpp:231
#define BFD_PRINT_INSN_MIPS
Definition: MipsISA.cpp:84
virtual void decode(std::ostream &os, MachInsn *mi, VMA vma, ushort opIndex)
Definition: MipsISA.cpp:386
virtual InsnDesc getInsnDesc(MachInsn *mi, ushort opIndex, ushort sz=0)
Definition: MipsISA.cpp:150
#define BFD_GETX32
Definition: MipsISA.cpp:83
int GNUbu_read_memory(bfd_vma vma, bfd_byte *myaddr, unsigned int len, struct disassemble_info *GCC_ATTR_UNUSED di)
Definition: ISA.cpp:206
MachInsn * insn_addr
Definition: ISA.hpp:516
void GNUbu_print_addr_stub(bfd_vma GCC_ATTR_UNUSED di_vma, struct disassemble_info *GCC_ATTR_UNUSED di)
Definition: ISA.cpp:199
VMA insn_vma
Definition: ISA.hpp:517
virtual VMA getInsnTargetVMA(MachInsn *mi, VMA pc, ushort opIndex, ushort sz=0)
Definition: MipsISA.cpp:298
void MachInsn
Definition: ISATypes.hpp:87
#define NULL
Definition: ElfHelper.cpp:85
virtual ~MipsISA()
Definition: MipsISA.cpp:142
static VMA GNUvma2vma(bfd_vma di_vma, MachInsn *insn_addr, VMA insn_vma)
Definition: MipsISA.cpp:90
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80
static MachInsn * mi
Definition: x86ISAXed.cpp:91
MipsISA()
Definition: MipsISA.cpp:115