forked from 21-cpu-group2/sim_binary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisassembler.cpp
More file actions
341 lines (332 loc) · 12 KB
/
disassembler.cpp
File metadata and controls
341 lines (332 loc) · 12 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#define REG_SIZE 32
//////// opecode start /////////
// RV32I
#define _BRANCH 0b1100011 // beq, bne, blt, bge, bltu, bgeu
#define _LOAD 0b0000011 // lb, lh, lw, lbu, lhu,
#define _STORE 0b0100011 // sb, sh, sw
#define _IMM 0b0010011 // addi, slti, sltiu, xori, ori, andi, slli, srli, srai
#define _OP 0b0110011 // add, sub, sll, slt, sltu, xor, srl, sra, or, and
#define _LUI 0b0110111 // lui
#define _AUIPC 0b0010111 // auipc
#define _JAL 0b1101111 // jal
#define _JALR 0b1100111 // jalr
#define _NOP 0b1111111 // nop
// RV32F
#define _FLW 0b0000111 // flw
#define _FSW 0b0100111 // fsw
#define _FMADDS 0b1000011 // fmadd.s
#define _FMSUBS 0b1000111 // fmsub.s
#define _FNMSUBS 0b1001011 // fnmsub.s
#define _FNMADDS 0b1001111 // fnmadd.s
#define _FOP 0b1010011 // fadd.s, fsub.s, fmul.s, fdiv.s, fsqrt.s, fsgnj.s, fsgnjn.s, fsgnnjx.s,
// fmin.s, fmax.s, fcvt.w.s, fcvt.wu.s, fmv.x.w, feq.s, flt.s, fle.s,
// fclass.s, fcvt.s.w, fcvt.s.wu, fmv.w.x
//////// opecode end /////////
using namespace std;
static char reg_name[REG_SIZE][16] = {
"%zero","%ra","%sp","%mc_hp",
"%in","%out","%a0","%a1",
"%a2","%a3","%a4","%a5",
"%a6","%a7","%a8","%a9",
"%a10","%a11","%a12","%fzero",
"%f0","%f1","%f2","%f3",
"%f4","%f5","%f6","%f7",
"%f8","%f9","%f10","%f11",
};
uint32_t bin2int(string bin) {
// std::stoiがうまく機能しないので自分で実装。
int ofst = (bin.at(1) == 'b') ? 2 : 0;
uint32_t ret = 0x00000000;
for (int i=0; i<bin.size()-ofst; i++){
ret <<= 1;
if (bin.at(i + ofst) == '1'){
ret += 1;
}
}
return ret;
}
int disassemble_one_instruction(uint32_t instruction){
uint32_t opcode = instruction & 0x007F;
if (opcode == _BRANCH){
uint32_t funct3 = (instruction & 0x00007000) >> 12;
uint32_t imm1 = (instruction & 0xFE000000) >> 20;
uint32_t imm2 = (instruction & 0x00000F80) >> 7;
int imm = imm1 + imm2;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
switch (funct3) {
case 0b000 :
cout << "beq " << reg_name[rs1] << " " << reg_name[rs2] << " " << imm;
break;
case 0b100 : // in risc-v 0'b001
cout << "bne " << reg_name[rs1] << " " << reg_name[rs2] << " " << imm;
break;
case 0b001 : // in risc-v 0'b100
cout << "blt " << reg_name[rs1] << " " << reg_name[rs2] << " " << imm;
break;
case 0b101 :
cout << "bge " << reg_name[rs1] << " " << reg_name[rs2] << " " << imm;
break;
default :
cout << "no function matched";
return 1;
}
}
else if (opcode == _LOAD){
uint32_t funct3 = (instruction & 0x00007000) >> 12;
int imm = (instruction & 0xFFF00000) >> 20;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
switch (funct3) {
case 0b010 :
cout << "lw " << reg_name[rd] << " " << reg_name[rs1] << " " << imm;
break;
default :
cout << "no function matched";
return 1;
}
}
else if (opcode == _STORE){
uint32_t funct3 = (instruction & 0x00007000) >> 12;
uint32_t imm1 = (instruction & 0xFE000000) >> 20;
uint32_t imm2 = (instruction & 0x00000F80) >> 7;
int imm = imm1 + imm2;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
switch (funct3) {
case 0b010 :
cout << "sw " << reg_name[rs2] << " " << reg_name[rs1] << " " << imm;
break;
default :
cout << "no function matched";
return 1;
}
}
else if (opcode == _IMM){
uint32_t funct3 = (instruction & 0x00007000) >> 12;
int imm = (instruction & 0xFFF00000) >> 20;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t shamt = (uint32_t)(imm & 0x0000001F);
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
switch (funct3) {
case 0b000 :
cout << "addi " << reg_name[rd] << " " << reg_name[rs1] << " " << imm;
break;
case 0b001 :
cout << "slli " << reg_name[rd] << " " << reg_name[rs1] << " " << imm;
break;
case 0b101 :
cout << "srli " << reg_name[rd] << " " << reg_name[rs1] << " " << imm;
break;
default :
cout << "no function matched";
return 1;
}
}
else if (opcode == _OP){
uint32_t funct3 = (instruction & 0x00007000) >> 12;
uint32_t funct7 = (instruction & 0xFE000000) >> 22;
uint32_t funct = funct7 + funct3;
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
switch (funct) {
// 下位10bit(7bit->funct7, 3bit->funct3)
case 0b0000000000 :
cout << "add " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2];
break;
case 0b0100000000 :
cout << "sub " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2];
break;
case 0b0000000001 :
cout << "sll " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2];
break;
default :
cout << "no function matched";
return 1;
}
}
else if (opcode == _LUI){
int imm = instruction & 0xFFFFF000;
uint32_t rd = (instruction & 0x00000F80) >> 7;
cout << "lui " << reg_name[rd] << " " << imm;
}
else if (opcode == _JAL){
int imm = (instruction & 0xFFFFF000) >> 12;
if (imm & (1<<19)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFF00000);
}
uint32_t rd = (instruction & 0x00000F80) >> 7;
cout << "jal " << reg_name[rd] << " " << imm;
}
else if (opcode == _JALR){
uint32_t funct3 = (instruction & 0x00007000) >> 12;
int imm = (instruction & 0xFFF00000) >> 20;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
switch (funct3) {
case 0b000 :
cout << "jalr " << reg_name[rd] << " " << reg_name[rs1] << " " << imm;
break;
default :
cout << "no function matched";
return 1;
}
}
else if (opcode == _FOP){
uint32_t rm = (instruction & 0x00007000) >> 12;
uint32_t funct7 = (instruction & 0xFE000000) >> 22;
uint32_t funct = funct7 + rm;
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
switch (funct) {
// 上位7bit->funct7, 下位3bit->funct3
case 0b0000000000 :
cout << "fadd " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2];
break;
case 0b0000100000 :
cout << "fsub " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2];
break;
case 0b0001000000 :
cout << "fmul " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2];
break;
case 0b0001100000 :
cout << "fdiv " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2];
break;
case 0b0001000001:
if (rs2 != 0){
cout << "no function matched";
return 1;
}
cout << "fhalf " << reg_name[rd] << " " << reg_name[rs1];
break;
case 0b0101100000:
if (rs2 != 0){
cout << "no function matched";
return 1;
}
cout << "sqrt " << reg_name[rd] << " " << reg_name[rs1];
break;
case 0b0010000010:
if (rs2 != 0){
cout << "no function matched";
return 1;
}
cout << "fabs " << reg_name[rd] << " " << reg_name[rs1];
break;
case 0b0010000001:
if (rs2 != 0){
cout << "no function matched";
return 1;
}
cout << "fneg " << reg_name[rd] << " " << reg_name[rs1];
break;
case 0b1010000010 :
if (rs2 != 0b00000){
cout << "no function matched";
return 1;
}
cout << "fiszero " << reg_name[rd] << " " << reg_name[rs1] << " ";
break;
case 0b1010000101 :
if (rs2 != 0b00000){
cout << "no function matched";
return 1;
}
cout << "fisneg " << reg_name[rd] << " " << reg_name[rs1] << " ";
break;
case 0b1010000011 :
if (rs2 != 0b00000){
cout << "no function matched";
return 1;
}
cout << "fispos " << reg_name[rd] << " " << reg_name[rs1] << " ";
break;
case 0b1010000001 :
cout << "fless " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2];
break;
case 0b1100000001 :
if (rs2 != 0b00000){
cout << "no function matched";
return 1;
}
cout << "floor " << reg_name[rd] << " " << reg_name[rs1] << " ";
break;
case 0b1100000000 :
if (rs2 != 0b00000){
cout << "no function matched";
return 1;
}
cout << "ftoi " << reg_name[rd] << " " << reg_name[rs1] << " ";
break;
case 0b1101000000 :
if (rs2 != 0b00000){
cout << "no function matched";
return 1;
}
cout << "itof " << reg_name[rd] << " " << reg_name[rs1] << " ";
break;
default :
cout << "no function matched";
return 1;
}
}
else if (opcode == _NOP){
if (instruction & 0xFFFFFFFF) {
cout << "nop ";
}
}
else {
cout << "no function matched";
return 1;
}
return 0;
}
int disassemble_instructions(string file_path) {
ifstream ifs(file_path);
if (ifs.fail()){
return 1;
}
string str;
int pc = 0;
while (getline(ifs, str)){
uint32_t inst = bin2int(str);
disassemble_one_instruction(inst);
cout << " # pc = " << pc << endl;
pc++;
}
return 0;
}
int main(int argc, char** argv) {
string file_path = argv[1];
if (disassemble_instructions(file_path)){
cout << "cannot open the file" << endl;
}
return 0;
}