decode_cache.hh revision 9024
12810SN/A/*
210623Smitch.hayenga@arm.com * Copyright (c) 2011 Google
39546Sandreas.hansson@arm.com * All rights reserved.
49546Sandreas.hansson@arm.com *
59546Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
69546Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
79546Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
89546Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
99546Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
109546Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
119546Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
129546Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
139546Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
142810SN/A * this software without specific prior written permission.
152810SN/A *
162810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272810SN/A *
282810SN/A * Authors: Gabe Black
292810SN/A */
302810SN/A
312810SN/A#ifndef __CPU_DECODE_CACHE_HH__
322810SN/A#define __CPU_DECODE_CACHE_HH__
332810SN/A
342810SN/A#include "arch/isa_traits.hh"
352810SN/A#include "arch/types.hh"
362810SN/A#include "base/hashmap.hh"
372810SN/A#include "config/the_isa.hh"
382810SN/A#include "cpu/static_inst_fwd.hh"
392810SN/A
402810SN/Anamespace TheISA
4110623Smitch.hayenga@arm.com{
422810SN/A    class Decoder;
432810SN/A}
442810SN/A
452810SN/Anamespace DecodeCache
462810SN/A{
472810SN/A
482810SN/A/// Hash for decoded instructions.
4910623Smitch.hayenga@arm.comtypedef m5::hash_map<TheISA::ExtMachInst, StaticInstPtr> InstMap;
5010623Smitch.hayenga@arm.com
512810SN/A/// A sparse map from an Addr to a Value, stored in page chunks.
5212727Snikos.nikoleris@arm.comtemplate<class Value>
5312727Snikos.nikoleris@arm.comclass AddrMap
543861SN/A{
5512727Snikos.nikoleris@arm.com  protected:
563861SN/A    // A pages worth of cache entries.
5712727Snikos.nikoleris@arm.com    struct CachePage {
589288Sandreas.hansson@arm.com        Value items[TheISA::PageBytes];
5913416Sjavier.bueno@metempsy.com    };
603861SN/A    // A map of cache pages which allows a sparse mapping.
612810SN/A    typedef typename m5::hash_map<Addr, CachePage *> PageMap;
6212727Snikos.nikoleris@arm.com    typedef typename PageMap::iterator PageIt;
633861SN/A    // Mini cache of recent lookups.
649288Sandreas.hansson@arm.com    PageIt recent[2];
652810SN/A    PageMap pageMap;
6613416Sjavier.bueno@metempsy.com
6713416Sjavier.bueno@metempsy.com    /// Update the mini cache of recent lookups.
6813416Sjavier.bueno@metempsy.com    /// @param recentest The most recent result;
6913416Sjavier.bueno@metempsy.com    void
7013416Sjavier.bueno@metempsy.com    update(PageIt recentest)
7113416Sjavier.bueno@metempsy.com    {
7213416Sjavier.bueno@metempsy.com        recent[1] = recent[0];
7313416Sjavier.bueno@metempsy.com        recent[0] = recentest;
7413416Sjavier.bueno@metempsy.com    }
7513416Sjavier.bueno@metempsy.com
7613416Sjavier.bueno@metempsy.com    /// Attempt to find the CacheePage which goes with a particular
7713416Sjavier.bueno@metempsy.com    /// address. First check the small cache of recent results, then
7813416Sjavier.bueno@metempsy.com    /// actually look in the hash_map.
7913667Sjavier.bueno@metempsy.com    /// @param addr The address to look up.
8013667Sjavier.bueno@metempsy.com    CachePage *
812810SN/A    getPage(Addr addr)
8213551Sjavier.bueno@metempsy.com    {
8313551Sjavier.bueno@metempsy.com        Addr page_addr = addr & ~(TheISA::PageBytes - 1);
8413551Sjavier.bueno@metempsy.com
8513551Sjavier.bueno@metempsy.com        // Check against recent lookups.
8613551Sjavier.bueno@metempsy.com        if (recent[0] != pageMap.end()) {
8713551Sjavier.bueno@metempsy.com            if (recent[0]->first == page_addr)
8813551Sjavier.bueno@metempsy.com                return recent[0]->second;
8913551Sjavier.bueno@metempsy.com            if (recent[1] != pageMap.end() &&
9013551Sjavier.bueno@metempsy.com                    recent[1]->first == page_addr) {
9113551Sjavier.bueno@metempsy.com                update(recent[1]);
9213551Sjavier.bueno@metempsy.com                // recent[1] has just become recent[0].
9313551Sjavier.bueno@metempsy.com                return recent[0]->second;
9413551Sjavier.bueno@metempsy.com            }
9513551Sjavier.bueno@metempsy.com        }
9613551Sjavier.bueno@metempsy.com
9713551Sjavier.bueno@metempsy.com        // Actually look in the has_map.
9813551Sjavier.bueno@metempsy.com        PageIt it = pageMap.find(page_addr);
9913551Sjavier.bueno@metempsy.com        if (it != pageMap.end()) {
10013551Sjavier.bueno@metempsy.com            update(it);
10113551Sjavier.bueno@metempsy.com            return it->second;
10213551Sjavier.bueno@metempsy.com        }
10313551Sjavier.bueno@metempsy.com
10413551Sjavier.bueno@metempsy.com        // Didn't find an existing page, so add a new one.
10513551Sjavier.bueno@metempsy.com        CachePage *newPage = new CachePage;
10613551Sjavier.bueno@metempsy.com        page_addr = page_addr & ~(TheISA::PageBytes - 1);
10713551Sjavier.bueno@metempsy.com        typename PageMap::value_type to_insert(page_addr, newPage);
10813551Sjavier.bueno@metempsy.com        update(pageMap.insert(to_insert).first);
10913551Sjavier.bueno@metempsy.com        return newPage;
11013551Sjavier.bueno@metempsy.com    }
11113551Sjavier.bueno@metempsy.com
11213551Sjavier.bueno@metempsy.com  public:
11313551Sjavier.bueno@metempsy.com    /// Constructor
11413551Sjavier.bueno@metempsy.com    AddrMap()
11513551Sjavier.bueno@metempsy.com    {
11613551Sjavier.bueno@metempsy.com        recent[0] = recent[1] = pageMap.end();
11713551Sjavier.bueno@metempsy.com    }
11813551Sjavier.bueno@metempsy.com
11913551Sjavier.bueno@metempsy.com    Value &
12013551Sjavier.bueno@metempsy.com    lookup(Addr addr)
12113551Sjavier.bueno@metempsy.com    {
12213551Sjavier.bueno@metempsy.com        CachePage *page = getPage(addr);
12313551Sjavier.bueno@metempsy.com        return page->items[addr & (TheISA::PageBytes - 1)];
12413551Sjavier.bueno@metempsy.com    }
12513551Sjavier.bueno@metempsy.com};
12613551Sjavier.bueno@metempsy.com
12713551Sjavier.bueno@metempsy.com} // namespace DecodeCache
12813551Sjavier.bueno@metempsy.com
12913551Sjavier.bueno@metempsy.com#endif // __CPU_DECODE_CACHE_HH__
13013551Sjavier.bueno@metempsy.com