gem5 v24.0.0.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"
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 reset();
48}
49
51{
52 aligned = true;
53 mid = false;
54 machInst = 0;
55 emi = 0;
56}
57
58void
60{
61 // The MSB of the upper and lower halves of a machine instruction.
62 constexpr size_t max_bit = sizeof(machInst) * 8 - 1;
63 constexpr size_t mid_bit = sizeof(machInst) * 4 - 1;
64
65 auto inst = letoh(machInst);
66 DPRINTF(Decode, "Requesting bytes 0x%08x from address %#x\n", inst,
67 fetchPC);
68
69 bool aligned = pc.instAddr() % sizeof(machInst) == 0;
70 if (aligned) {
71 emi.instBits = inst;
72 if (compressed(inst))
73 emi.instBits = bits(inst, mid_bit, 0);
75 instDone = true;
76 } else {
77 if (mid) {
78 assert(bits(emi.instBits, max_bit, mid_bit + 1) == 0);
79 replaceBits(emi.instBits, max_bit, mid_bit + 1, inst);
80 mid = false;
81 outOfBytes = false;
82 instDone = true;
83 } else {
84 emi.instBits = bits(inst, max_bit, mid_bit + 1);
85 mid = !compressed(emi);
86 outOfBytes = true;
88 }
89 }
90}
91
94{
95 DPRINTF(Decode, "Decoding instruction 0x%08x at address %#x\n",
96 mach_inst.instBits, addr);
97
98 StaticInstPtr &si = instMap[mach_inst];
99 if (!si)
100 si = decodeInst(mach_inst);
101
102 si->size(compressed(mach_inst) ? 2 : 4);
103
104 DPRINTF(Decode, "Decode: Decoded %s instruction: %#x\n",
105 si->getName(), mach_inst);
106 return si;
107}
108
111{
112 if (!instDone)
113 return nullptr;
114 instDone = false;
115
116 auto &next_pc = _next_pc.as<PCState>();
117
118 if (compressed(emi)) {
119 next_pc.npc(next_pc.instAddr() + sizeof(machInst) / 2);
120 next_pc.compressed(true);
121 } else {
122 next_pc.npc(next_pc.instAddr() + sizeof(machInst));
123 next_pc.compressed(false);
124 }
125
126 emi.vl = next_pc.vl();
127 emi.vtype8 = next_pc.vtype() & 0xff;
128 emi.vill = next_pc.vtype().vill;
129 emi.rv_type = static_cast<int>(next_pc.rvType());
130
131 return decode(emi, next_pc.instAddr());
132}
133
134} // namespace RiscvISA
135} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
Target & as()
Definition pcstate.hh:73
void moreBytes(const PCStateBase &pc, Addr fetchPC) override
Feed data to the decoder.
Definition decoder.cc:59
void reset() override
Definition decoder.cc:50
StaticInstPtr decode(ExtMachInst mach_inst, Addr addr)
Decode a machine instruction.
Definition decoder.cc:93
virtual StaticInstPtr decodeInst(ExtMachInst mach_inst)
Decoder(const RiscvDecoderParams &p)
Definition decoder.cc:42
decode_cache::InstMap< ExtMachInst > instMap
Definition decoder.hh:54
unsigned getVecElemLenInBits()
Definition isa.hh:167
unsigned getVecLenInBits()
Methods for getting VLEN, VLENB and ELEN values.
Definition isa.hh:165
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 - Pranith Kumar Copyright (c) 2020 Inria 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 Tue Jun 18 2024 16:23:57 for gem5 by doxygen 1.11.0