X86: Add a .serializing directive that makes a macroop serializing.
[gem5.git] / src / arch / x86 / predecoder.hh
1 /*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
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.
13 *
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.
24 *
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.
36 *
37 * Authors: Gabe Black
38 */
39
40 #ifndef __ARCH_X86_PREDECODER_HH__
41 #define __ARCH_X86_PREDECODER_HH__
42
43 #include <cassert>
44
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"
51
52 class ThreadContext;
53
54 namespace X86ISA
55 {
56 class Predecoder
57 {
58 private:
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];
64
65 protected:
66 ThreadContext * tc;
67 //The bytes to be predecoded
68 MachInst fetchChunk;
69 //The pc of the start of fetchChunk
70 Addr basePC;
71 //The pc the current instruction started at
72 Addr origPC;
73 //The offset into fetchChunk of current processing
74 int offset;
75 //The extended machine instruction being generated
76 ExtMachInst emi;
77 HandyM5Reg m5Reg;
78
79 inline uint8_t getNextByte()
80 {
81 return ((uint8_t *)&fetchChunk)[offset];
82 }
83
84 void getImmediate(int &collected, uint64_t &current, int size)
85 {
86 //Figure out how many bytes we still need to get for the
87 //immediate.
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;
93
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.
103 collected += toGet;
104 consumeBytes(toGet);
105 }
106
107 inline void consumeByte()
108 {
109 offset++;
110 assert(offset <= sizeof(MachInst));
111 if(offset == sizeof(MachInst))
112 outOfBytes = true;
113 }
114
115 inline void consumeBytes(int numBytes)
116 {
117 offset += numBytes;
118 assert(offset <= sizeof(MachInst));
119 if(offset == sizeof(MachInst))
120 outOfBytes = true;
121 }
122
123 void doReset();
124
125 //State machine state
126 protected:
127 //Whether or not we're out of bytes
128 bool outOfBytes;
129 //Whether we've completed generating an ExtMachInst
130 bool emiIsReady;
131 //The size of the displacement value
132 int displacementSize;
133 //The size of the immediate value
134 int immediateSize;
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;
138
139 enum State {
140 ResetState,
141 PrefixState,
142 OpcodeState,
143 ModRMState,
144 SIBState,
145 DisplacementState,
146 ImmediateState,
147 //We should never get to this state. Getting here is an error.
148 ErrorState
149 };
150
151 State state;
152
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();
160
161 public:
162 Predecoder(ThreadContext * _tc) :
163 tc(_tc), basePC(0), origPC(0), offset(0),
164 outOfBytes(true), emiIsReady(false),
165 state(ResetState)
166 {
167 emi.mode.mode = LongMode;
168 emi.mode.submode = SixtyFourBitMode;
169 m5Reg = 0;
170 }
171
172 void reset()
173 {
174 state = ResetState;
175 }
176
177 ThreadContext * getTC()
178 {
179 return tc;
180 }
181
182 void setTC(ThreadContext * _tc)
183 {
184 tc = _tc;
185 }
186
187 void process();
188
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)
192 {
193 DPRINTF(Predecoder, "Getting more bytes.\n");
194 basePC = fetchPC;
195 offset = (fetchPC >= pc) ? 0 : pc - fetchPC;
196 fetchChunk = data;
197 outOfBytes = false;
198 process();
199 }
200
201 bool needMoreBytes()
202 {
203 return outOfBytes;
204 }
205
206 bool extMachInstReady()
207 {
208 return emiIsReady;
209 }
210
211 //This returns a constant reference to the ExtMachInst to avoid a copy
212 const ExtMachInst & getExtMachInst()
213 {
214 assert(emiIsReady);
215 emiIsReady = false;
216 return emi;
217 }
218
219 int getInstSize()
220 {
221 DPRINTF(Predecoder,
222 "Calculating the instruction size: "
223 "basePC: %#x offset: %#x origPC: %#x\n",
224 basePC, offset, origPC);
225 return basePC + offset - origPC;
226 }
227 };
228 };
229
230 #endif // __ARCH_X86_PREDECODER_HH__