decoder.hh revision 9023:e9201a7bce59
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 * Authors: Gabe Black 29 */ 30 31#ifndef __ARCH_ARM_DECODER_HH__ 32#define __ARCH_ARM_DECODER_HH__ 33 34#include <cassert> 35 36#include "arch/arm/miscregs.hh" 37#include "arch/arm/types.hh" 38#include "base/types.hh" 39#include "cpu/decode_cache.hh" 40 41class ThreadContext; 42 43namespace ArmISA 44{ 45 46class Decoder 47{ 48 protected: 49 ThreadContext * tc; 50 //The extended machine instruction being generated 51 ExtMachInst emi; 52 MachInst data; 53 bool bigThumb; 54 bool instDone; 55 bool outOfBytes; 56 int offset; 57 bool foundIt; 58 ITSTATE itBits; 59 60 public: 61 void reset() 62 { 63 bigThumb = false; 64 offset = 0; 65 emi = 0; 66 instDone = false; 67 outOfBytes = true; 68 foundIt = false; 69 } 70 71 Decoder(ThreadContext * _tc) : tc(_tc), data(0) 72 { 73 reset(); 74 } 75 76 ThreadContext * getTC() 77 { 78 return tc; 79 } 80 81 void 82 setTC(ThreadContext * _tc) 83 { 84 tc = _tc; 85 } 86 87 void process(); 88 89 //Use this to give data to the decoder. This should be used 90 //when there is control flow. 91 void moreBytes(const PCState &pc, Addr fetchPC, MachInst inst); 92 93 //Use this to give data to the decoder. This should be used 94 //when instructions are executed in order. 95 void moreBytes(MachInst machInst) 96 { 97 moreBytes(0, 0, machInst); 98 } 99 100 inline void consumeBytes(int numBytes) 101 { 102 offset += numBytes; 103 assert(offset <= sizeof(MachInst)); 104 if (offset == sizeof(MachInst)) 105 outOfBytes = true; 106 } 107 108 bool needMoreBytes() const 109 { 110 return outOfBytes; 111 } 112 113 bool instReady() const 114 { 115 return instDone; 116 } 117 118 int getInstSize() const 119 { 120 return (!emi.thumb || emi.bigThumb) ? 4 : 2; 121 } 122 123 protected: 124 /// A cache of decoded instruction objects. 125 static DecodeCache defaultCache; 126 127 public: 128 StaticInstPtr decodeInst(ExtMachInst mach_inst); 129 130 /// Decode a machine instruction. 131 /// @param mach_inst The binary instruction to decode. 132 /// @retval A pointer to the corresponding StaticInst object. 133 StaticInstPtr 134 decode(ExtMachInst mach_inst, Addr addr) 135 { 136 return defaultCache.decode(this, mach_inst, addr); 137 } 138 139 StaticInstPtr 140 decode(ArmISA::PCState &nextPC) 141 { 142 if (!instDone) 143 return NULL; 144 145 assert(instDone); 146 ExtMachInst thisEmi = emi; 147 nextPC.npc(nextPC.pc() + getInstSize()); 148 if (foundIt) 149 nextPC.nextItstate(itBits); 150 thisEmi.itstate = nextPC.itstate(); 151 nextPC.size(getInstSize()); 152 emi = 0; 153 instDone = false; 154 foundIt = false; 155 return decode(thisEmi, nextPC.instAddr()); 156 } 157}; 158 159} // namespace ArmISA 160 161#endif // __ARCH_ARM_DECODER_HH__ 162