2 Copyright (C) 2024 Tampere University.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * @file RISCVTools.icc
21 * Implementation of RISCVTools class.
23 * @author Kari Hepola 2024 (kari.hepola@tuni.fi)
31RISCVTools::getFunc3Int(const int encoding) {
32 const int mask = 0b1110000000;
33 const int enc = ((encoding & mask) >> 7);
38RISCVTools::getFunc7Int(const int encoding) {
39 const int mask = 0b11111110000000000;
40 const int enc = ((encoding & mask) >> 10);
45RISCVTools::getFunc2Int(const int encoding) {
46 const int mask = 0b11000000000000000;
47 const int enc = ((encoding & mask) >> 15);
52RISCVTools::getOpcodeInt(const int encoding) {
53 const int mask = 0b1111111;
54 const int enc = encoding & mask;
59RISCVTools::getFunc3Str(const int encoding) {
60 const int enc = getFunc3Int(encoding);
61 const std::string encStr = std::bitset<3>(enc).to_string();
62 assert(encStr.length() == 3);
68RISCVTools::getFunc7Str(const int encoding) {
69 const int enc = getFunc7Int(encoding);
70 const std::string encStr = std::bitset<7>(enc).to_string();
71 assert(encStr.length() == 7);
76RISCVTools::getFunc2Str(const int encoding) {
77 const int enc = getFunc7Int(encoding);
78 const std::string encStr = std::bitset<2>(enc).to_string();
79 assert(encStr.length() == 2);
84RISCVTools::getOpcodeStr(const int encoding) {
85 const int enc = getOpcodeInt(encoding);
86 const std::string encStr = std::bitset<7>(enc).to_string();
87 assert(encStr.length() == 7);
92RISCVTools::findCustomOps(std::map<std::string, int>& customOps_, BinaryEncoding* bem_) {
94 const std::vector<std::string> formatsToSearch = {
95 RISCVFields::RISCV_R_TYPE_NAME,
96 RISCVFields::RISCV_R1R_TYPE_NAME,
97 RISCVFields::RISCV_R1_TYPE_NAME,
98 RISCVFields::RISCV_R3R_TYPE_NAME
100 for (const std::string& fName : formatsToSearch) {
101 InstructionFormat* format = bem_->instructionFormat(fName);
102 if (format == NULL) {
105 for (int i = 0; i < format->operationCount(); i++) {
106 const std::string op = format->operationAtIndex(i);
107 if (!MapTools::containsKey(RISCVFields::RISCVRTypeOperations, op)) {
108 customOps_.insert({op, format->encoding(op)});
115 * @param instruction the whole opcode, as it is found in the objdump
116 * @return Struct of decoded values. Register values are discarded.
119RISCVTools::decodeR4Instruction(const uint32_t instruction) {
120 int baseopcode = instruction & 0x7F;
121 int funct3 = (instruction >> 12) & 0x7;
122 int funct7 = (instruction >> 25) & 0x7F;
123 int funct2 = (instruction >> 25) & 0x3;
124 return {baseopcode, funct3, funct7, funct2};