gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
decoder.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Google
3 * Copyright (c) The University of Virginia
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "arch/riscv/decoder.hh"
32#include "arch/riscv/isa.hh"
33#include "arch/riscv/types.hh"
34#include "base/bitfield.hh"
35#include "debug/Decode.hh"
36
37namespace gem5
38{
39
40namespace RiscvISA
41{
42
43Decoder::Decoder(const RiscvDecoderParams &p) : InstDecoder(p, &machInst)
44{
45 ISA *isa = dynamic_cast<ISA*>(p.isa);
46 vlen = isa->getVecLenInBits();
48 _enableZcd = isa->enableZcd();
49 reset();
50}
51
53{
54 aligned = true;
55 mid = false;
56 machInst = 0;
57 emi = 0;
58 jvtEntry = 0;
59 squashed = true;
60}
61
62void
64{
65 // The MSB of the upper and lower halves of a machine instruction.
66 constexpr size_t max_bit = sizeof(machInst) * 8 - 1;
67 constexpr size_t mid_bit = sizeof(machInst) * 4 - 1;
68
69 auto inst = letoh(machInst);
70 DPRINTF(Decode, "Requesting bytes 0x%08x from address %#x\n", inst,
71 fetchPC);
72
73 PCState pc_state = pc.as<PCState>();
74
75 if (GEM5_UNLIKELY(pc_state.zcmtSecondFetch())) {
76 if (mid) {
77 replaceBits(jvtEntry, sizeof(jvtEntry) * 8 - 1, max_bit + 1, inst);
78 mid = false;
79 instDone = true;
80 outOfBytes = true;
81 } else {
82 replaceBits(jvtEntry, max_bit, 0, inst);
83 mid = (pc_state.rvType() != RV32);
84 instDone = (pc_state.rvType() == RV32);
85 outOfBytes = true;
86 }
87
88 if (instDone && pc_state.rvType() == RV32) {
90 }
91 return;
92 }
93
94 bool aligned = pc.instAddr() % sizeof(machInst) == 0;
95 if (aligned) {
96 emi.instBits = inst;
97 if (compressed(inst))
98 emi.instBits = bits(inst, mid_bit, 0);
100 instDone = true;
101 } else {
102 if (mid) {
103 assert(bits(emi.instBits, max_bit, mid_bit + 1) == 0);
104 replaceBits(emi.instBits, max_bit, mid_bit + 1, inst);
105 mid = false;
106 outOfBytes = false;
107 instDone = true;
108 } else {
109 emi.instBits = bits(inst, max_bit, mid_bit + 1);
110 mid = !compressed(emi);
111 outOfBytes = true;
113 }
114 }
115}
116
119{
120 DPRINTF(Decode, "Decoding instruction 0x%08x at address %#x\n",
121 mach_inst.instBits, addr);
122
123 StaticInstPtr &si = instMap[mach_inst];
124 if (!si)
125 si = decodeInst(mach_inst);
126
127 si->size(compressed(mach_inst) ? 2 : 4);
128
129 DPRINTF(Decode, "Decode: Decoded %s instruction: %#x\n",
130 si->getName(), mach_inst);
131 return si;
132}
133
136{
137 if (!instDone)
138 return nullptr;
139 instDone = false;
140
141 auto &next_pc = _next_pc.as<PCState>();
142
143 if (GEM5_UNLIKELY(next_pc.zcmtSecondFetch())) {
144 return new ZcmtSecondFetchInst(emi, jvtEntry);
145 }
146
147 if (compressed(emi)) {
148 next_pc.npc(next_pc.instAddr() + sizeof(machInst) / 2);
149 next_pc.compressed(true);
150 } else {
151 next_pc.npc(next_pc.instAddr() + sizeof(machInst));
152 next_pc.compressed(false);
153 }
154
155 if (GEM5_UNLIKELY(squashed || next_pc.new_vconf())) {
156 squashed = false;
157 next_pc.new_vconf(false);
158 vl = next_pc.vl();
159 vtype = next_pc.vtype();
160 } else {
161 next_pc.vl(vl);
162 next_pc.vtype(vtype);
163 }
164
165 emi.vl = vl;
166 emi.vtype8 = vtype & 0xff;
167 emi.vill = vtype.vill;
168 emi.rv_type = static_cast<int>(next_pc.rvType());
169 emi.enable_zcd = _enableZcd;
170
171 return decode(emi, next_pc.instAddr());
172}
173
174} // namespace RiscvISA
175} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
InstDecoder(const InstDecoderParams &params, MoreBytesType *mb_buf)
Definition decoder.hh:54
Target & as()
Definition pcstate.hh:73
void moreBytes(const PCStateBase &pc, Addr fetchPC) override
Feed data to the decoder.
Definition decoder.cc:63
void reset() override
Definition decoder.cc:52
StaticInstPtr decode(ExtMachInst mach_inst, Addr addr)
Decode a machine instruction.
Definition decoder.cc:118
virtual StaticInstPtr decodeInst(ExtMachInst mach_inst)
Decoder(const RiscvDecoderParams &p)
Definition decoder.cc:43
decode_cache::InstMap< ExtMachInst > instMap
Definition decoder.hh:54
bool enableZcd()
Definition isa.hh:198
unsigned getVecElemLenInBits()
Definition isa.hh:190
unsigned getVecLenInBits()
Methods for getting VLEN, VLENB and ELEN values.
Definition isa.hh:188
void zcmtSecondFetch(bool z)
Definition pcstate.hh:120
void rvType(RiscvType rvType)
Definition pcstate.hh:108
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition bitfield.hh:79
constexpr uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition bitfield.hh:129
constexpr void replaceBits(T &val, unsigned first, unsigned last, B bit_val)
A convenience function to replace bits first to last of val with bit_val in place.
Definition bitfield.hh:216
Bitfield< 6 > si
Bitfield< 0 > p
constexpr enums::RiscvType RV32
Definition pcstate.hh:56
Bitfield< 61 > compressed
Definition types.hh:60
Bitfield< 4 > pc
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
T letoh(T value)
Definition byteswap.hh:173
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
RefCountingPtr< StaticInst > StaticInstPtr

Generated on Mon May 26 2025 09:18:58 for gem5 by doxygen 1.13.2