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/types.hh"
46 #include "arch/x86/miscregs.hh"
47 #include "base/bitfield.hh"
48 #include "base/misc.hh"
49 #include "base/trace.hh"
50 #include "base/types.hh"
59 //These are defined and documented in predecoder_tables.cc
60 static const uint8_t Prefixes[256];
61 static const uint8_t UsesModRM[2][256];
62 static const uint8_t ImmediateType[2][256];
63 static const uint8_t SizeTypeToSize[3][10];
67 //The bytes to be predecoded
69 //The pc of the start of fetchChunk
71 //The pc the current instruction started at
73 //The offset into fetchChunk of current processing
75 //The extended machine instruction being generated
79 inline uint8_t getNextByte()
81 return ((uint8_t *)&fetchChunk)[offset];
84 void getImmediate(int &collected, uint64_t ¤t, int size)
86 //Figure out how many bytes we still need to get for the
88 int toGet = size - collected;
89 //Figure out how many bytes are left in our "buffer"
90 int remaining = sizeof(MachInst) - offset;
91 //Get as much as we need, up to the amount available.
92 toGet = toGet > remaining ? remaining : toGet;
94 //Shift the bytes we want to be all the way to the right
95 uint64_t partialImm = fetchChunk >> (offset * 8);
96 //Mask off what we don't want
97 partialImm &= mask(toGet * 8);
98 //Shift it over to overlay with our displacement.
99 partialImm <<= (immediateCollected * 8);
100 //Put it into our displacement
101 current |= partialImm;
102 //Update how many bytes we've collected.
107 inline void consumeByte()
110 assert(offset <= sizeof(MachInst));
111 if(offset == sizeof(MachInst))
115 inline void consumeBytes(int numBytes)
118 assert(offset <= sizeof(MachInst));
119 if(offset == sizeof(MachInst))
125 //State machine state
127 //Whether or not we're out of bytes
129 //Whether we've completed generating an ExtMachInst
131 //The size of the displacement value
132 int displacementSize;
133 //The size of the immediate value
135 //This is how much of any immediate value we've gotten. This is used
136 //for both the actual immediate and the displacement.
137 int immediateCollected;
147 //We should never get to this state. Getting here is an error.
153 //Functions to handle each of the states
154 State doPrefixState(uint8_t);
155 State doOpcodeState(uint8_t);
156 State doModRMState(uint8_t);
157 State doSIBState(uint8_t);
158 State doDisplacementState();
159 State doImmediateState();
162 Predecoder(ThreadContext * _tc) :
163 tc(_tc), basePC(0), origPC(0), offset(0),
164 outOfBytes(true), emiIsReady(false),
167 emi.mode.mode = LongMode;
168 emi.mode.submode = SixtyFourBitMode;
177 ThreadContext * getTC()
182 void setTC(ThreadContext * _tc)
189 //Use this to give data to the predecoder. This should be used
190 //when there is control flow.
191 void moreBytes(Addr pc, Addr fetchPC, MachInst data)
193 DPRINTF(Predecoder, "Getting more bytes.\n");
195 offset = (fetchPC >= pc) ? 0 : pc - fetchPC;
206 bool extMachInstReady()
211 //This returns a constant reference to the ExtMachInst to avoid a copy
212 const ExtMachInst & getExtMachInst()
222 "Calculating the instruction size: "
223 "basePC: %#x offset: %#x origPC: %#x\n",
224 basePC, offset, origPC);
225 return basePC + offset - origPC;
230 #endif // __ARCH_X86_PREDECODER_HH__