Merge zizzer.eecs.umich.edu:/bk/newmem
[gem5.git] / src / arch / x86 / predecoder.hh
1 /*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * Redistribution and use of this software in source and binary forms,
6 * with or without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * The software must be used only for Non-Commercial Use which means any
10 * use which is NOT directed to receiving any direct monetary
11 * compensation for, or commercial advantage from such use. Illustrative
12 * examples of non-commercial use are academic research, personal study,
13 * teaching, education and corporate research & development.
14 * Illustrative examples of commercial use are distributing products for
15 * commercial advantage and providing services using the software for
16 * commercial advantage.
17 *
18 * If you wish to use this software or functionality therein that may be
19 * covered by patents for commercial use, please contact:
20 * Director of Intellectual Property Licensing
21 * Office of Strategy and Technology
22 * Hewlett-Packard Company
23 * 1501 Page Mill Road
24 * Palo Alto, California 94304
25 *
26 * Redistributions of source code must retain the above copyright notice,
27 * this list of conditions and the following disclaimer. Redistributions
28 * in binary form must reproduce the above copyright notice, this list of
29 * conditions and the following disclaimer in the documentation and/or
30 * other materials provided with the distribution. Neither the name of
31 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
32 * contributors may be used to endorse or promote products derived from
33 * this software without specific prior written permission. No right of
34 * sublicense is granted herewith. Derivatives of the software and
35 * output created using the software may be prepared, but only for
36 * Non-Commercial Uses. Derivatives of the software may be shared with
37 * others provided: (i) the others agree to abide by the list of
38 * conditions herein which includes the Non-Commercial Use restrictions;
39 * and (ii) such Derivatives of the software include the above copyright
40 * notice to acknowledge the contribution from this software where
41 * applicable, this list of conditions and the disclaimer below.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 *
55 * Authors: Gabe Black
56 */
57
58 #ifndef __ARCH_X86_PREDECODER_HH__
59 #define __ARCH_X86_PREDECODER_HH__
60
61 #include "arch/x86/types.hh"
62 #include "base/bitfield.hh"
63 #include "base/misc.hh"
64 #include "base/trace.hh"
65 #include "sim/host.hh"
66
67 class ThreadContext;
68
69 namespace X86ISA
70 {
71 class Predecoder
72 {
73 private:
74 //These are defined and documented in predecoder_tables.cc
75 static const uint8_t Prefixes[256];
76 static const uint8_t UsesModRM[2][256];
77 static const uint8_t ImmediateType[2][256];
78 static const uint8_t ImmediateTypeToSize[3][10];
79
80 protected:
81 ThreadContext * tc;
82 //The bytes to be predecoded
83 MachInst fetchChunk;
84 //The pc of the start of fetchChunk
85 Addr basePC;
86 //The offset into fetchChunk of current processing
87 int offset;
88 //The extended machine instruction being generated
89 ExtMachInst emi;
90
91 //State machine state
92 protected:
93 //Whether or not we're out of bytes
94 bool outOfBytes;
95 //Whether we've completed generating an ExtMachInst
96 bool emiIsReady;
97 //The size of the displacement value
98 int displacementSize;
99 int displacementCollected;
100 //The size of the immediate value
101 int immediateSize;
102 int immediateCollected;
103
104 //These are local to some of the states. I need to turn the states
105 //into inline functions to clean things up a bit.
106 int toGet;
107 int remaining;
108 MachInst partialDisp;
109
110 enum State {
111 Prefix,
112 Opcode,
113 ModRM,
114 SIB,
115 Displacement,
116 Immediate
117 };
118
119 State state;
120
121 public:
122 Predecoder(ThreadContext * _tc) :
123 tc(_tc), basePC(0), offset(0),
124 outOfBytes(true), emiIsReady(false),
125 state(Prefix)
126 {}
127
128 ThreadContext * getTC()
129 {
130 return tc;
131 }
132
133 void setTC(ThreadContext * _tc)
134 {
135 tc = _tc;
136 }
137
138 void process()
139 {
140 assert(!outOfBytes);
141 assert(!emiIsReady);
142 while(!emiIsReady && !outOfBytes)
143 {
144 uint8_t nextByte = (fetchChunk >> (offset * 8)) & 0xff;
145 switch(state)
146 {
147 case Prefix:
148 uint8_t prefix = Prefixes[nextByte];
149 if(prefix)
150 offset++;
151 switch(prefix)
152 {
153 //Operand size override prefixes
154 case OperandSizeOverride:
155 DPRINTF(Predecoder, "Found operand size override prefix.\n");
156 break;
157 case AddressSizeOverride:
158 DPRINTF(Predecoder, "Found address size override prefix.\n");
159 break;
160 //Segment override prefixes
161 case CSOverride:
162 DPRINTF(Predecoder, "Found cs segment override.\n");
163 break;
164 case DSOverride:
165 DPRINTF(Predecoder, "Found ds segment override.\n");
166 break;
167 case ESOverride:
168 DPRINTF(Predecoder, "Found es segment override.\n");
169 break;
170 case FSOverride:
171 DPRINTF(Predecoder, "Found fs segment override.\n");
172 break;
173 case GSOverride:
174 DPRINTF(Predecoder, "Found gs segment override.\n");
175 break;
176 case SSOverride:
177 DPRINTF(Predecoder, "Found ss segment override.\n");
178 break;
179 case Lock:
180 DPRINTF(Predecoder, "Found lock prefix.\n");
181 break;
182 case Rep:
183 DPRINTF(Predecoder, "Found rep prefix.\n");
184 break;
185 case Repne:
186 DPRINTF(Predecoder, "Found repne prefix.\n");
187 break;
188 case Rex:
189 DPRINTF(Predecoder, "Found Rex prefix %#x.\n", nextByte);
190 emi.rexPrefix = nextByte;
191 break;
192 case 0:
193 emi.numOpcodes = 0;
194 state = Opcode;
195 break;
196 default:
197 panic("Unrecognized prefix %#x\n", nextByte);
198 }
199 break;
200 case Opcode:
201 emi.numOpcodes++;
202 assert(emi.numOpcodes < 2);
203 if(nextByte == 0xf0)
204 {
205 DPRINTF(Predecoder, "Found two byte opcode.\n");
206 }
207 else
208 {
209 immediateCollected = 0;
210 displacementCollected = 0;
211 emi.immediate = 0;
212 emi.displacement = 0;
213 int immType = ImmediateType[
214 emi.numOpcodes - 1][nextByte];
215 if(0) //16 bit mode
216 immediateSize = ImmediateTypeToSize[0][immType];
217 else if(!(emi.rexPrefix & 0x4)) //32 bit mode
218 immediateSize = ImmediateTypeToSize[1][immType];
219 else //64 bit mode
220 immediateSize = ImmediateTypeToSize[2][immType];
221 DPRINTF(Predecoder, "Found opcode %#x.\n", nextByte);
222 if (UsesModRM[emi.numOpcodes - 1][nextByte]) {
223 state = ModRM;
224 } else if(immediateSize) {
225 state = Immediate;
226 } else {
227 emiIsReady = true;
228 state = Prefix;
229 }
230 }
231 offset++;
232 break;
233 case ModRM:
234 DPRINTF(Predecoder, "Found modrm byte %#x.\n", nextByte);
235 if (0) {//in 16 bit mode
236 //figure out 16 bit displacement size
237 if(nextByte & 0xC7 == 0x06 ||
238 nextByte & 0xC0 == 0x80)
239 displacementSize = 2;
240 else if(nextByte & 0xC0 == 0x40)
241 displacementSize = 1;
242 else
243 displacementSize = 0;
244 } else {
245 //figure out 32/64 bit displacement size
246 if(nextByte & 0xC7 == 0x05 ||
247 nextByte & 0xC0 == 0x80)
248 displacementSize = 4;
249 else if(nextByte & 0xC0 == 0x40)
250 displacementSize = 2;
251 else
252 displacementSize = 0;
253 }
254 //If there's an SIB, get that next.
255 //There is no SIB in 16 bit mode.
256 if(nextByte & 0x7 == 4 &&
257 nextByte & 0xC0 != 0xC0) {
258 // && in 32/64 bit mode)
259 state = SIB;
260 } else if(displacementSize) {
261 state = Displacement;
262 } else if(immediateSize) {
263 state = Immediate;
264 } else {
265 emiIsReady = true;
266 state = Prefix;
267 }
268 //The ModRM byte is consumed no matter what
269 offset++;
270 break;
271 case SIB:
272 DPRINTF(Predecoder, "Found SIB byte %#x.\n", nextByte);
273 offset++;
274 if(displacementSize) {
275 state = Displacement;
276 } else if(immediateSize) {
277 state = Immediate;
278 } else {
279 emiIsReady = true;
280 state = Prefix;
281 }
282 break;
283 case Displacement:
284 //Gather up the displacement, or at least as much of it
285 //as we can get.
286
287 //Figure out how many bytes we still need to get for the
288 //displacement.
289 toGet = displacementSize - displacementCollected;
290 //Figure out how many bytes are left in our "buffer"
291 remaining = sizeof(MachInst) - offset;
292 //Get as much as we need, up to the amount available.
293 toGet = toGet > remaining ? remaining : toGet;
294
295 //Shift the bytes we want to be all the way to the right
296 partialDisp = fetchChunk >> (offset * 8);
297 //Mask off what we don't want
298 partialDisp &= mask(toGet * 8);
299 //Shift it over to overlay with our displacement.
300 partialDisp <<= (displacementCollected * 8);
301 //Put it into our displacement
302 emi.displacement |= partialDisp;
303 //Update how many bytes we've collected.
304 displacementCollected += toGet;
305 offset += toGet;
306 DPRINTF(Predecoder, "Collecting %d byte displacement, got %d bytes.\n",
307 displacementSize, displacementCollected);
308
309 if(displacementSize == displacementCollected) {
310 //Sign extend the displacement
311 switch(displacementSize)
312 {
313 case 1:
314 emi.displacement = sext<8>(emi.displacement);
315 break;
316 case 2:
317 emi.displacement = sext<16>(emi.displacement);
318 break;
319 case 4:
320 emi.displacement = sext<32>(emi.displacement);
321 break;
322 default:
323 panic("Undefined displacement size!\n");
324 }
325 DPRINTF(Predecoder, "Collected displacement %#x.\n",
326 emi.displacement);
327 if(immediateSize) {
328 state = Immediate;
329 } else {
330 emiIsReady = true;
331 state = Prefix;
332 }
333 }
334 break;
335 case Immediate:
336 //Gather up the displacement, or at least as much of it
337 //as we can get
338
339 //Figure out how many bytes we still need to get for the
340 //immediate.
341 toGet = immediateSize - immediateCollected;
342 //Figure out how many bytes are left in our "buffer"
343 remaining = sizeof(MachInst) - offset;
344 //Get as much as we need, up to the amount available.
345 toGet = toGet > remaining ? remaining : toGet;
346
347 //Shift the bytes we want to be all the way to the right
348 partialDisp = fetchChunk >> (offset * 8);
349 //Mask off what we don't want
350 partialDisp &= mask(toGet * 8);
351 //Shift it over to overlay with our immediate.
352 partialDisp <<= (immediateCollected * 8);
353 //Put it into our immediate
354 emi.immediate |= partialDisp;
355 //Update how many bytes we've collected.
356 immediateCollected += toGet;
357 offset += toGet;
358 DPRINTF(Predecoder, "Collecting %d byte immediate, got %d bytes.\n",
359 immediateSize, immediateCollected);
360
361 if(immediateSize == immediateCollected)
362 {
363 DPRINTF(Predecoder, "Collected immediate %#x.\n",
364 emi.immediate);
365 emiIsReady = true;
366 state = Prefix;
367 }
368 break;
369 default:
370 panic("Unrecognized state! %d\n", state);
371 }
372 if(offset == sizeof(MachInst))
373 outOfBytes = true;
374 }
375 }
376
377 //Use this to give data to the predecoder. This should be used
378 //when there is control flow.
379 void moreBytes(Addr currPC, Addr off, MachInst data)
380 {
381 basePC = currPC;
382 offset = off;
383 fetchChunk = data;
384 assert(off < sizeof(MachInst));
385 outOfBytes = false;
386 process();
387 }
388
389 //Use this to give data to the predecoder. This should be used
390 //when instructions are executed in order.
391 void moreBytes(MachInst machInst)
392 {
393 moreBytes(basePC + sizeof(machInst), 0, machInst);
394 }
395
396 bool needMoreBytes()
397 {
398 return outOfBytes;
399 }
400
401 bool extMachInstReady()
402 {
403 return emiIsReady;
404 }
405
406 //This returns a constant reference to the ExtMachInst to avoid a copy
407 const ExtMachInst & getExtMachInst()
408 {
409 assert(emiIsReady);
410 emiIsReady = false;
411 return emi;
412 }
413 };
414 };
415
416 #endif // __ARCH_X86_PREDECODER_HH__