gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
decoder.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Google
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __ARCH_X86_DECODER_HH__
30#define __ARCH_X86_DECODER_HH__
31
32#include <cassert>
33#include <unordered_map>
34#include <vector>
35
38#include "arch/x86/regs/misc.hh"
39#include "arch/x86/types.hh"
40#include "base/bitfield.hh"
41#include "base/logging.hh"
42#include "base/trace.hh"
43#include "base/types.hh"
44#include "cpu/decode_cache.hh"
45#include "cpu/static_inst.hh"
46#include "debug/Decoder.hh"
47#include "params/X86Decoder.hh"
48
49namespace gem5
50{
51
52class BaseISA;
53
54namespace X86ISA
55{
56
57class Decoder : public InstDecoder
58{
59 private:
60 // These are defined and documented in decoder_tables.cc
61 static const uint8_t SizeTypeToSize[3][10];
62 typedef const uint8_t ByteTable[256];
63 static const ByteTable Prefixes[2];
64
69
74
76
77 protected:
78 using MachInst = uint64_t;
79
80 struct InstBytes
81 {
86
88 {}
89
90 void
92 {
93 si = nullptr;
94 chunks.clear();
95 masks.clear();
96 lastOffset = 0;
97 }
98 };
99
100 // The bytes to be predecoded.
104 // The pc of the start of fetchChunk.
106 // The pc the current instruction started at.
108 // The offset into fetchChunk of current processing.
109 int offset = 0;
110 // The extended machine instruction being generated.
112 // Predecoding state.
113 X86Mode mode = LongMode;
115 uint8_t altOp = 0;
116 uint8_t defOp = 0;
117 uint8_t altAddr = 0;
118 uint8_t defAddr = 0;
119 uint8_t stack = 0;
120
121 uint8_t cpl = 0;
122
123 uint8_t
125 {
126 return ((uint8_t *)&fetchChunk)[offset];
127 }
128
129 void
130 getImmediate(int &collected, uint64_t &current, int size)
131 {
132 // Figure out how many bytes we still need to get for the
133 // immediate.
134 int toGet = size - collected;
135 // Figure out how many bytes are left in our "buffer".
136 int remaining = sizeof(MachInst) - offset;
137 // Get as much as we need, up to the amount available.
138 toGet = toGet > remaining ? remaining : toGet;
139
140 // Shift the bytes we want to be all the way to the right
141 uint64_t partialImm = fetchChunk >> (offset * 8);
142 // Mask off what we don't want.
143 partialImm &= mask(toGet * 8);
144 // Shift it over to overlay with our displacement.
145 partialImm <<= (immediateCollected * 8);
146 // Put it into our displacement.
147 current |= partialImm;
148 // Update how many bytes we've collected.
149 collected += toGet;
150 consumeBytes(toGet);
151 }
152
153 void
155 {
156 assert(offset <= sizeof(MachInst));
157 if (offset == sizeof(MachInst)) {
158 DPRINTF(Decoder, "At the end of a chunk, idx = %d, chunks = %d.\n",
159 chunkIdx, instBytes.chunks.size());
160 chunkIdx++;
161 if (chunkIdx == instBytes.chunks.size()) {
162 outOfBytes = true;
163 } else {
164 offset = 0;
165 fetchChunk = instBytes.chunks[chunkIdx];
166 basePC += sizeof(MachInst);
167 }
168 }
169 }
170
171 void
173 {
174 offset++;
176 }
177
178 void
179 consumeBytes(int numBytes)
180 {
181 offset += numBytes;
183 }
184
185 // State machine state.
186 protected:
187 // The size of the displacement value.
189 // The size of the immediate value.
191 // This is how much of any immediate value we've gotten. This is used
192 // for both the actual immediate and the displacement.
194
215
217
218 // Functions to handle each of the states
220 State doPrefixState(uint8_t);
221 State doVex2Of2State(uint8_t);
222 State doVex2Of3State(uint8_t);
223 State doVex3Of3State(uint8_t);
224 State doVexOpcodeState(uint8_t);
229 State doModRMState(uint8_t);
230 State doSIBState(uint8_t);
233
234 // Process the actual opcode found earlier, using the supplied tables.
235 State processOpcode(ByteTable &immTable, ByteTable &modrmTable,
236 bool addrSizedImm = false);
237 // Process the opcode found with VEX / XOP prefix.
239
240 protected:
242
244
246 typedef std::unordered_map<
249
251
256
257 void process();
258
259 public:
260 Decoder(const X86DecoderParams &p) : InstDecoder(p, &fetchChunk)
261 {
262 emi.reset();
263 emi.mode.cpl = cpl;
264 emi.mode.mode = mode;
265 emi.mode.submode = submode;
266 }
267
268 void
269 setM5Reg(HandyM5Reg m5Reg)
270 {
271 cpl = m5Reg.cpl;
272 mode = (X86Mode)(uint64_t)m5Reg.mode;
273 submode = (X86SubMode)(uint64_t)m5Reg.submode;
274 emi.mode.cpl = cpl;
275 emi.mode.mode = mode;
276 emi.mode.submode = submode;
277 altOp = m5Reg.altOp;
278 defOp = m5Reg.defOp;
279 altAddr = m5Reg.altAddr;
280 defAddr = m5Reg.defAddr;
281 stack = m5Reg.stack;
282
283 InstCacheMap::iterator imIter = instCacheMap.find(m5Reg);
284 if (imIter != instCacheMap.end()) {
285 instMap = imIter->second;
286 } else {
288 instCacheMap[m5Reg] = instMap;
289 }
290 }
291
292 void
294 {
296
297 Decoder *dec = dynamic_cast<Decoder *>(old);
298 assert(dec);
299
300 cpl = dec->cpl;
301 mode = dec->mode;
302 submode = dec->submode;
303 emi.mode.cpl = cpl;
304 emi.mode.mode = mode;
305 emi.mode.submode = submode;
306 altOp = dec->altOp;
307 defOp = dec->defOp;
308 altAddr = dec->altAddr;
309 defAddr = dec->defAddr;
310 stack = dec->stack;
311 }
312
313 void
314 reset() override
315 {
318 }
319
320 // Use this to give data to the decoder. This should be used
321 // when there is control flow.
322 void
323 moreBytes(const PCStateBase &pc, Addr fetchPC) override
324 {
325 DPRINTF(Decoder, "Getting more bytes.\n");
326 basePC = fetchPC;
327 offset = (fetchPC >= pc.instAddr()) ? 0 : pc.instAddr() - fetchPC;
329 outOfBytes = false;
330 process();
331 }
332
333 void
335 {
336 if (!nextPC.size()) {
337 int size = basePC + offset - origPC;
339 "Calculating the instruction size: "
340 "basePC: %#x offset: %#x origPC: %#x size: %d\n",
341 basePC, offset, origPC, size);
342 nextPC.size(size);
343 nextPC.npc(nextPC.pc() + size);
344 }
345 }
346
347 public:
348 StaticInstPtr decode(PCStateBase &next_pc) override;
349
351 MicroPC micropc, StaticInstPtr curMacroop) override;
352};
353
354} // namespace X86ISA
355} // namespace gem5
356
357#endif // __ARCH_X86_DECODER_HH__
#define DPRINTF(x,...)
Definition trace.hh:209
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
virtual void takeOverFrom(InstDecoder *old)
Take over the state from an old decoder when switching CPUs.
Definition decoder.hh:89
InstDecoder(const InstDecoderParams &params, MoreBytesType *mb_buf)
Definition decoder.hh:54
virtual void reset()
Definition decoder.hh:63
State doPrefixState(uint8_t)
Definition decoder.cc:141
std::unordered_map< CacheKey, decode_cache::InstMap< ExtMachInst > * > InstCacheMap
Definition decoder.hh:247
StaticInstPtr decode(ExtMachInst mach_inst, Addr addr)
Decode a machine instruction.
Definition decoder.cc:634
static const ByteTable ImmediateTypeTwoByte
Definition decoder.hh:71
State processExtendedOpcode(ByteTable &immTable)
void consumeBytes(int numBytes)
Definition decoder.hh:179
StaticInstPtr fetchRomMicroop(MicroPC micropc, StaticInstPtr curMacroop) override
Definition decoder.cc:697
State doThreeByte0F3AOpcodeState(uint8_t)
Definition decoder.cc:398
static const ByteTable UsesModRMTwoByte
Definition decoder.hh:66
uint8_t getNextByte()
Definition decoder.hh:124
void updateNPC(X86ISA::PCState &nextPC)
Definition decoder.hh:334
State doVex3Of3State(uint8_t)
Definition decoder.cc:278
decode_cache::InstMap< ExtMachInst > * instMap
Definition decoder.hh:245
static const ByteTable UsesModRMThreeByte0F3A
Definition decoder.hh:68
State doVexOpcodeState(uint8_t)
Definition decoder.cc:315
void takeOverFrom(InstDecoder *old) override
Take over the state from an old decoder when switching CPUs.
Definition decoder.hh:293
State doDisplacementState()
Definition decoder.cc:547
RegVal CacheKey
Caching for decoded instruction objects.
Definition decoder.hh:243
const uint8_t ByteTable[256]
Definition decoder.hh:62
State doVex2Of2State(uint8_t)
Definition decoder.cc:207
static const ByteTable ImmediateTypeThreeByte0F38
Definition decoder.hh:72
State doVex2Of3State(uint8_t)
Definition decoder.cc:237
Decoder(const X86DecoderParams &p)
Definition decoder.hh:260
void getImmediate(int &collected, uint64_t &current, int size)
Definition decoder.hh:130
State processOpcode(ByteTable &immTable, ByteTable &modrmTable, bool addrSizedImm=false)
Definition decoder.cc:412
static const ByteTable UsesModRMThreeByte0F38
Definition decoder.hh:67
StaticInstPtr decodeInst(ExtMachInst mach_inst)
State doOneByteOpcodeState(uint8_t)
Definition decoder.cc:339
static const uint8_t SizeTypeToSize[3][10]
Definition decoder.hh:61
static const ByteTable Prefixes[2]
Definition decoder.hh:63
InstCacheMap instCacheMap
Definition decoder.hh:248
State doTwoByteOpcodeState(uint8_t)
Definition decoder.cc:361
X86ISAInst::MicrocodeRom microcodeRom
Definition decoder.hh:75
void setM5Reg(HandyM5Reg m5Reg)
Definition decoder.hh:269
static const ByteTable ImmediateTypeThreeByte0F3A
Definition decoder.hh:73
State doThreeByte0F38OpcodeState(uint8_t)
Definition decoder.cc:384
static const ByteTable ImmediateTypeOneByte
Definition decoder.hh:70
State doSIBState(uint8_t)
Definition decoder.cc:526
State doModRMState(uint8_t)
Definition decoder.cc:472
void reset() override
Definition decoder.hh:314
void moreBytes(const PCStateBase &pc, Addr fetchPC) override
Feed data to the decoder.
Definition decoder.hh:323
static const ByteTable UsesModRMOneByte
Definition decoder.hh:65
uint8_t size() const
Definition pcstate.hh:87
STL vector class.
Definition stl.hh:37
This is exposed globally, independent of the ISA.
Definition tlb.cc:65
Bitfield< 19 > pc
Definition misc.hh:840
Bitfield< 3 > addr
Definition types.hh:84
Bitfield< 0 > p
Definition pagetable.hh:151
@ SixtyFourBitMode
Definition types.hh:204
std::unordered_map< EMI, StaticInstPtr > InstMap
Hash for decoded instructions.
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
T letoh(T value)
Definition byteswap.hh:173
uint64_t RegVal
Definition types.hh:173
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
RefCountingPtr< StaticInst > StaticInstPtr
uint16_t MicroPC
Definition types.hh:149
std::vector< MachInst > chunks
Definition decoder.hh:83
std::vector< MachInst > masks
Definition decoder.hh:84

Generated on Mon May 26 2025 09:18:58 for gem5 by doxygen 1.13.2