// -*- mode:c++ -*-

// Copyright (c) 2015 RISC-V Foundation
// Copyright (c) 2016-2017 The University of Virginia
// Copyright (c) 2020 Barkhausen Institut
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met: redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer;
// redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution;
// neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

////////////////////////////////////////////////////////////////////
//
// Integer instructions
//

def template ImmDeclare {{
    //
    // Static instruction class for "%(mnemonic)s".
    //
    class %(class_name)s : public %(base_class)s
    {
      private:
        %(reg_idx_arr_decl)s;

      public:
        /// Constructor.
        %(class_name)s(MachInst machInst);
        Fault execute(ExecContext *, Trace::InstRecord *) const override;
        std::string generateDisassembly(Addr pc,
            const loader::SymbolTable *symtab) const override;
    };
}};

def template ImmConstructor {{
    %(class_name)s::%(class_name)s(MachInst machInst)
        : %(base_class)s("%(mnemonic)s", machInst, %(op_class)s)
    {
        %(set_reg_idx_arr)s;
        %(constructor)s;
        %(imm_code)s;
    }
}};

def template ImmExecute {{
    Fault
    %(class_name)s::execute(
        ExecContext *xc, Trace::InstRecord *traceData) const
    {
        %(op_decl)s;
        %(op_rd)s;
        %(code)s;
        %(op_wb)s;
        return NoFault;
    }

    std::string
    %(class_name)s::generateDisassembly(Addr pc,
            const loader::SymbolTable *symtab) const
    {
        std::vector<RegId> indices = {%(regs)s};
        std::stringstream ss;
        ss << mnemonic << ' ';
        for (const RegId& idx: indices)
            ss << registerName(idx) << ", ";
        ss << imm;
        return ss.str();
    }
}};

def template CILuiExecute {{
    Fault
    %(class_name)s::execute(
        ExecContext *xc, Trace::InstRecord *traceData) const
    {
        %(op_decl)s;
        %(op_rd)s;
        %(code)s;
        %(op_wb)s;
        return NoFault;
    }

    std::string
    %(class_name)s::generateDisassembly(Addr pc,
            const loader::SymbolTable *symtab) const
    {
        std::vector<RegId> indices = {%(regs)s};
        std::stringstream ss;
        ss << mnemonic << ' ';
        for (const RegId& idx: indices)
            ss << registerName(idx) << ", ";
        // To be compliant with GCC, the immediate is formated to a 20-bit
        // signed integer.
        ss << ((((uint64_t)imm) >> 12) & 0xFFFFF);
        return ss.str();
    }
}};

def template FenceExecute {{
    Fault
    %(class_name)s::execute(
        ExecContext *xc, Trace::InstRecord *traceData) const
    {
        %(op_decl)s;
        %(op_rd)s;
        %(code)s;
        %(op_wb)s;
        return NoFault;
    }

    std::string
    %(class_name)s::generateDisassembly(Addr pc,
            const loader::SymbolTable *symtab) const
    {
        std::stringstream ss;
        ss << mnemonic;
        if (!FUNCT3) {
            ss << ' ';
            if (PRED & 0x8)
                ss << 'i';
            if (PRED & 0x4)
                ss << 'o';
            if (PRED & 0x2)
                ss << 'r';
            if (PRED & 0x1)
                ss << 'w';
            ss << ", ";
            if (SUCC & 0x8)
                ss << 'i';
            if (SUCC & 0x4)
                ss << 'o';
            if (SUCC & 0x2)
                ss << 'r';
            if (SUCC & 0x1)
                ss << 'w';
        }
        return ss.str();
    }
}};

def template BranchDeclare {{
    //
    // Static instruction class for "%(mnemonic)s".
    //
    class %(class_name)s : public %(base_class)s
    {
      private:
        %(reg_idx_arr_decl)s;

      public:
        /// Constructor.
        %(class_name)s(MachInst machInst);
        Fault execute(ExecContext *, Trace::InstRecord *) const override;

        std::string
        generateDisassembly(
                Addr pc, const loader::SymbolTable *symtab) const override;

        RiscvISA::PCState
        branchTarget(const RiscvISA::PCState &branchPC) const override;

        using StaticInst::branchTarget;
    };
}};

def template BranchExecute {{
    Fault
    %(class_name)s::execute(ExecContext *xc,
        Trace::InstRecord *traceData) const
    {
        %(op_decl)s;
        %(op_rd)s;
        %(code)s;
        %(op_wb)s;
        return NoFault;
    }

    RiscvISA::PCState
    %(class_name)s::branchTarget(const RiscvISA::PCState &branchPC) const
    {
        return branchPC.pc() + imm;
    }

    std::string
    %(class_name)s::generateDisassembly(
            Addr pc, const loader::SymbolTable *symtab) const
    {
        std::vector<RegId> indices = {%(regs)s};
        std::stringstream ss;
        ss << mnemonic << ' ';
        for (const RegId& idx: indices)
            ss << registerName(idx) << ", ";
        ss << imm;
        return ss.str();
    }
}};

def template JumpDeclare {{
    //
    // Static instruction class for "%(mnemonic)s".
    //
    class %(class_name)s : public %(base_class)s
    {
      private:
        %(reg_idx_arr_decl)s;

      public:
        /// Constructor.
        %(class_name)s(MachInst machInst);
        Fault execute(ExecContext *, Trace::InstRecord *) const override;

        std::string
        generateDisassembly(
                Addr pc, const loader::SymbolTable *symtab) const override;

        RiscvISA::PCState
        branchTarget(ThreadContext *tc) const override;

        using StaticInst::branchTarget;
    };
}};

def template JumpExecute {{
    Fault
    %(class_name)s::execute(
        ExecContext *xc, Trace::InstRecord *traceData) const
    {
        %(op_decl)s;
        %(op_rd)s;
        %(code)s;
        %(op_wb)s;
        return NoFault;
    }

    RiscvISA::PCState
    %(class_name)s::branchTarget(ThreadContext *tc) const
    {
        PCState pc = tc->pcState();
        pc.set((tc->readIntReg(srcRegIdx(0).index()) + imm)&~0x1);
        return pc;
    }

    std::string
    %(class_name)s::generateDisassembly(
            Addr pc, const loader::SymbolTable *symtab) const
    {
        std::stringstream ss;
        ss << mnemonic << ' ';
        if (QUADRANT == 0x3)
            ss << registerName(destRegIdx(0)) << ", "
               << imm << "(" << registerName(srcRegIdx(0)) << ")";
        else
            ss << registerName(srcRegIdx(0));
        return ss.str();
    }
}};

def template CSRExecuteRo {{
    Fault
    %(class_name)s::execute(ExecContext *xc,
        Trace::InstRecord *traceData) const
    {
        if (!valid) {
            return std::make_shared<IllegalInstFault>(
                    csprintf("Illegal CSR index %#x\n", csr), machInst);
        }

        %(op_decl)s;
        %(op_rd)s;

        switch (csr) {
          case CSR_SATP: {
            auto pm = (PrivilegeMode)xc->readMiscReg(MISCREG_PRV);
            STATUS status = xc->readMiscReg(MISCREG_STATUS);
            if (pm == PRV_U || (pm == PRV_S && status.tvm == 1)) {
                return std::make_shared<IllegalInstFault>(
                        "SATP access in user mode or with TVM enabled\n",
                        machInst);
            }
            break;
          }
          case CSR_MSTATUS: {
            auto pm = (PrivilegeMode)xc->readMiscReg(MISCREG_PRV);
            if (pm != PrivilegeMode::PRV_M) {
                return std::make_shared<IllegalInstFault>(
                        "MSTATUS is only accessibly in machine mode\n",
                        machInst);
            }
            break;
          }
          default:
            break;
        }

        RegVal data;
        if (csr == CSR_FCSR) {
            data = xc->readMiscReg(MISCREG_FFLAGS) |
                   (xc->readMiscReg(MISCREG_FRM) << FRM_OFFSET);
        } else {
            data = xc->readMiscReg(midx);
        }

        DPRINTF(RiscvMisc, "Reading CSR %s: %#x\n", csrName, data);

        %(code)s;
        %(op_wb)s;

        return NoFault;
    }
}};

def template CSRExecuteRw {{
    Fault
    %(class_name)s::execute(ExecContext *xc,
        Trace::InstRecord *traceData) const
    {
        if (!valid) {
            return std::make_shared<IllegalInstFault>(
                    csprintf("Illegal CSR index %#x\n", csr), machInst);
        }
        if (bits(csr, 11, 10) == 0x3) {
            return std::make_shared<IllegalInstFault>(
                    csprintf("CSR %s is read-only\n", csrName), machInst);
        }

        %(op_decl)s;
        %(op_rd)s;

        switch (csr) {
          case CSR_SATP: {
            auto pm = (PrivilegeMode)xc->readMiscReg(MISCREG_PRV);
            STATUS status = xc->readMiscReg(MISCREG_STATUS);
            if (pm == PRV_U || (pm == PRV_S && status.tvm == 1)) {
                return std::make_shared<IllegalInstFault>(
                        "SATP access in user mode or with TVM enabled\n",
                        machInst);
            }
            break;
          }
          case CSR_MSTATUS: {
            auto pm = (PrivilegeMode)xc->readMiscReg(MISCREG_PRV);
            if (pm != PrivilegeMode::PRV_M) {
                return std::make_shared<IllegalInstFault>(
                        "MSTATUS is only accessibly in machine mode\n",
                        machInst);
            }
            break;
          }
          default:
            break;
        }

        RegVal data;
        if (csr == CSR_FCSR) {
            data = xc->readMiscReg(MISCREG_FFLAGS) |
                      (xc->readMiscReg(MISCREG_FRM) << FRM_OFFSET);
        } else {
            data = xc->readMiscReg(midx);
        }

        RegVal original = data;

        DPRINTF(RiscvMisc, "Reading CSR %s: %#x\n", csrName, data & maskVal);

        %(code)s;

        // We must keep those original bits not in the mask. Hidden bits should
        // keep their original value.
        data = (original & ~maskVal) | (data & maskVal);

        DPRINTF(RiscvMisc, "Writing %#x to CSR %s.\n", data, csrName);

        if (csr == CSR_FCSR) {
            xc->setMiscReg(MISCREG_FFLAGS, bits(data, 4, 0));
            xc->setMiscReg(MISCREG_FRM, bits(data, 7, 5));
        } else {
            xc->setMiscReg(midx, data);
        }

        %(op_wb)s;
        return NoFault;
    }
}};

def format ROp(code, *opt_flags) {{
    iop = InstObjParams(name, Name, 'RegOp', code, opt_flags)
    header_output = BasicDeclare.subst(iop)
    decoder_output = BasicConstructor.subst(iop)
    decode_block = BasicDecode.subst(iop)
    exec_output = BasicExecute.subst(iop)
}};

def format IOp(code, imm_type='int64_t', imm_code='imm = sext<12>(IMM12);',
              *opt_flags) {{
    regs = ['destRegIdx(0)','srcRegIdx(0)']
    iop = InstObjParams(name, Name, 'ImmOp<%s>' % imm_type,
        {'imm_code': imm_code, 'code': code,
         'regs': ','.join(regs)}, opt_flags)
    header_output = ImmDeclare.subst(iop)
    decoder_output = ImmConstructor.subst(iop)
    decode_block = BasicDecode.subst(iop)
    exec_output = ImmExecute.subst(iop)
}};

def format FenceOp(code, imm_type='int64_t', *opt_flags) {{
    regs = ['destRegIdx(0)','srcRegIdx(0)']
    iop = InstObjParams(name, Name, 'ImmOp<%s>' % imm_type,
        {'code': code, 'imm_code': 'imm = sext<12>(IMM12);',
         'regs': ','.join(regs)}, opt_flags)
    header_output = ImmDeclare.subst(iop)
    decoder_output = ImmConstructor.subst(iop)
    decode_block = BasicDecode.subst(iop)
    exec_output = FenceExecute.subst(iop)
}};

def format BOp(code, *opt_flags) {{
    imm_code = """
                imm = BIMM12BITS4TO1 << 1  |
                      BIMM12BITS10TO5 << 5 |
                      BIMM12BIT11 << 11    |
                      IMMSIGN << 12;
                imm = sext<13>(imm);
               """
    regs = ['srcRegIdx(0)','srcRegIdx(1)']
    iop = InstObjParams(name, Name, 'ImmOp<int64_t>',
        {'code': code, 'imm_code': imm_code,
         'regs': ','.join(regs)}, opt_flags)
    header_output = BranchDeclare.subst(iop)
    decoder_output = ImmConstructor.subst(iop)
    decode_block = BasicDecode.subst(iop)
    exec_output = BranchExecute.subst(iop)
}};

def format Jump(code, *opt_flags) {{
    regs = ['srcRegIdx(0)']
    iop = InstObjParams(name, Name, 'ImmOp<int64_t>',
        {'code': code, 'imm_code': 'imm = sext<12>(IMM12);',
         'regs': ','.join(regs)}, opt_flags)
    header_output = JumpDeclare.subst(iop)
    decoder_output = ImmConstructor.subst(iop)
    decode_block = BasicDecode.subst(iop)
    exec_output = JumpExecute.subst(iop)
}};

def format UOp(code, *opt_flags) {{
    regs = ['destRegIdx(0)']
    iop = InstObjParams(name, Name, 'ImmOp<int64_t>',
        {'code': code, 'imm_code': 'imm = IMM20;',
         'regs': ','.join(regs)}, opt_flags)
    header_output = ImmDeclare.subst(iop)
    decoder_output = ImmConstructor.subst(iop)
    decode_block = BasicDecode.subst(iop)
    exec_output = ImmExecute.subst(iop)
}};

def format JOp(code, *opt_flags) {{
    imm_code = """
                imm = UJIMMBITS10TO1 << 1   |
                      UJIMMBIT11 << 11      |
                      UJIMMBITS19TO12 << 12 |
                      IMMSIGN << 20;
                imm = sext<21>(imm);
               """
    pc = 'pc.set(pc.pc() + imm);'
    regs = ['destRegIdx(0)']
    iop = InstObjParams(name, Name, 'ImmOp<int64_t>',
        {'code': code, 'imm_code': imm_code,
         'regs': ','.join(regs)}, opt_flags)
    header_output = BranchDeclare.subst(iop)
    decoder_output = ImmConstructor.subst(iop)
    decode_block = BasicDecode.subst(iop)
    exec_output = BranchExecute.subst(iop)
}};

def format SystemOp(code, *opt_flags) {{
    iop = InstObjParams(name, Name, 'SystemOp', code, opt_flags)
    header_output = BasicDeclare.subst(iop)
    decoder_output = BasicConstructor.subst(iop)
    decode_block = BasicDecode.subst(iop)
    exec_output = BasicExecute.subst(iop)
}};

def template CSRDecode {{
    if (RS1)
        return new %(class_name)sRw(machInst);
    else
        return new %(class_name)sRo(machInst);
}};

def format CSROp(code, *opt_flags) {{
    iop = InstObjParams(name, Name + "Ro", 'CSROp', code, opt_flags)
    header_output = BasicDeclare.subst(iop)
    decoder_output = BasicConstructor.subst(iop)
    exec_output = CSRExecuteRo.subst(iop)

    iop = InstObjParams(name, Name + "Rw", 'CSROp', code, opt_flags)
    header_output += BasicDeclare.subst(iop)
    decoder_output += BasicConstructor.subst(iop)
    exec_output += CSRExecuteRw.subst(iop)

    iop = InstObjParams(name, Name, 'CSROp', "", opt_flags)
    decode_block = CSRDecode.subst(iop)
}};
