3 // Copyright (c) 2015 RISC-V Foundation
4 // Copyright (c) 2016 The University of Virginia
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met: redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer;
11 // redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution;
14 // neither the name of the copyright holders nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Authors: Alec Roelke
32 ////////////////////////////////////////////////////////////////////
34 // Integer instructions
39 * Base class for R-type operations
41 class ROp : public RiscvStaticInst
45 ROp(const char *mnem, MachInst _machInst, OpClass __opClass)
46 : RiscvStaticInst(mnem, _machInst, __opClass)
50 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
54 * Base class for I-type operations
56 class IOp : public RiscvStaticInst
62 IOp(const char *mnem, MachInst _machInst, OpClass __opClass)
63 : RiscvStaticInst(mnem, _machInst, __opClass),imm(IMM12)
66 imm |= ~((uint64_t)0x7FF);
70 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
74 * Class for jalr instructions
76 class Jump : public IOp
79 Jump(const char *mnem, MachInst _machInst, OpClass __opClass)
80 : IOp(mnem, _machInst, __opClass)
84 branchTarget(ThreadContext *tc) const;
86 using StaticInst::branchTarget;
87 using IOp::generateDisassembly;
91 * Base class for S-type operations
93 class SOp : public RiscvStaticInst
99 SOp(const char *mnem, MachInst _machInst, OpClass __opClass)
100 : RiscvStaticInst(mnem, _machInst, __opClass),imm(0)
105 imm |= ~((uint64_t)0x7FF);
109 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
113 * Base class for SB-type operations
115 class SBOp : public RiscvStaticInst
121 SBOp(const char *mnem, MachInst _machInst, OpClass __opClass)
122 : RiscvStaticInst(mnem, _machInst, __opClass),imm(0)
124 imm |= BIMM12BIT11 << 11;
125 imm |= BIMM12BITS4TO1 << 1;
126 imm |= BIMM12BITS10TO5 << 5;
128 imm |= ~((uint64_t)0xFFF);
132 branchTarget(const RiscvISA::PCState &branchPC) const;
134 using StaticInst::branchTarget;
137 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
141 * Base class for U-type operations
143 class UOp : public RiscvStaticInst
149 UOp(const char *mnem, MachInst _machInst, OpClass __opClass)
150 : RiscvStaticInst(mnem, _machInst, __opClass), imm(0)
152 int32_t temp = IMM20 << 12;
157 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
161 * Base class for UJ-type operations
163 class UJOp : public RiscvStaticInst
169 UJOp(const char *mnem, MachInst _machInst, OpClass __opClass)
170 : RiscvStaticInst(mnem, _machInst, __opClass),imm(0)
172 imm |= UJIMMBITS19TO12 << 12;
173 imm |= UJIMMBIT11 << 11;
174 imm |= UJIMMBITS10TO1 << 1;
176 imm |= ~((uint64_t)0xFFFFF);
180 branchTarget(const RiscvISA::PCState &branchPC) const;
182 using StaticInst::branchTarget;
185 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
189 //Outputs to decoder.cc
192 ROp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
194 std::stringstream ss;
195 ss << mnemonic << ' ' << regName(_destRegIdx[0]) << ", " <<
196 regName(_srcRegIdx[0]) << ", " << regName(_srcRegIdx[1]);
201 IOp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
203 std::stringstream ss;
204 ss << mnemonic << ' ' << regName(_destRegIdx[0]) << ", " <<
205 regName(_srcRegIdx[0]) << ", " << imm;
210 Jump::branchTarget(ThreadContext *tc) const
212 PCState pc = tc->pcState();
213 IntReg Rs1 = tc->readIntReg(_srcRegIdx[0]);
214 pc.set((Rs1 + imm)&~0x1);
219 SOp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
221 std::stringstream ss;
222 ss << mnemonic << ' ' << regName(_srcRegIdx[1]) << ", " << imm <<
223 '(' << regName(_srcRegIdx[0]) << ')';
228 SBOp::branchTarget(const RiscvISA::PCState &branchPC) const
230 return branchPC.pc() + imm;
234 SBOp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
236 std::stringstream ss;
237 ss << mnemonic << ' ' << regName(_srcRegIdx[0]) << ", " <<
238 regName(_srcRegIdx[1]) << ", " << imm;
243 UOp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
245 std::stringstream ss;
246 ss << mnemonic << ' ' << regName(_destRegIdx[0]) << ", " << imm;
251 UJOp::branchTarget(const RiscvISA::PCState &branchPC) const
253 return branchPC.pc() + imm;
257 UJOp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
259 std::stringstream ss;
260 ss << mnemonic << ' ' << regName(_destRegIdx[0]) << ", " << imm;
265 def format ROp(code, *opt_flags) {{
266 iop = InstObjParams(name, Name, 'ROp', code, opt_flags)
267 header_output = BasicDeclare.subst(iop)
268 decoder_output = BasicConstructor.subst(iop)
269 decode_block = BasicDecode.subst(iop)
270 exec_output = BasicExecute.subst(iop)
273 def format IOp(code, *opt_flags) {{
274 iop = InstObjParams(name, Name, 'IOp', code, opt_flags)
275 header_output = BasicDeclare.subst(iop)
276 decoder_output = BasicConstructor.subst(iop)
277 decode_block = BasicDecode.subst(iop)
278 exec_output = BasicExecute.subst(iop)
281 def format Jump(code, *opt_flags) {{
282 iop = InstObjParams(name, Name, 'Jump', code, opt_flags)
283 header_output = BasicDeclare.subst(iop)
284 decoder_output = BasicConstructor.subst(iop)
285 decode_block = BasicDecode.subst(iop)
286 exec_output = BasicExecute.subst(iop)
289 def format SOp(code, *opt_flags) {{
290 iop = InstObjParams(name, Name, 'SOp', code, opt_flags)
291 header_output = BasicDeclare.subst(iop)
292 decoder_output = BasicConstructor.subst(iop)
293 decode_block = BasicDecode.subst(iop)
294 exec_output = BasicExecute.subst(iop)
297 def format SBOp(code, *opt_flags) {{
298 iop = InstObjParams(name, Name, 'SBOp', code, opt_flags)
299 header_output = BasicDeclare.subst(iop)
300 decoder_output = BasicConstructor.subst(iop)
301 decode_block = BasicDecode.subst(iop)
302 exec_output = BasicExecute.subst(iop)
305 def format UOp(code, *opt_flags) {{
306 iop = InstObjParams(name, Name, 'UOp', code, opt_flags)
307 header_output = BasicDeclare.subst(iop)
308 decoder_output = BasicConstructor.subst(iop)
309 decode_block = BasicDecode.subst(iop)
310 exec_output = BasicExecute.subst(iop)
313 def format UJOp(code, *opt_flags) {{
314 iop = InstObjParams(name, Name, 'UJOp', code, opt_flags)
315 header_output = BasicDeclare.subst(iop)
316 decoder_output = BasicConstructor.subst(iop)
317 decode_block = BasicDecode.subst(iop)
318 exec_output = BasicExecute.subst(iop)