OpenASIP 2.2
Loading...
Searching...
No Matches
RISCVTools.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2024 Tampere University.
3
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.
8
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.
13
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
17 */
18/**
19 * @file RISCVTools.icc
20 *
21 * Implementation of RISCVTools class.
22 *
23 * @author Kari Hepola 2024 (kari.hepola@tuni.fi)
24 * @note rating: red
25 */
26
27#include <bitset>
28#include "MapTools.hh"
29
30inline int
31RISCVTools::getFunc3Int(const int encoding) {
32 const int mask = 0b1110000000;
33 const int enc = ((encoding & mask) >> 7);
34 return enc;
35}
36
37inline int
38RISCVTools::getFunc7Int(const int encoding) {
39 const int mask = 0b11111110000000000;
40 const int enc = ((encoding & mask) >> 10);
41 return enc;
42}
43
44inline int
45RISCVTools::getFunc2Int(const int encoding) {
46 const int mask = 0b11000000000000000;
47 const int enc = ((encoding & mask) >> 15);
48 return enc;
49}
50
51inline int
52RISCVTools::getOpcodeInt(const int encoding) {
53 const int mask = 0b1111111;
54 const int enc = encoding & mask;
55 return enc;
56}
57
58inline std::string
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);
63 return "0b" + encStr;
64}
65
66
67inline std::string
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);
72 return "0b" + encStr;
73}
74
75inline std::string
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);
80 return "0b" + encStr;
81}
82
83inline std::string
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);
88 return "0b" + encStr;
89}
90
91void
92RISCVTools::findCustomOps(std::map<std::string, int>& customOps_, BinaryEncoding* bem_) {
93 customOps_.clear();
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
99 };
100 for (const std::string& fName : formatsToSearch) {
101 InstructionFormat* format = bem_->instructionFormat(fName);
102 if (format == NULL) {
103 continue;
104 }
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)});
109 }
110 }
111 }
112}
113
114/**
115 * @param instruction the whole opcode, as it is found in the objdump
116 * @return Struct of decoded values. Register values are discarded.
117 */
118inline R4Instruction
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};
125}
126