decode_cache.cc revision 9022
1/* 2 * Copyright (c) 2011-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#include "arch/decoder.hh" 32#include "arch/isa_traits.hh" 33#include "arch/types.hh" 34#include "base/hashmap.hh" 35#include "config/the_isa.hh" 36#include "cpu/static_inst.hh" 37 38void 39DecodeCache::DecodePages::update(PageIt recentest) 40{ 41 recent[1] = recent[0]; 42 recent[0] = recentest; 43} 44 45void 46DecodeCache::DecodePages::addPage(Addr addr, DecodePage *page) 47{ 48 Addr page_addr = addr & ~(TheISA::PageBytes - 1); 49 typename PageMap::value_type to_insert(page_addr, page); 50 update(pageMap.insert(to_insert).first); 51} 52 53DecodeCache::DecodePages::DecodePages() 54{ 55 recent[0] = recent[1] = pageMap.end(); 56} 57 58DecodeCache::DecodePage * 59DecodeCache::DecodePages::getPage(Addr addr) 60{ 61 Addr page_addr = addr & ~(TheISA::PageBytes - 1); 62 63 // Check against recent lookups. 64 if (recent[0] != pageMap.end()) { 65 if (recent[0]->first == page_addr) 66 return recent[0]->second; 67 if (recent[1] != pageMap.end() && 68 recent[1]->first == page_addr) { 69 update(recent[1]); 70 // recent[1] has just become recent[0]. 71 return recent[0]->second; 72 } 73 } 74 75 // Actually look in the has_map. 76 PageIt it = pageMap.find(page_addr); 77 if (it != pageMap.end()) { 78 update(it); 79 return it->second; 80 } 81 82 // Didn't find an existing page, so add a new one. 83 DecodePage *newPage = new DecodePage; 84 addPage(page_addr, newPage); 85 return newPage; 86} 87 88StaticInstPtr 89DecodeCache::decode(TheISA::Decoder *decoder, 90 ExtMachInst mach_inst, Addr addr) 91{ 92 // Try to find a matching address based table of instructions. 93 DecodePage *page = decodePages.getPage(addr); 94 95 // Use the table to decode the instruction. It will fall back to other 96 // mechanisms if it needs to. 97 Addr offset = addr & (TheISA::PageBytes - 1); 98 StaticInstPtr si = page->insts[offset]; 99 if (si && (si->machInst == mach_inst)) 100 return si; 101 102 InstMap::iterator iter = instMap.find(mach_inst); 103 if (iter != instMap.end()) { 104 si = iter->second; 105 page->insts[offset] = si; 106 return si; 107 } 108 109 si = decoder->decodeInst(mach_inst); 110 instMap[mach_inst] = si; 111 page->insts[offset] = si; 112 return si; 113} 114