-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscode.hpp
More file actions
146 lines (135 loc) · 3.35 KB
/
transcode.hpp
File metadata and controls
146 lines (135 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef TRANSCODE
#define TRANSCODE
#include "enum.hpp"
#include "command.hpp"
#include "ultility.hpp"
#include "storage.hpp"
_com* transcode(int cur) {
int opcode = get(0, 6, cur);
int funct3, funct7;
switch (opcode) {
case 0b0110111: return new _com_U(cur, LUI);
case 0b0010111: return new _com_U(cur, AUIPC);
case 0b1101111: return new _com_UJ(cur, JAL);
case 0b1100111: return new _com_I(cur, JALR);
case 0b0000011:
funct3 = get(12, 14, cur);
switch (funct3) {
case 0b000: return new _com_I(cur, LB);
case 0b001: return new _com_I(cur, LH);
case 0b010: return new _com_I(cur, LW);
case 0b100: return new _com_I(cur, LBU);
case 0b101: return new _com_I(cur, LHU);
}
case 0b1100011:
funct3 = get(12, 14, cur);
switch (funct3) {
case 0b000: return new _com_SB(cur, BEQ);
case 0b001: return new _com_SB(cur, BNE);
case 0b100: return new _com_SB(cur, BLT);
case 0b101: return new _com_SB(cur, BGE);
case 0b110: return new _com_SB(cur, BLTU);
case 0b111: return new _com_SB(cur, BGEU);
}
case 0b0100011:
funct3 = get(12, 14, cur);
switch (funct3) {
case 0b000: return new _com_S(cur, SB);
case 0b001: return new _com_S(cur, SH);
case 0b010: return new _com_S(cur, SW);
}
case 0b0110011:
funct3 = get(12, 14, cur);
switch (funct3) {
case 0b000:
funct7 = get(25, 31, cur);
switch (funct7) {
case 0b0000000: return new _com_R(cur, ADD);
case 0b0100000: return new _com_R(cur, SUB);
}
case 0b001: return new _com_R(cur, SLL);
case 0b010: return new _com_R(cur, SLT);
case 0b011: return new _com_R(cur, SLTU);
case 0b100: return new _com_R(cur, XOR);
case 0b101:
funct7 = get(25, 31, cur);
switch (funct7) {
case 0b0000000: return new _com_R(cur, SRL);
case 0b0100000: return new _com_R(cur, SRA);
}
case 0b110: return new _com_R(cur, OR);
case 0b111: return new _com_R(cur, AND);
}
case 0b0010011:
funct3 = get(12, 14, cur);
switch (funct3) {
case 0b000: return new _com_I(cur, ADDI);
case 0b010: return new _com_I(cur, SLTI);
case 0b011: return new _com_I(cur, SLTIU);
case 0b100: return new _com_I(cur, XORI);
case 0b110: return new _com_I(cur, ORI);
case 0b111: return new _com_I(cur, ANDI);
case 0b001: return new _com_I(cur, SLLI);
case 0b101:
funct7 = get(25, 31, cur);
switch (funct7) {
case 0b0000000: return new _com_I(cur, SRLI);
case 0b0100000: return new _com_I(cur, SRAI);
}
}
default:
return new _com(0);
}
}
bool isBranch(int command) {
int opcode = get(0, 6, command);
switch (opcode) {
case 0b1100011: case 0b1101111: case 0b1100111:
return true;
default:
return false;
}
}
bool isB(int command) {
int opcode = get(0, 6, command);
switch (opcode) {
case 0b1100011:
return true;
default:
return false;
}
}
bool isJ(int command) {
int opcode = get(0, 6, command);
switch (opcode) {
case 0b1101111:
return true;
default:
return false;
}
}
bool isJR(int command) {
int opcode = get(0, 6, command);
switch (opcode) {
case 0b1100111:
return true;
default:
return false;
}
}
bool spjdg(int command) {
int opcode = get(0, 6, command);
switch (opcode) {
case 0b0110111:
case 0b0010111:
case 0b1101111:
case 0b1100111:
case 0b0000011:
case 0b0110011:
case 0b0010011:
return true;
default:
return false;
}
}
#endif // !TRANSCODE