decoder.cc revision 12407
112837Sgabeblack@google.com/*
212837Sgabeblack@google.com * Copyright (c) 2012 Google
312837Sgabeblack@google.com * Copyright (c) The University of Virginia
412837Sgabeblack@google.com * All rights reserved.
512837Sgabeblack@google.com *
612837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
712837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
812837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1012837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1112837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1212837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1312837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1412837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1512837Sgabeblack@google.com * this software without specific prior written permission.
1612837Sgabeblack@google.com *
1712837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1812837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1912837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2012837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2112837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2212837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2312837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2412837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2512837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2612837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2712837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2812837Sgabeblack@google.com *
2912837Sgabeblack@google.com * Authors: Gabe Black
3012837Sgabeblack@google.com *          Alec Roelke
3112837Sgabeblack@google.com */
3212837Sgabeblack@google.com
3312837Sgabeblack@google.com#include "arch/riscv/decoder.hh"
3412837Sgabeblack@google.com#include "arch/riscv/types.hh"
3512837Sgabeblack@google.com#include "debug/Decode.hh"
3612837Sgabeblack@google.com
3712837Sgabeblack@google.comnamespace RiscvISA
3812837Sgabeblack@google.com{
3912837Sgabeblack@google.com
4012837Sgabeblack@google.comstatic const MachInst LowerBitMask = (1 << sizeof(MachInst) * 4) - 1;
4112837Sgabeblack@google.comstatic const MachInst UpperBitMask = LowerBitMask << sizeof(MachInst) * 4;
4212837Sgabeblack@google.com
4312837Sgabeblack@google.comvoid Decoder::reset()
4412837Sgabeblack@google.com{
4512837Sgabeblack@google.com    aligned = true;
4612837Sgabeblack@google.com    mid = false;
4712837Sgabeblack@google.com    more = true;
4812837Sgabeblack@google.com    emi = 0;
4912837Sgabeblack@google.com    instDone = false;
5012837Sgabeblack@google.com}
5112837Sgabeblack@google.com
5212837Sgabeblack@google.comvoid
5312837Sgabeblack@google.comDecoder::moreBytes(const PCState &pc, Addr fetchPC, MachInst inst)
5412837Sgabeblack@google.com{
5512837Sgabeblack@google.com    DPRINTF(Decode, "Requesting bytes 0x%08x from address %#x\n", inst,
5612837Sgabeblack@google.com            fetchPC);
5712837Sgabeblack@google.com
5812837Sgabeblack@google.com    bool aligned = pc.pc() % sizeof(MachInst) == 0;
5912837Sgabeblack@google.com    if (aligned) {
6012837Sgabeblack@google.com        emi = inst;
6112837Sgabeblack@google.com        if (compressed(emi))
6212837Sgabeblack@google.com            emi &= LowerBitMask;
6312837Sgabeblack@google.com        more = !compressed(emi);
6412837Sgabeblack@google.com        instDone = true;
6512837Sgabeblack@google.com    } else {
6612837Sgabeblack@google.com        if (mid) {
6712837Sgabeblack@google.com            assert((emi & UpperBitMask) == 0);
6812837Sgabeblack@google.com            emi |= (inst & LowerBitMask) << sizeof(MachInst)*4;
6912837Sgabeblack@google.com            mid = false;
7012837Sgabeblack@google.com            more = false;
7112837Sgabeblack@google.com            instDone = true;
7212837Sgabeblack@google.com        } else {
7312837Sgabeblack@google.com            emi = (inst & UpperBitMask) >> sizeof(MachInst)*4;
7412837Sgabeblack@google.com            mid = !compressed(emi);
7512837Sgabeblack@google.com            more = true;
7612837Sgabeblack@google.com            instDone = compressed(emi);
7712837Sgabeblack@google.com        }
7812837Sgabeblack@google.com    }
7912837Sgabeblack@google.com}
8012837Sgabeblack@google.com
8112837Sgabeblack@google.comStaticInstPtr
8212837Sgabeblack@google.comDecoder::decode(ExtMachInst mach_inst, Addr addr)
8312837Sgabeblack@google.com{
8412837Sgabeblack@google.com    DPRINTF(Decode, "Decoding instruction 0x%08x at address %#x\n",
8512837Sgabeblack@google.com            mach_inst, addr);
8612837Sgabeblack@google.com    if (instMap.find(mach_inst) != instMap.end())
8712837Sgabeblack@google.com        return instMap[mach_inst];
8812837Sgabeblack@google.com    else {
8912837Sgabeblack@google.com        StaticInstPtr si = decodeInst(mach_inst);
9012837Sgabeblack@google.com        instMap[mach_inst] = si;
9112837Sgabeblack@google.com        return si;
9212837Sgabeblack@google.com    }
9312837Sgabeblack@google.com}
9412837Sgabeblack@google.com
9512837Sgabeblack@google.comStaticInstPtr
9612837Sgabeblack@google.comDecoder::decode(RiscvISA::PCState &nextPC)
9712837Sgabeblack@google.com{
9812837Sgabeblack@google.com    if (!instDone)
9912837Sgabeblack@google.com        return nullptr;
10012837Sgabeblack@google.com    instDone = false;
10112837Sgabeblack@google.com
10212837Sgabeblack@google.com    if (compressed(emi)) {
10312837Sgabeblack@google.com        nextPC.npc(nextPC.instAddr() + sizeof(MachInst) / 2);
10412837Sgabeblack@google.com    } else {
10512837Sgabeblack@google.com        nextPC.npc(nextPC.instAddr() + sizeof(MachInst));
10612837Sgabeblack@google.com    }
10712837Sgabeblack@google.com
10812837Sgabeblack@google.com    return decode(emi, nextPC.instAddr());
10912837Sgabeblack@google.com}
11012837Sgabeblack@google.com
11112837Sgabeblack@google.com}
11212837Sgabeblack@google.com