gem5 v24.1.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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"
31#include "arch/riscv/isa.hh"
32#include "arch/riscv/types.hh"
33#include "base/bitfield.hh"
34#include "debug/Decode.hh"
35
36namespace gem5
37{
38
39namespace RiscvISA
40{
41
42Decoder::Decoder(const RiscvDecoderParams &p) : InstDecoder(p, &machInst)
43{
44 ISA *isa = dynamic_cast<ISA*>(p.isa);
45 vlen = isa->getVecLenInBits();
47 _enableZcd = isa->enableZcd();
48 reset();
49}
50
52{
53 aligned = true;
54 mid = false;
55 machInst = 0;
56 emi = 0;
57}
58
59void
61{
62 // The MSB of the upper and lower halves of a machine instruction.
63 constexpr size_t max_bit = sizeof(machInst) * 8 - 1;
64 constexpr size_t mid_bit = sizeof(machInst) * 4 - 1;
65
66 auto inst = letoh(machInst);
67 DPRINTF(Decode, "Requesting bytes 0x%08x from address %#x\n", inst,
68 fetchPC);
69
70 bool aligned = pc.instAddr() % sizeof(machInst) == 0;
71 if (aligned) {
72 emi.instBits = inst;
73 if (compressed(inst))
74 emi.instBits = bits(inst, mid_bit, 0);
76 instDone = true;
77 } else {
78 if (mid) {
79 assert(bits(emi.instBits, max_bit, mid_bit + 1) == 0);
80 replaceBits(emi.instBits, max_bit, mid_bit + 1, inst);
81 mid = false;
82 outOfBytes = false;
83 instDone = true;
84 } else {
85 emi.instBits = bits(inst, max_bit, mid_bit + 1);
86 mid = !compressed(emi);
87 outOfBytes = true;
89 }
90 }
91}
92
95{
96 DPRINTF(Decode, "Decoding instruction 0x%08x at address %#x\n",
97 mach_inst.instBits, addr);
98
99 StaticInstPtr &si = instMap[mach_inst];
100 if (!si)
101 si = decodeInst(mach_inst);
102
103 si->size(compressed(mach_inst) ? 2 : 4);
104
105 DPRINTF(Decode, "Decode: Decoded %s instruction: %#x\n",
106 si->getName(), mach_inst);
107 return si;
108}
109
112{
113 if (!instDone)
114 return nullptr;
115 instDone = false;
116
117 auto &next_pc = _next_pc.as<PCState>();
118
119 if (compressed(emi)) {
120 next_pc.npc(next_pc.instAddr() + sizeof(machInst) / 2);
121 next_pc.compressed(true);
122 } else {
123 next_pc.npc(next_pc.instAddr() + sizeof(machInst));
124 next_pc.compressed(false);
125 }
126
127 emi.vl = next_pc.vl();
128 emi.vtype8 = next_pc.vtype() & 0xff;
129 emi.vill = next_pc.vtype().vill;
130 emi.rv_type = static_cast<int>(next_pc.rvType());
131 emi.enable_zcd = _enableZcd;
132
133 return decode(emi, next_pc.instAddr());
134}
135
136} // namespace RiscvISA
137} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
Target & as()
Definition pcstate.hh:73
void moreBytes(const PCStateBase &pc, Addr fetchPC) override
Feed data to the decoder.
Definition decoder.cc:60
void reset() override
Definition decoder.cc:51
StaticInstPtr decode(ExtMachInst mach_inst, Addr addr)
Decode a machine instruction.
Definition decoder.cc:94
virtual StaticInstPtr decodeInst(ExtMachInst mach_inst)
Decoder(const RiscvDecoderParams &p)
Definition decoder.cc:42
decode_cache::InstMap< ExtMachInst > instMap
Definition decoder.hh:54
bool enableZcd()
Definition isa.hh:192
unsigned getVecElemLenInBits()
Definition isa.hh:184
unsigned getVecLenInBits()
Methods for getting VLEN, VLENB and ELEN values.
Definition isa.hh:182
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 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
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

Generated on Mon Jan 13 2025 04:28:21 for gem5 by doxygen 1.9.8