gem5 v23.0.0.0
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/types.hh"
32#include "base/bitfield.hh"
33#include "debug/Decode.hh"
34
35namespace gem5
36{
37
38namespace RiscvISA
39{
40
42{
43 aligned = true;
44 mid = false;
45 machInst = 0;
46 emi = 0;
47}
48
49void
51{
52 // The MSB of the upper and lower halves of a machine instruction.
53 constexpr size_t max_bit = sizeof(machInst) * 8 - 1;
54 constexpr size_t mid_bit = sizeof(machInst) * 4 - 1;
55
56 auto inst = letoh(machInst);
57 DPRINTF(Decode, "Requesting bytes 0x%08x from address %#x\n", inst,
58 fetchPC);
59
60 bool aligned = pc.instAddr() % sizeof(machInst) == 0;
61 if (aligned) {
62 emi.instBits = inst;
63 if (compressed(inst))
64 emi.instBits = bits(inst, mid_bit, 0);
66 instDone = true;
67 } else {
68 if (mid) {
69 assert(bits(emi.instBits, max_bit, mid_bit + 1) == 0);
70 replaceBits(emi.instBits, max_bit, mid_bit + 1, inst);
71 mid = false;
72 outOfBytes = false;
73 instDone = true;
74 } else {
75 emi.instBits = bits(inst, max_bit, mid_bit + 1);
76 mid = !compressed(emi);
77 outOfBytes = true;
79 }
80 }
81}
82
85{
86 DPRINTF(Decode, "Decoding instruction 0x%08x at address %#x\n",
87 mach_inst.instBits, addr);
88
89 StaticInstPtr &si = instMap[mach_inst];
90 if (!si)
91 si = decodeInst(mach_inst);
92
93 DPRINTF(Decode, "Decode: Decoded %s instruction: %#x\n",
94 si->getName(), mach_inst);
95 return si;
96}
97
100{
101 if (!instDone)
102 return nullptr;
103 instDone = false;
104
105 auto &next_pc = _next_pc.as<PCState>();
106
107 if (compressed(emi)) {
108 next_pc.npc(next_pc.instAddr() + sizeof(machInst) / 2);
109 next_pc.compressed(true);
110 } else {
111 next_pc.npc(next_pc.instAddr() + sizeof(machInst));
112 next_pc.compressed(false);
113 }
114
115 emi.rv_type = static_cast<int>(next_pc.rvType());
116 return decode(emi, next_pc.instAddr());
117}
118
119} // namespace RiscvISA
120} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
Target & as()
Definition pcstate.hh:72
StaticInstPtr decodeInst(ExtMachInst mach_inst)
void moreBytes(const PCStateBase &pc, Addr fetchPC) override
Feed data to the decoder.
Definition decoder.cc:50
void reset() override
Definition decoder.cc:41
StaticInstPtr decode(ExtMachInst mach_inst, Addr addr)
Decode a machine instruction.
Definition decoder.cc:84
decode_cache::InstMap< ExtMachInst > instMap
Definition decoder.hh:53
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:76
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:213
Bitfield< 6 > si
Bitfield< 61 > compressed
Definition types.hh:61
Bitfield< 4 > pc
Bitfield< 3 > addr
Definition types.hh:84
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
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 Jul 10 2023 14:24:26 for gem5 by doxygen 1.9.7