gem5  v22.1.0.0
static_inst.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 The Hewlett-Packard Development Company
3  * Copyright (c) 2013 Advanced Micro Devices, Inc.
4  * All rights reserved.
5  *
6  * The license below extends only to copyright in the software and shall
7  * not be construed as granting a license to any other intellectual
8  * property including but not limited to intellectual property relating
9  * to a hardware implementation of the functionality of the software
10  * licensed hereunder. You may use the software subject to the license
11  * terms below provided that you ensure that this notice is replicated
12  * unmodified and in its entirety in all distributions of the software,
13  * modified or unmodified, in source code or in binary form.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are
17  * met: redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer;
19  * redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution;
22  * neither the name of the copyright holders nor the names of its
23  * contributors may be used to endorse or promote products derived from
24  * this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
40 
41 #include "arch/x86/regs/segment.hh"
42 #include "cpu/reg_class.hh"
43 
44 namespace gem5
45 {
46 
47 namespace X86ISA
48 {
49 
50 void
51 X86StaticInst::printMnemonic(std::ostream &os, const char *mnemonic)
52 {
53  ccprintf(os, " %s ", mnemonic);
54 }
55 
56 void
57 X86StaticInst::printMnemonic(std::ostream &os, const char *instMnemonic,
58  const char *mnemonic)
59 {
60  ccprintf(os, " %s : %s ", instMnemonic, mnemonic);
61 }
62 
63 void X86StaticInst::printSegment(std::ostream &os, int segment)
64 {
65  switch (segment)
66  {
67  case segment_idx::Es:
68  ccprintf(os, "ES");
69  break;
70  case segment_idx::Cs:
71  ccprintf(os, "CS");
72  break;
73  case segment_idx::Ss:
74  ccprintf(os, "SS");
75  break;
76  case segment_idx::Ds:
77  ccprintf(os, "DS");
78  break;
79  case segment_idx::Fs:
80  ccprintf(os, "FS");
81  break;
82  case segment_idx::Gs:
83  ccprintf(os, "GS");
84  break;
85  case segment_idx::Hs:
86  ccprintf(os, "HS");
87  break;
88  case segment_idx::Tsl:
89  ccprintf(os, "TSL");
90  break;
91  case segment_idx::Tsg:
92  ccprintf(os, "TSG");
93  break;
94  case segment_idx::Ls:
95  ccprintf(os, "LS");
96  break;
97  case segment_idx::Ms:
98  ccprintf(os, "MS");
99  break;
100  case segment_idx::Tr:
101  ccprintf(os, "TR");
102  break;
103  case segment_idx::Idtr:
104  ccprintf(os, "IDTR");
105  break;
106  default:
107  panic("Unrecognized segment %d\n", segment);
108  }
109 }
110 
111 void
112 X86StaticInst::divideStep(uint64_t dividend, uint64_t divisor,
113  uint64_t &quotient, uint64_t &remainder)
114 {
115  // Check for divide by zero.
116  assert(divisor != 0);
117  // If the divisor is bigger than the dividend, don't do anything.
118  if (divisor <= dividend) {
119  // Shift the divisor so it's msb lines up with the dividend.
120  int dividendMsb = findMsbSet(dividend);
121  int divisorMsb = findMsbSet(divisor);
122  int shift = dividendMsb - divisorMsb;
123  divisor <<= shift;
124  // Compute what we'll add to the quotient if the divisor isn't
125  // now larger than the dividend.
126  uint64_t quotientBit = 1;
127  quotientBit <<= shift;
128  // If we need to step back a bit (no pun intended) because the
129  // divisor got too to large, do that here. This is the "or two"
130  // part of one or two bit division.
131  if (divisor > dividend) {
132  quotientBit >>= 1;
133  divisor >>= 1;
134  }
135  // Decrement the remainder and increment the quotient.
136  quotient += quotientBit;
137  remainder -= divisor;
138  }
139 }
140 
141 void
142 X86StaticInst::printReg(std::ostream &os, RegId reg, int size)
143 {
144  assert(size == 1 || size == 2 || size == 4 || size == 8);
145  static const char * abcdFormats[9] =
146  {"", "%s", "%sx", "", "e%sx", "", "", "", "r%sx"};
147  static const char * piFormats[9] =
148  {"", "%s", "%s", "", "e%s", "", "", "", "r%s"};
149  static const char * longFormats[9] =
150  {"", "r%sb", "r%sw", "", "r%sd", "", "", "", "r%s"};
151  static const char * microFormats[9] =
152  {"", "t%db", "t%dw", "", "t%dd", "", "", "", "t%d"};
153 
154  RegIndex reg_idx = reg.index();
155 
156  switch (reg.classValue()) {
157  case IntRegClass:
158  {
159  const char * suffix = "";
160  bool fold = reg_idx & IntFoldBit;
161  reg_idx &= ~IntFoldBit;
162 
163  if (fold)
164  suffix = "h";
165  else if (reg_idx < 8 && size == 1)
166  suffix = "l";
167 
168  switch (reg_idx) {
169  case int_reg::Rax:
170  ccprintf(os, abcdFormats[size], "a");
171  break;
172  case int_reg::Rbx:
173  ccprintf(os, abcdFormats[size], "b");
174  break;
175  case int_reg::Rcx:
176  ccprintf(os, abcdFormats[size], "c");
177  break;
178  case int_reg::Rdx:
179  ccprintf(os, abcdFormats[size], "d");
180  break;
181  case int_reg::Rsp:
182  ccprintf(os, piFormats[size], "sp");
183  break;
184  case int_reg::Rbp:
185  ccprintf(os, piFormats[size], "bp");
186  break;
187  case int_reg::Rsi:
188  ccprintf(os, piFormats[size], "si");
189  break;
190  case int_reg::Rdi:
191  ccprintf(os, piFormats[size], "di");
192  break;
193  case int_reg::R8:
194  ccprintf(os, longFormats[size], "8");
195  break;
196  case int_reg::R9:
197  ccprintf(os, longFormats[size], "9");
198  break;
199  case int_reg::R10:
200  ccprintf(os, longFormats[size], "10");
201  break;
202  case int_reg::R11:
203  ccprintf(os, longFormats[size], "11");
204  break;
205  case int_reg::R12:
206  ccprintf(os, longFormats[size], "12");
207  break;
208  case int_reg::R13:
209  ccprintf(os, longFormats[size], "13");
210  break;
211  case int_reg::R14:
212  ccprintf(os, longFormats[size], "14");
213  break;
214  case int_reg::R15:
215  ccprintf(os, longFormats[size], "15");
216  break;
217  default:
218  ccprintf(os, microFormats[size],
219  reg_idx - int_reg::MicroBegin);
220  }
221  ccprintf(os, suffix);
222  }
223  break;
224  case FloatRegClass:
225  if (reg_idx < NumMMXRegs) {
226  ccprintf(os, "%%mmx%d", reg_idx);
227  return;
228  }
229  reg_idx -= NumMMXRegs;
230  if (reg_idx < NumXMMRegs * 2) {
231  ccprintf(os, "%%xmm%d_%s", reg_idx / 2,
232  (reg_idx % 2) ? "high": "low");
233  return;
234  }
235  reg_idx -= NumXMMRegs * 2;
236  if (reg_idx < NumMicroFpRegs) {
237  ccprintf(os, "%%ufp%d", reg_idx);
238  return;
239  }
240  reg_idx -= NumMicroFpRegs;
241  ccprintf(os, "%%st(%d)", reg_idx);
242  break;
243  case CCRegClass:
244  ccprintf(os, "%%cc%d", reg_idx);
245  break;
246  case MiscRegClass:
247  switch (reg_idx) {
248  default:
249  ccprintf(os, "%%ctrl%d", reg_idx);
250  }
251  break;
252  default:
253  panic("Unrecognized register class.");
254  }
255 }
256 
257 void
258 X86StaticInst::printMem(std::ostream &os, uint8_t segment,
259  uint8_t scale, RegIndex index, RegIndex base,
260  uint64_t disp, uint8_t addressSize, bool rip)
261 {
262  bool someAddr = false;
263  printSegment(os, segment);
264  os << ":[";
265  if (rip) {
266  os << "rip";
267  someAddr = true;
268  } else {
269  if (scale != 0 && index != int_reg::NumRegs) {
270  if (scale != 1)
271  ccprintf(os, "%d*", scale);
272  printReg(os, intRegClass[index], addressSize);
273  someAddr = true;
274  }
275  if (base != int_reg::NumRegs) {
276  if (someAddr)
277  os << " + ";
278  printReg(os, intRegClass[base], addressSize);
279  someAddr = true;
280  }
281  }
282  if (disp != 0) {
283  if (someAddr)
284  os << " + ";
285  ccprintf(os, "%#x", disp);
286  someAddr = true;
287  }
288  if (!someAddr)
289  os << "0";
290  os << "]";
291 }
292 
293 std::string
295  Addr pc, const loader::SymbolTable *symtab) const
296 {
297  std::stringstream ss;
298 
300 
301  return ss.str();
302 }
303 
304 } // namespace X86ISA
305 } // namespace gem5
Register ID: describe an architectural register with its class and index.
Definition: reg_class.hh:91
const char * mnemonic
Base mnemonic (e.g., "add").
Definition: static_inst.hh:259
std::string generateDisassembly(Addr pc, const loader::SymbolTable *symtab) const override
Internal function to generate disassembly string.
Definition: static_inst.cc:294
static void printMem(std::ostream &os, uint8_t segment, uint8_t scale, RegIndex index, RegIndex base, uint64_t disp, uint8_t addressSize, bool rip)
Definition: static_inst.cc:258
static void printSegment(std::ostream &os, int segment)
Definition: static_inst.cc:63
static void printMnemonic(std::ostream &os, const char *mnemonic)
Definition: static_inst.cc:51
static void divideStep(uint64_t divident, uint64_t divisor, uint64_t &quotient, uint64_t &remainder)
Definition: static_inst.cc:112
static void printReg(std::ostream &os, RegId reg, int size)
Definition: static_inst.cc:142
constexpr int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
Definition: bitfield.hh:260
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
Bitfield< 6, 5 > shift
Definition: types.hh:117
constexpr RegId R12
Definition: int.hh:144
constexpr RegId R9
Definition: int.hh:141
constexpr RegId R8
Definition: int.hh:140
constexpr RegId R14
Definition: int.hh:146
constexpr RegId Rbx
Definition: int.hh:135
constexpr RegId Rsi
Definition: int.hh:138
constexpr RegId R15
Definition: int.hh:147
constexpr RegId Rax
Definition: int.hh:132
constexpr RegId Rdx
Definition: int.hh:134
constexpr RegId Rsp
Definition: int.hh:136
constexpr RegId Rdi
Definition: int.hh:139
constexpr RegId R13
Definition: int.hh:145
constexpr RegId Rbp
Definition: int.hh:137
constexpr RegId R11
Definition: int.hh:143
constexpr RegId R10
Definition: int.hh:142
constexpr RegId Rcx
Definition: int.hh:133
Bitfield< 19 > pc
Definition: misc.hh:812
Bitfield< 5, 3 > reg
Definition: types.hh:92
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
Bitfield< 17 > os
Definition: misc.hh:810
const int NumXMMRegs
Definition: x86_traits.hh:53
constexpr RegClass intRegClass
Definition: int.hh:123
Bitfield< 5, 3 > index
Definition: types.hh:98
constexpr RegIndex IntFoldBit
Definition: int.hh:178
const int NumMMXRegs
Definition: x86_traits.hh:52
const int NumMicroFpRegs
Definition: x86_traits.hh:54
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint16_t RegIndex
Definition: types.hh:176
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
@ FloatRegClass
Floating-point register.
Definition: reg_class.hh:61
@ CCRegClass
Condition-code register.
Definition: reg_class.hh:67
@ IntRegClass
Integer register.
Definition: reg_class.hh:60
@ MiscRegClass
Control (misc) register.
Definition: reg_class.hh:68
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
return remainder
Definition: scfx_rep.cc:2169
std::stringstream ss
Definition: trace.test.cc:45

Generated on Wed Dec 21 2022 10:22:26 for gem5 by doxygen 1.9.1