-
Notifications
You must be signed in to change notification settings - Fork 1
/
Disassembler.cpp
412 lines (406 loc) · 13.6 KB
/
Disassembler.cpp
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//
// Created by moiz on 5/16/20.
//
#include "Disassembler.h"
#include <ios>
#include <sstream>
#include "instruction_encdec.h"
namespace RISCV_EMULATOR {
std::string GetRegName(int reg) {
std::array<std::string, 32> kRegNames = {
"ZERO", "RA", "SP", "GP", "TP", "T0", "T1", "T2", "FP", "S1", "A0",
"A1", "A2", "A3", "A4", "A5", "A6", "A7", "S2", "S3", "S4", "S5",
"S6", "S7", "S8", "S9", "S10", "S11", "T3", "T4", "T5", "T6",
};
return kRegNames[reg];
}
std::string NumberToHex(uint32_t num, int length = 0) {
std::stringstream s;
s << "0x" << std::hex << num;
return std::string(s.str());
}
// TODO: Add tests for disassembly;
// Disassemble16 and GetCode16 have very similar logic. Should be combined in
// some way.
std::string Disassemble16(uint32_t ir, int mxl = 1) {
std::string cmd = "Unsupported C Instruction";
uint32_t instruction, rd, rs1, rs2;
int32_t imm;
RiscvCpu::GetCode16(ir, mxl, &instruction, &rd, &rs1, &rs2, &imm);
uint16_t opcode = (bitcrop(ir, 3, 13) << 2) | bitcrop(ir, 2, 0);
switch (opcode) {
case 0b00000:
cmd = "C.ADDI4SPN " + GetRegName(rd) + ", SP, " + NumberToHex(imm);
break;
case 0b00001:
cmd = "C.ADDI " + GetRegName(rd) + ", " + NumberToHex(imm);
break;
case 0b00010:
cmd = "C.SLLI " + GetRegName(rd) + ", " + NumberToHex(imm);
break;
case 0b00101:
if (mxl == 1) {
cmd = "C.JAL " + NumberToHex(SignExtend(imm, 12));
} else {
cmd = "C.ADDIW " + GetRegName(rd) + ", " + NumberToHex(imm);
}
break;
case 0b01000:
cmd = "C.LW " + GetRegName(rd) + ", " + NumberToHex(imm) + "(" +
GetRegName(rs1) + ")";
break;
case 0b01001:
cmd = "C.LI " + GetRegName(rd) + ", " + NumberToHex(imm);
break;
case 0b01010:
cmd = "C.LWSP " + GetRegName(rd) + ", " + NumberToHex(imm) + "(SP)";
break;
case 0b01100:
cmd = "C.LD " + GetRegName(rd) + ", " + NumberToHex(imm) + "(" +
GetRegName(rs1) + ")";
break;
case 0b01101:
if (bitcrop(ir, 5, 7) == 0b00010) {
// c.addi16sp.
cmd = "C.ADDI16SP SP, SP, " + NumberToHex(imm);
} else {
cmd = "C.LUI " + GetRegName(rd) + ", " + NumberToHex(imm);
}
break;
case 0b01110:
cmd = "C.LDSP " + GetRegName(rd) + ", " + NumberToHex(imm) + "(SP)";
break;
case 0b10010: // c.add
if (bitcrop(ir, 1, 12) == 1) {
if (bitcrop(ir, 5, 2) == 0 && bitcrop(ir, 5, 7) == 0) {
cmd = "C.EBREAK";
} else if (bitcrop(ir, 5, 2) == 0) {
cmd = "C.JALR " + GetRegName(rs1);
} else if (bitcrop(ir, 5, 7) != 0) {
cmd = "C.ADD " + GetRegName(rd) + ", " + GetRegName(rs2);
}
} else if (bitcrop(ir, 5, 2) == 0) {
cmd = "C.JR " + GetRegName(rs1);
} else {
cmd = "C.MV " + GetRegName(rd) + ", " + GetRegName(rs2);
}
break;
case 0b10001:
if (bitcrop(ir, 3, 10) == 0b011 && bitcrop(ir, 2, 5) == 0b11) {
// c.and.
cmd = "C.AND " + GetRegName(rd) + ", " + GetRegName(rs2);
} else if (bitcrop(ir, 3, 10) == 0b011 && bitcrop(ir, 2, 5) == 0b01) {
cmd = "C.XOR " + GetRegName(rd) + ", " + GetRegName(rs2);
} else if (bitcrop(ir, 3, 10) == 0b011 && bitcrop(ir, 2, 5) == 0b00) {
cmd = "C.SUB " + GetRegName(rd) + ", " + GetRegName(rs2);
} else if (bitcrop(ir, 3, 10) == 0b011 && bitcrop(ir, 2, 5) == 0b10) {
cmd = "C.OR " + GetRegName(rd) + ", " + GetRegName(rs2);
} else if (bitcrop(ir, 3, 10) == 0b111 && bitcrop(ir, 2, 5) == 0b01) {
cmd = "C.ADDW " + GetRegName(rd) + ", " + GetRegName(rs2);
} else if (bitcrop(ir, 3, 10) == 0b111 && bitcrop(ir, 2, 5) == 0b00) {
cmd = "C.SUBW " + GetRegName(rd) + ", " + GetRegName(rs2);
} else if (bitcrop(ir, 2, 10) == 0b01) {
cmd = "C.SRAI " + GetRegName(rd) + ", " + NumberToHex(imm);
} else if (bitcrop(ir, 2, 10) == 0b00) {
cmd = "C.SRLI " + GetRegName(rd) + ", " + NumberToHex(imm);
} else if (bitcrop(ir, 2, 10) == 0b10) {
cmd = "C.ANDI " + GetRegName(rd) + ", " +
NumberToHex(SignExtend(imm, 6));
}
break;
case 0b10101:
cmd = "C.J " + NumberToHex(SignExtend(imm, 12));
break;
case 0b11000:
cmd = "C.SW " + GetRegName(rs2) + ", " + NumberToHex(imm) + "(" +
GetRegName(rs1) + ")";
case 0b11001:
cmd = "C.BEQZ " + GetRegName(rs1) + ", " +
NumberToHex(SignExtend(imm, 9));
break;
case 0b11010:
cmd = "C.SWSP " + GetRegName(rs2) + ", " + NumberToHex(imm) + "(SP)";
break;
case 0b11100:
cmd = "C.SD " + GetRegName(rs2) + ", " + NumberToHex(imm) + "(" +
GetRegName(rs1) + ")";
break;
case 0b11101:
cmd = "C.BNEZ " + GetRegName(rs1) + ", " +
NumberToHex(SignExtend(imm, 9));
break;
case 0b11110:
cmd = "C.SDSP " + GetRegName(rs2) + ", " + NumberToHex(imm) + "(SP)";
}
return cmd;
}
std::string Disassemble(uint32_t ir, int mxl) {
uint16_t opcode = bitcrop(ir, 7, 0);
if ((opcode & 0b11) != 0b11) {
return Disassemble16(ir, mxl);
}
uint8_t funct3 = bitcrop(ir, 3, 12);
uint8_t funct7 = bitcrop(ir, 7, 25);
uint8_t funct5 = funct7 >> 2;
uint32_t rd = GetRd(ir);
uint32_t rs1 = GetRs1(ir);
uint32_t rs2 = GetRs2(ir);
int16_t imm12 = GetImm12(ir);
int16_t csr = GetCsr(ir);
int16_t imm13 = GetImm13(ir);
int32_t imm21 = GetImm21(ir);
int16_t imm12_stype = GetStypeImm12(ir);
int32_t imm20 = GetImm20(ir);
std::string cmd = "UNDEF";
switch (opcode) {
case OPCODE_ARITHLOG: // ADD, SUB
if (funct7 == FUNC_NORM || funct7 == FUNC_ALT) {
if (funct3 == FUNC3_ADDSUB) {
cmd = (funct7 == FUNC_NORM) ? "ADD" : "SUB";
} else if (funct3 == FUNC3_AND) {
cmd = "AND";
} else if (funct3 == FUNC3_OR) {
cmd = "OR";
} else if (funct3 == FUNC3_XOR) {
cmd = "XOR";
} else if (funct3 == FUNC3_SR) {
if (funct7 == FUNC_NORM) {
cmd = "SRL";
} else if (funct7 == FUNC_ALT) {
cmd = "SRA";
}
} else if (funct3 == FUNC3_SL) {
cmd = "SLL";
} else if (funct3 == FUNC3_SLT) {
cmd = "SLT";
} else if (funct3 == FUNC3_SLTU) {
cmd = "SLTU";
}
} else if (funct7 == FUNC_MULT) {
if (funct3 == FUNC3_MUL) {
cmd = "MUL";
} else if (funct3 == FUNC3_MULH) {
cmd = "MULH";
} else if (funct3 == FUNC3_MULHSU) {
cmd = "MULHSU";
} else if (funct3 == FUNC3_MULHU) {
cmd = "MULHU";
} else if (funct3 == FUNC3_DIV) {
cmd = "DIV";
} else if (funct3 == FUNC3_DIVU) {
cmd = "DIVU";
} else if (funct3 == FUNC3_REM) {
cmd = "REM";
} else if (funct3 == FUNC3_REMU) {
cmd = "REMU";
}
cmd += " " + GetRegName(rd) + ", " + GetRegName(rs1) + ", " +
GetRegName(rs2);
break;
case OPCODE_ARITHLOG_64:
if (funct7 == FUNC_NORM || funct7 == FUNC_ALT) {
if (funct3 == FUNC3_ADDSUB) {
cmd = (funct7 == FUNC_NORM) ? "ADDW" : "SUBW";
} else if (funct3 == FUNC3_SL) {
cmd = "SLLW";
} else if (funct3 == FUNC3_SR) {
if (funct7 == FUNC_NORM) {
cmd = "SRLW";
} else if (funct7 == FUNC_ALT) {
cmd = "SRAW";
}
}
} else if (funct7 == FUNC_MULT) {
if (funct3 == FUNC3_MUL) {
cmd = "MULW";
} else if (funct3 == FUNC3_DIVU) {
cmd = "DIVUW";
} else if (funct3 == FUNC3_DIV) {
cmd = "DIVW";
} else if (funct3 == FUNC3_REMU) {
cmd = "REMUW";
} else if (funct3 == FUNC3_REM) {
cmd = "REMW";
}
}
}
cmd += " " + GetRegName(rd) + ", " + GetRegName(rs1) + ", " +
GetRegName(rs2);
break;
case OPCODE_ARITHLOG_I: // ADDI, SUBI
if (funct3 == FUNC3_ADDSUB) {
cmd = "ADDI";
} else if (funct3 == FUNC3_AND) {
cmd = "ANDI";
} else if (funct3 == FUNC3_OR) {
cmd = "ORI";
} else if (funct3 == FUNC3_XOR) {
cmd = "XORI";
} else if (funct3 == FUNC3_SL) {
cmd = "SLLI";
} else if (funct3 == FUNC3_SR) {
if ((funct7 >> 1) == 0b000000) {
cmd = "SRLI";
} else if ((funct7 >> 1) == 0b010000) {
cmd = "SRAI";
}
// If top 6 bits do not match, it's an error.
} else if (funct3 == FUNC3_SLT) {
cmd = "SLTI";
} else if (funct3 == FUNC3_SLTU) {
cmd = "SLTIU";
}
cmd += " " + GetRegName(rd) + ", " + GetRegName(rs1) + ", " +
NumberToHex(imm12);
break;
case OPCODE_ARITHLOG_I64:
if (funct3 == FUNC3_ADDSUB) {
cmd = "ADDIW";
} else if (funct3 == FUNC3_SL) {
cmd = "SLLIW";
} else if (funct3 == FUNC3_SR) {
if ((funct7 >> 1) == 0b000000) {
cmd = "SRLIW";
} else if ((funct7 >> 1) == 0b010000) {
cmd = "SRAIW";
}
}
cmd += " " + GetRegName(rd) + ", " + GetRegName(rs1) + ", " +
NumberToHex(imm12);
break;
case OPCODE_B: // beq, bltu, bge, bne
if (funct3 == FUNC3_BEQ) {
cmd = "BEQ";
} else if (funct3 == FUNC3_BLT) {
cmd = "BLT";
} else if (funct3 == FUNC3_BLTU) {
cmd = "BLTU";
} else if (funct3 == FUNC3_BGE) {
cmd = "BGE";
} else if (funct3 == FUNC3_BGEU) {
cmd = "BGEU";
} else if (funct3 == FUNC3_BNE) {
cmd = "BNE";
}
cmd += " " + GetRegName(rs1) + ", " + GetRegName(rs2) + ", " +
NumberToHex(imm13);
break;
case OPCODE_J: // jal
cmd = "JAL " + GetRegName(rd) + ", " + NumberToHex(imm21);
break;
case OPCODE_JALR: // jalr
if (funct3 == FUNC3_JALR) {
cmd = "JALR " + GetRegName(rd) + ", " + NumberToHex(imm12) + "(" +
GetRegName(rs1) + ")";
}
break;
case OPCODE_LD: // LW
if (funct3 == FUNC3_LSB) {
cmd = "LB";
} else if (funct3 == FUNC3_LSBU) {
cmd = "LBU";
} else if (funct3 == FUNC3_LSH) {
cmd = "LH";
} else if (funct3 == FUNC3_LSHU) {
cmd = "LHU";
} else if (funct3 == FUNC3_LSW) {
cmd = "LW";
} else if (funct3 == FUNC3_LSWU) {
cmd = "LWU";
} else if (funct3 == FUNC3_LSD) {
cmd = "LD";
}
cmd += " " + GetRegName(rd) + ", " + NumberToHex(imm12) + "(" +
GetRegName(rs1) + ")";
break;
case OPCODE_S: // SW
if (funct3 == FUNC3_LSB) {
cmd = "SB";
} else if (funct3 == FUNC3_LSH) {
cmd = "SH";
} else if (funct3 == FUNC3_LSW) {
cmd = "SW";
} else if (funct3 == FUNC3_LSD) {
cmd = "SD";
}
cmd += " " + GetRegName(rs2) + ", " + NumberToHex(imm12_stype) + "(" +
GetRegName(rs1) + ")";
break;
case OPCODE_LUI: // LUI
cmd = "LUI";
cmd += " " + GetRegName(rd) + ", " + NumberToHex(imm20) + " << 12";
break;
case OPCODE_AUIPC: // AUIPC
cmd = "AUIPC";
cmd += " " + GetRegName(rd) + ", " + NumberToHex(imm20) + " << 12";
break;
case OPCODE_SYSTEM: // EBREAK
if (funct3 == FUNC3_SYSTEM) {
if (imm12 == 0) {
cmd = "ECALL";
} else if (imm12 == 1) {
cmd = "EBREAK";
} else if (imm12 == 0b001100000010) {
cmd = "MRET";
} else if (imm12 == 0b000100000010) {
cmd = "SRET";
} else if (((imm12 >> 5) == 0b0001001) && (rd == 0b00000)) {
cmd = "sfence.vma";
} else {
cmd = "Undefined System Instruction";
}
} else {
if (funct3 == FUNC3_CSRRC) {
cmd = "CSRRC";
} else if (funct3 == FUNC3_CSRRCI) {
cmd = "CSRRCI";
} else if (funct3 == FUNC3_CSRRS) {
cmd = "CSRRS";
} else if (funct3 == FUNC3_CSRRSI) {
cmd = "CSRRSI";
} else if (funct3 == FUNC3_CSRRW) {
cmd = "CSRRW";
} else if (funct3 == FUNC3_CSRRWI) {
cmd = "CSRRWI";
}
cmd += " " + GetRegName(rd) + ", " + NumberToHex(csr) + ", " +
GetRegName(rs1);
}
break;
case OPCODE_FENCE:
if (funct3 == FUNC3_FENCEI) {
cmd = "FENCEI";
} else if (funct3 == FUNC3_FENCE) {
cmd = "FENCE";
}
break;
case OPCODE_AMO:
if (funct5 == FUNC5_AMOADD) {
cmd = funct3 == FUNC3_AMOD ? "AMOADD.D" : "AMOADD.W";
} else if (funct5 == FUNC5_AMOAND) {
cmd = funct3 == FUNC3_AMOD ? "AMOAND.D" : "AMOAND.W";
} else if (funct5 == FUNC5_AMOMAX) {
cmd = funct3 == FUNC3_AMOD ? "AMOMAX.D" : "AMOMAX.W";
} else if (funct5 == FUNC5_AMOMAXU) {
cmd = funct3 == FUNC3_AMOD ? "AMOMAXU.D" : "AMOMAXU.W";
} else if (funct5 == FUNC5_AMOMIN) {
cmd = funct3 == FUNC3_AMOD ? "AMOMIN.D" : "AMOMIN.W";
} else if (funct5 == FUNC5_AMOMINU) {
cmd = funct3 == FUNC3_AMOD ? "AMOMINU.D" : "AMOMAIN.W";
} else if (funct5 == FUNC5_AMOOR) {
cmd = funct3 == FUNC3_AMOD ? "AMOOR.D" : "AMOOR.W";
} else if (funct5 == FUNC5_AMOXOR) {
cmd = funct3 == FUNC3_AMOD ? "AMOXOR.D" : "AMOXOR.W";
} else if (funct5 == FUNC5_AMOSWAP) {
cmd = funct3 == FUNC3_AMOD ? "AMOSWAP.D" : "AMOSWAP.W";
}
cmd += " " + GetRegName(rd) + ", " + GetRegName(rs2) + ", (" +
GetRegName(rs1) + ")";
break;
default:
cmd = "Undefined instruction";
break;
}
return cmd;
}
} // namespace RISCV_EMULATOR