2 * Copyright (c) 2007 The Hewlett-Packard Development Company
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #ifndef __ARCH_X86_PREDECODER_HH__
41 #define __ARCH_X86_PREDECODER_HH__
45 #include "arch/x86/regs/misc.hh"
46 #include "arch/x86/types.hh"
47 #include "base/bitfield.hh"
48 #include "base/misc.hh"
49 #include "base/trace.hh"
50 #include "base/types.hh"
51 #include "debug/Predecoder.hh"
60 //These are defined and documented in predecoder_tables.cc
61 static const uint8_t Prefixes[256];
62 static const uint8_t UsesModRM[2][256];
63 static const uint8_t ImmediateType[2][256];
64 static const uint8_t SizeTypeToSize[3][10];
68 //The bytes to be predecoded
70 //The pc of the start of fetchChunk
72 //The pc the current instruction started at
74 //The offset into fetchChunk of current processing
76 //The extended machine instruction being generated
80 inline uint8_t getNextByte()
82 return ((uint8_t *)&fetchChunk)[offset];
85 void getImmediate(int &collected, uint64_t ¤t, int size)
87 //Figure out how many bytes we still need to get for the
89 int toGet = size - collected;
90 //Figure out how many bytes are left in our "buffer"
91 int remaining = sizeof(MachInst) - offset;
92 //Get as much as we need, up to the amount available.
93 toGet = toGet > remaining ? remaining : toGet;
95 //Shift the bytes we want to be all the way to the right
96 uint64_t partialImm = fetchChunk >> (offset * 8);
97 //Mask off what we don't want
98 partialImm &= mask(toGet * 8);
99 //Shift it over to overlay with our displacement.
100 partialImm <<= (immediateCollected * 8);
101 //Put it into our displacement
102 current |= partialImm;
103 //Update how many bytes we've collected.
108 inline void consumeByte()
111 assert(offset <= sizeof(MachInst));
112 if(offset == sizeof(MachInst))
116 inline void consumeBytes(int numBytes)
119 assert(offset <= sizeof(MachInst));
120 if(offset == sizeof(MachInst))
126 //State machine state
128 //Whether or not we're out of bytes
130 //Whether we've completed generating an ExtMachInst
132 //The size of the displacement value
133 int displacementSize;
134 //The size of the immediate value
136 //This is how much of any immediate value we've gotten. This is used
137 //for both the actual immediate and the displacement.
138 int immediateCollected;
148 //We should never get to this state. Getting here is an error.
154 //Functions to handle each of the states
155 State doPrefixState(uint8_t);
156 State doOpcodeState(uint8_t);
157 State doModRMState(uint8_t);
158 State doSIBState(uint8_t);
159 State doDisplacementState();
160 State doImmediateState();
163 Predecoder(ThreadContext * _tc) :
164 tc(_tc), basePC(0), origPC(0), offset(0),
165 outOfBytes(true), emiIsReady(false),
168 emi.mode.mode = LongMode;
169 emi.mode.submode = SixtyFourBitMode;
178 ThreadContext * getTC()
183 void setTC(ThreadContext * _tc)
190 //Use this to give data to the predecoder. This should be used
191 //when there is control flow.
192 void moreBytes(const PCState &pc, Addr fetchPC, MachInst data)
194 DPRINTF(Predecoder, "Getting more bytes.\n");
196 offset = (fetchPC >= pc.instAddr()) ? 0 : pc.instAddr() - fetchPC;
207 bool extMachInstReady()
215 int size = basePC + offset - origPC;
217 "Calculating the instruction size: "
218 "basePC: %#x offset: %#x origPC: %#x size: %d\n",
219 basePC, offset, origPC, size);
223 //This returns a constant reference to the ExtMachInst to avoid a copy
225 getExtMachInst(X86ISA::PCState &nextPC)
229 if (!nextPC.size()) {
230 Addr size = getInstSize();
232 nextPC.npc(nextPC.pc() + size);
239 #endif // __ARCH_X86_PREDECODER_HH__