forked from byungwoo733/riscv-ovpsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riscvDisassemble.c
619 lines (512 loc) · 15.5 KB
/
riscvDisassemble.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Copyright (c) 2005-2020 Imperas Software Ltd., www.imperas.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// standard includes
#include <ctype.h>
#include <stdio.h>
#include <string.h>
// VMI header files
#include "vmi/vmiMessage.h"
// model header files
#include "riscvCSR.h"
#include "riscvDecode.h"
#include "riscvDecodeTypes.h"
#include "riscvDisassembleFormats.h"
#include "riscvFunctions.h"
#include "riscvUtils.h"
//
// Prefix for messages from this module
//
#define CPU_PREFIX "RISCV_DISASS"
////////////////////////////////////////////////////////////////////////////////
// UTILITY FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
//
// Append the character to the result
//
static void putChar(char **result, char ch) {
// get the tail pointer
char *tail = *result;
// do the append
*tail++ = ch;
// add null terminator
*tail = 0;
// update the tail pointer
*result = tail;
}
//
// Append the string to the result
//
static void putString(char **result, const char *string) {
// get the tail pointer
char *tail = *result;
char ch;
// do the append
while((ch=*string++)) {
*tail++ = ch;
}
// add null terminator
*tail = 0;
// update the tail pointer
*result = tail;
}
//
// Emit key for uncooked value if required
//
static void putUncookedKey(char **result, const char *key, Bool uncooked) {
if(uncooked) {
putString(result, key);
putChar(result, ':');
}
}
//
// Return number of members of an array
//
#define NUM_MEMBERS(_A) (sizeof(_A)/sizeof((_A)[0]))
//
// This defines the minimum string width to use for the opcode
//
#define OP_WIDTH 7
//
// Emit instruction format if required
//
static void putInstruction(riscvInstrInfoP info, char **result) {
const char *fmt;
// select format string
if(info->bytes==2) {
fmt = "%04x ";
} else {
fmt = "%08x ";
}
// emit basic opcode string
*result += sprintf(*result, fmt, info->instruction);
}
//
// Append comma separator, unless previous character is space or comma (allows
// for omitted optional arguments)
//
static void putComma(char **result, Bool uncooked) {
if(!uncooked) {
char *tail = *result;
switch(tail[-1]) {
case ',':
case ' ':
break;
default:
putChar(result, ',');
break;
}
}
}
//
// Emit signed argument
//
static void putD(char **result, Uns32 value) {
*result += sprintf(*result, "%d", value);
}
//
// Emit hexadeximal argument
//
static void putX(char **result, Uns32 value) {
*result += sprintf(*result, "0x%x", value);
}
//
// Emit target address argument
//
static void putTarget(char **result, Uns64 value) {
*result += sprintf(*result, FMT_Ax, value);
}
//
// Emit register argument
//
static void putReg(char **result, riscvRegDesc r, Bool opt, Bool uncooked) {
Uns32 index = getRIndex(r);
if(opt && !uncooked && !index && isXReg(r)) {
// omit zero register
} else if(isXReg(r)) {
putString(result, riscvGetXRegName(index));
} else if(isFReg(r)) {
putString(result, riscvGetFRegName(index));
} else if(isVReg(r)) {
putString(result, riscvGetVRegName(index));
} else {
VMI_ABORT("Bad register specifier 0x%x", r); // LCOV_EXCL_LINE
}
}
//
// Emit optional mask register argument
//
static void putOptMask(
char **result,
riscvRegDesc mask,
const char *suffix,
Bool uncooked
) {
if(mask) {
putUncookedKey(result, " MASK", uncooked);
putString(result, riscvGetVRegName(getRIndex(mask)));
if(!uncooked) {
putString(result, suffix);
}
}
}
//
// Emit CSR argument
//
static void putCSR(char **result, riscvP riscv, Uns32 csr) {
const char *name = riscvGetCSRName(riscv, csr);
if(name) {
putString(result, name);
} else {
*result += sprintf(*result, "0x%03x", csr);
}
}
//
// Emit fence argument
//
static void putFence(
char **result,
riscvFenceDesc fence,
riscvFenceDesc other,
Bool uncooked
) {
if(!fence) {
putString(result, "unknown");
} else if(uncooked || (fence!=RV_FENCE_IOWR) || (other!=RV_FENCE_IOWR)) {
if(fence & RV_FENCE_I) putChar(result, 'i');
if(fence & RV_FENCE_O) putChar(result, 'o');
if(fence & RV_FENCE_R) putChar(result, 'r');
if(fence & RV_FENCE_W) putChar(result, 'w');
}
}
//
// Emit rounding mode argument
//
static void putOptRM(char **result, riscvRMDesc rm, Bool uncooked) {
if(rm) {
putUncookedKey(result, " RM", uncooked);
if(!uncooked && (rm==RV_RM_ROD)) {
// rounding mode in opcode (not consistent with base architecture)
} else {
static const char *map[] = {
[RV_RM_CURRENT] = "",
[RV_RM_RTE] = "rte",
[RV_RM_RTZ] = "rtz",
[RV_RM_RDN] = "rdn",
[RV_RM_RUP] = "rup",
[RV_RM_RMM] = "rmm",
[RV_RM_ROD] = "rod",
[RV_RM_BAD5] = "rm5",
[RV_RM_BAD6] = "rm6",
};
putString(result, map[rm]);
}
}
}
//
// Emit VType argument
//
static void putVType(char **result, Uns8 vsew, Uns8 vlmul) {
putChar(result, 'e');
putD(result, 8<<vsew);
putString(result, ",m");
putD(result, 1<<vlmul);
}
//
// Emit opcode modifier based on argument type
//
static riscvRegDesc putType(
char **result,
riscvInstrInfoP info,
riscvRegDesc this,
riscvRegDesc prev
) {
if(this && !isQReg(this) && (getRType(this)!=getRType(prev))) {
Uns32 bits = getRBits(this);
if(info->explicitType) {
// emit dot before type
putChar(result, '.');
// show explicit operand types
if(isFXReg(this)) {
putChar(result, 'x');
} else if(isWLReg(this)) {
putChar(result, (bits==32) ? 'w' : 'l');
} else if(isXReg(this)) {
putChar(result, (bits==32) ? 'w' : 'd');
} else if(isFReg(this)) {
putChar(result, (bits==32) ? 's' : 'd');
} else {
VMI_ABORT("Bad register specifier 0x%x", this); // LCOV_EXCL_LINE
}
// show explicit unsigned if required
if(isUReg(this)) {
putChar(result, 'u');
}
} else if(info->explicitW) {
// indicate "w" type (with no dot)
putChar(result, 'w');
}
prev = this;
}
return prev;
}
//
// Add opcode string to result
//
static void putOpcode(char **result, riscvP riscv, riscvInstrInfoP info) {
riscvRegDesc type = RV_RD_NA;
Uns32 i;
// emit basic opcode
putString(result, info->opcode);
// emit modifiers based on argument register types
for(i=0; i<RV_MAX_AREGS; i++) {
type = putType(result, info, info->r[i], type);
}
if(info->isWhole) {
// whole register load/store
putD(result, info->nf+1);
putChar(result, 'r');
} else {
// emit number of fields if required
if(info->nf) {
putString(result, "seg");
putD(result, info->nf+1);
}
// emit size modifier if required
switch(info->memBits) {
case 8: putChar(result, 'b'); break;
case 16: putChar(result, 'h'); break;
case 32: putChar(result, 'w'); break;
case 64: putChar(result, 'd'); break;
case -1: putChar(result, 'e'); break;
}
}
// emit unsigned modifier if required
if(info->unsExt) {
putChar(result, 'u');
}
// emit CSR as part of opcode if required
if(info->csrInOp) {
putCSR(result, riscv, info->csr);
}
// emit ff suffix if required
if(info->isFF) {
putString(result, "ff");
}
// type describing suffix actions
typedef struct viDescInfoS {
const char *suffix; // initial suffix
Bool addMaskM; // add m if mask is specified
} viDescInfo;
// vector suffixes
static const viDescInfo viDescs[RV_VIT_LAST] = {
[RV_VIT_NA] = {"", 0},
[RV_VIT_V] = {".v", 0},
[RV_VIT_W] = {".w", 0},
[RV_VIT_VV] = {".vv", 0},
[RV_VIT_VI] = {".vi", 0},
[RV_VIT_VX] = {".vx", 0},
[RV_VIT_WV] = {".wv", 0},
[RV_VIT_WI] = {".wi", 0},
[RV_VIT_WX] = {".wx", 0},
[RV_VIT_VF] = {".vf", 0},
[RV_VIT_WF] = {".wf", 0},
[RV_VIT_VS] = {".vs", 0},
[RV_VIT_M] = {".m", 0},
[RV_VIT_MM] = {".mm", 0},
[RV_VIT_VM] = {".vm", 0},
[RV_VIT_VVM] = {".vv", 1},
[RV_VIT_VXM] = {".vx", 1},
[RV_VIT_VIM] = {".vi", 1},
[RV_VIT_VFM] = {".vf", 1}
};
// emit vector suffix
VMI_ASSERT(info->VIType<NUM_MEMBERS(viDescs), "bad VIType (%u)", info->VIType);
putString(result, viDescs[info->VIType].suffix);
// add additional 'm' if mask is specified
if(viDescs[info->VIType].addMaskM && info->mask) {
putChar(result, 'm');
}
// acquire/release modifier suffixes
static const char *aqrlDescs[] = {
[RV_AQRL_NA] = "",
[RV_AQRL_RL] = ".rl",
[RV_AQRL_AQ] = ".aq",
[RV_AQRL_AQRL] = ".aqrl"
};
// emit acquire/release modifier
VMI_ASSERT(info->aqrl<NUM_MEMBERS(aqrlDescs), "bad aqrl (%u)", info->aqrl);
putString(result, aqrlDescs[info->aqrl]);
}
//
// Generate instruction disassembly using the format string
//
static void disassembleFormat(
riscvP riscv,
riscvInstrInfoP info,
char **result,
const char *format,
Bool uncooked
) {
// generate instruction pattern
if(!uncooked) {
putInstruction(info, result);
}
// get offset at opcode start
const char *opcodeStart = *result;
// add opcode string to result
putOpcode(result, riscv, info);
if(*format) {
// calculate length of opcode text
const char *opcodeEnd = *result;
Uns32 len = opcodeEnd-opcodeStart;
char ch;
// emit formatted space if cooked output
if(!uncooked) {
// pad to minimum width
for(; len<OP_WIDTH; len++) {
putChar(result, ' ');
}
// emit space before arguments
putChar(result, ' ');
}
// this defines whether the next argument is optional
Bool nextOpt = False;
// generate arguments in appropriate format
while((ch=*format++)) {
// is this argument optional?
Bool opt = nextOpt;
// assume subsequent argument is mandatory
nextOpt = False;
switch(ch) {
case EMIT_R1:
putUncookedKey(result, " R1", uncooked);
putReg(result, info->r[0], opt, uncooked);
break;
case EMIT_R2:
putUncookedKey(result, " R2", uncooked);
putReg(result, info->r[1], opt, uncooked);
break;
case EMIT_R3:
putUncookedKey(result, " R3", uncooked);
putReg(result, info->r[2], opt, uncooked);
break;
case EMIT_R4:
putUncookedKey(result, " R4", uncooked);
putReg(result, info->r[3], opt, uncooked);
break;
case EMIT_CS:
putUncookedKey(result, " C", uncooked);
putD(result, info->c);
break;
case EMIT_CX:
putUncookedKey(result, " C", uncooked);
putX(result, info->c);
break;
case EMIT_UI:
putUncookedKey(result, " C", uncooked);
putX(result, ((Uns32)info->c)>>12);
break;
case EMIT_TGT:
putUncookedKey(result, " T", uncooked);
putTarget(result, info->c);
break;
case EMIT_CSR:
putUncookedKey(result, " CSR", uncooked);
putCSR(result, riscv, info->csr);
break;
case EMIT_PRED:
putUncookedKey(result, " PRED", uncooked);
putFence(result, info->pred, info->succ, uncooked);
break;
case EMIT_SUCC:
putUncookedKey(result, " SUCC", uncooked);
putFence(result, info->succ, info->pred, uncooked);
break;
case EMIT_VTYPE:
putUncookedKey(result, " VTYPE", uncooked);
putVType(result, info->vsew, info->vlmul);
break;
case EMIT_RM:
putOptMask(result, info->mask, ".t", uncooked);
break;
case EMIT_RMR:
putOptMask(result, info->mask, "", uncooked);
break;
case '*':
nextOpt = True;
break;
case ',':
putComma(result, uncooked);
break;
default:
if(!uncooked) {putChar(result, ch);}
break;
}
}
// emit comma before optional rounding mode
putComma(result, uncooked);
}
// emit optional rounding mode
putOptRM(result, info->rm, uncooked);
// strip trailing whitespace and commas
char *tail = (*result)-1;
while((*tail == ' ') || (*tail == ',')) {
*tail-- = 0;
}
}
//
// This defines the size of the disassembly buffer
//
#define DISASS_BUFFER_SIZE 256
//
// riscv disassembler, decoded instruction interface
//
static const char *disassembleInfo(
riscvP riscv,
riscvInstrInfoP info,
vmiDisassAttrs attrs
) {
// static buffer to hold disassembly result
static char result[DISASS_BUFFER_SIZE];
const char *format = info->format;
char *tail = result;
// sanity check format is specified
VMI_ASSERT(format, "null instruction format");
// disassemble using the format for the type
disassembleFormat(riscv, info, &tail, format, attrs==DSA_UNCOOKED);
// validate disassembly buffer has not overflowed
VMI_ASSERT(
tail <= &result[DISASS_BUFFER_SIZE-1],
"buffer overflow for instruction '%s'\n",
result
);
// return the result
return result;
}
//
// riscv disassembler, VMI interface
//
VMI_DISASSEMBLE_FN(riscvDisassemble) {
riscvP riscv = (riscvP)processor;
riscvInstrInfo info;
// decode instruction
riscvDecode(riscv, thisPC, &info);
// return disassembled instruction
return disassembleInfo(riscv, &info, attrs);
}