-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
855 lines (790 loc) · 15.9 KB
/
decode.go
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
package main
// 在go语言中逻辑右移还是算术右移是通过远算数的类型来决定的
func GetQuadrant(data uint32) uint32 {
return ((data) >> 0) & 0x3
}
/*
非压缩指令
从risc-v的指令中提取出各个操作数(操作寄存器,目的寄存器,立即数等)
*/
func OpCode(data uint32) uint32 {
return (data >> 2) & 0x1f
}
func Rd(data uint32) uint32 {
return (data >> 7) & 0x1f
}
func Rs1(data uint32) uint32 {
return (data >> 15) & 0x1f
}
func Rs2(data uint32) uint32 {
return (data >> 20) & 0x1f
}
func Rs3(data uint32) uint32 {
return (data >> 27) & 0x1f
}
func Funct2(data uint32) uint32 {
return (data >> 25) & 0x3
}
func Funct3(data uint32) uint32 {
return (data >> 12) & 0x7
}
func Funct7(data uint32) uint32 {
return (data >> 25) & 0x7f
}
func Imm116(data uint32) uint32 {
return (data >> 26) & 0x3f
}
func InstrBTypeRead(instruction *Instruction, data uint32) {
imm12 := (data >> 31) & 0x1
imm105 := (data >> 25) & 0x3f
imm41 := (data >> 8) & 0xf
imm11 := (data >> 7) & 0x1
imm := int32((imm12 << 12) | (imm11 << 11) | (imm105 << 5) | (imm41 << 1))
imm = (imm << 19) >> 19
instruction.imm = imm
instruction.rs1 = int8(Rs1(data))
instruction.rs2 = int8(Rs2(data))
return
}
func InstrITypeRead(instruction *Instruction, data uint32) {
instruction.imm = int32(data >> 20)
instruction.rs1 = int8(Rs1(data))
instruction.rd = int8(Rd(data))
return
}
func InstrUTypeRead(instruction *Instruction, data uint32) {
instruction.imm = int32(data & 0xfffff000)
instruction.rd = int8(Rd(data))
return
}
func InstrSTypeRead(instruction *Instruction, data uint32) {
imm115 := (data >> 25) & 0x7f
imm40 := (data >> 7) & 0x1f
imm := int32((imm115 << 5) | imm40)
instruction.imm = imm
instruction.rs1 = int8(Rs1(data))
instruction.rs2 = int8(Rs2(data))
return
}
func InstrRTypeRead(instruction *Instruction, data uint32) {
instruction.rs1 = int8(Rs1(data))
instruction.rs2 = int8(Rs2(data))
instruction.rd = int8(Rd(data))
return
}
func InstrFprTypeRead(instruction *Instruction, data uint32) {
instruction.rs1 = int8(Rs1(data))
instruction.rs2 = int8(Rs2(data))
instruction.rs3 = int8(Rs3(data))
instruction.rd = int8(Rd(data))
return
}
func InstrJTypeRead(instruction *Instruction, data uint32) {
imm20 := (data >> 31) & 0x1
imm101 := (data >> 21) & 0x3ff
imm11 := (data >> 20) & 0x1
imm1912 := (data >> 12) & 0xff
imm := int32((imm20 << 20) | (imm1912 << 12) | (imm11 << 11) | (imm101 << 1))
imm = (imm << 11) >> 11
instruction.imm = imm
instruction.rd = int8(Rd(data))
return
}
func InstrCsrTypeRead(instruction *Instruction, data uint32) {
instruction.csr = int16(data >> 20)
instruction.rs1 = int8(Rs1(data))
instruction.rd = int8(Rd(data))
return
}
func InstructionDecode(instruction *Instruction, data uint32) {
quadrant := GetQuadrant(data)
switch quadrant {
case 0x0:
Fatal("unimplemented")
case 0x1:
Fatal("unimplemented")
case 0x2:
Fatal("unimplemented")
case 0x3:
opCode := OpCode(data)
switch opCode {
case 0x0:
funct3 := Funct3(data)
InstrITypeRead(instruction, data)
switch funct3 {
case 0x0: /* LB */
instruction.iType = InsnLb
return
case 0x1: /* LH */
instruction.iType = InsnLh
return
case 0x2:
instruction.iType = InsnLw
return
case 0x3:
instruction.iType = InsnLd
return
case 0x4:
instruction.iType = InsnLbu
return
case 0x5:
instruction.iType = InsnLhu
return
case 0x6:
instruction.iType = InsnLwu
return
default:
UnReachable()
}
UnReachable()
case 0x1:
funct3 := Funct3(data)
InstrITypeRead(instruction, data)
switch funct3 {
case 0x2:
instruction.iType = InsnFlw
return
case 0x3:
instruction.iType = InsnFld
return
default:
UnReachable()
}
UnReachable()
case 0x3:
funct3 := Funct3(data)
switch funct3 {
case 0x0: /* FENCE */
instruction.iType = InsnFence
return
case 0x1:
instruction.iType = InsnFenceI
default:
UnReachable()
}
UnReachable()
case 0x4:
funct3 := Funct3(data)
InstrITypeRead(instruction, data)
switch funct3 {
case 0x0:
instruction.iType = InsnAddi
return
case 0x1:
imm116 := Imm116(data)
if imm116 == 0 {
instruction.iType = InsnSlli
} else {
UnReachable()
}
return
case 0x2:
instruction.iType = InsnSlti
return
case 0x3:
instruction.iType = InsnSltiu
return
case 0x4:
instruction.iType = InsnXori
return
case 0x5:
imm116 := Imm116(data)
if imm116 == 0x0 {
instruction.iType = InsnSrli
} else if imm116 == 0x10 {
instruction.iType = InsnSrai
} else {
UnReachable()
}
return
case 0x6:
instruction.iType = InsnOri
return
case 0x7:
instruction.iType = InsnAndi
return
default:
Fatal("unknow funct3")
}
UnReachable()
case 0x5:
InstrUTypeRead(instruction, data)
instruction.iType = InsnAuipc
return
case 0x6:
funct3 := Funct3(data)
funct7 := Funct7(data)
InstrITypeRead(instruction, data)
switch funct3 {
case 0x0:
instruction.iType = InsnAddiw
return
case 0x1:
assert(funct7 == 0, "funct7 is not 0")
instruction.iType = InsnSlliw
return
case 0x5:
switch funct7 {
case 0x0:
instruction.iType = InsnSrliw
return
case 0x20:
instruction.iType = InsnSraiw
return
default:
UnReachable()
}
default:
UnReachable()
}
UnReachable()
case 0x8:
funct3 := Funct3(data)
InstrSTypeRead(instruction, data)
switch funct3 {
case 0x0:
instruction.iType = InsnSb
return
case 0x1:
instruction.iType = InsnSh
return
case 0x2:
instruction.iType = InsnSw
return
case 0x3:
instruction.iType = InsnSd
return
default:
UnReachable()
}
UnReachable()
case 0x9:
funct3 := Funct3(data)
InstrSTypeRead(instruction, data)
switch funct3 {
case 0x2: /* FSW */
instruction.iType = InsnFsw
return
case 0x3: /* FSD */
instruction.iType = InsnFsd
return
default:
UnReachable()
}
UnReachable()
case 0xc:
InstrRTypeRead(instruction, data)
funct3 := Funct3(data)
funct7 := Funct7(data)
switch funct7 {
case 0x0:
switch funct3 {
case 0x0:
instruction.iType = InsnAdd
return
case 0x1:
instruction.iType = InsnSll
return
case 0x2:
instruction.iType = InsnSlt
return
case 0x3:
instruction.iType = InsnSltu
return
case 0x4:
instruction.iType = InsnXor
return
case 0x5:
instruction.iType = InsnSrl
return
case 0x6:
instruction.iType = InsnOr
return
case 0x7:
instruction.iType = InsnAnd
return
default:
UnReachable()
}
case 0x1:
switch funct3 {
case 0x0:
instruction.iType = InsnMul
return
case 0x1:
instruction.iType = InsnMulh
return
case 0x2:
instruction.iType = InsnMulhsu
return
case 0x3:
instruction.iType = InsnMulhu
return
case 0x4:
instruction.iType = InsnDiv
return
case 0x5:
instruction.iType = InsnDivu
return
case 0x6:
instruction.iType = InsnRem
return
case 0x7:
instruction.iType = InsnRemu
return
default:
UnReachable()
}
case 0x20:
switch funct3 {
case 0x0:
instruction.iType = InsnSub
return
case 0x5:
instruction.iType = InsnSra
return
default:
UnReachable()
}
UnReachable()
default:
UnReachable()
}
UnReachable()
case 0xd:
InstrUTypeRead(instruction, data)
instruction.iType = InsnLui
return
case 0xe:
InstrRTypeRead(instruction, data)
funct3 := Funct3(data)
funct7 := Funct7(data)
switch funct7 {
case 0x0:
switch funct3 {
case 0x0:
instruction.iType = InsnAddw
return
case 0x1:
instruction.iType = InsnSllw
return
case 0x5:
instruction.iType = InsnSrlw
return
default:
UnReachable()
}
case 0x1:
switch funct3 {
case 0x0:
instruction.iType = InsnMulw
return
case 0x4:
instruction.iType = InsnDivw
return
case 0x5:
instruction.iType = InsnDivuw
return
case 0x6:
instruction.iType = InsnRemw
return
case 0x7:
instruction.iType = InsnRemuw
return
default:
UnReachable()
}
case 0x20:
switch funct3 {
case 0x0:
instruction.iType = InsnSubw
return
case 0x5:
instruction.iType = InsnSraw
return
default:
UnReachable()
}
default:
UnReachable()
}
UnReachable()
case 0x10:
funct2 := Funct2(data)
InstrFprTypeRead(instruction, data)
switch funct2 {
case 0x0:
instruction.iType = InsnFmaddS
return
case 0x1:
instruction.iType = InsnFmaddD
return
default:
UnReachable()
}
UnReachable()
case 0x11:
funct2 := Funct2(data)
InstrFprTypeRead(instruction, data)
switch funct2 {
case 0x0:
instruction.iType = InsnFmsubS
return
case 0x1:
instruction.iType = InsnFmsubD
return
default:
UnReachable()
}
UnReachable()
case 0x12:
funct2 := Funct2(data)
InstrFprTypeRead(instruction, data)
switch funct2 {
case 0x0:
instruction.iType = InsnFnmsubS
return
case 0x1:
instruction.iType = InsnFnmsubD
return
default:
UnReachable()
}
UnReachable()
case 0x13:
funct2 := Funct2(data)
InstrFprTypeRead(instruction, data)
switch funct2 {
case 0x0:
instruction.iType = InsnFnmaddS
return
case 0x1:
instruction.iType = InsnFnmaddD
return
default:
UnReachable()
}
UnReachable()
case 0x14:
funct7 := Funct7(data)
InstrRTypeRead(instruction, data)
switch funct7 {
case 0x0:
instruction.iType = InsnFaddS
return
case 0x1:
instruction.iType = InsnFaddD
return
case 0x4:
instruction.iType = InsnFsubS
return
case 0x5:
instruction.iType = InsnFsubD
return
case 0x8:
instruction.iType = InsnFmulS
return
case 0x9:
instruction.iType = InsnFmulD
return
case 0xc:
instruction.iType = InsnFdivS
return
case 0xd:
instruction.iType = InsnFdivD
return
case 0x10:
funct3 := Funct3(data)
switch funct3 {
case 0x0:
instruction.iType = InsnFsgnjS
return
case 0x1:
instruction.iType = InsnFsgnjnS
return
case 0x2:
instruction.iType = InsnFsgnjxS
return
default:
UnReachable()
}
case 0x11:
funct3 := Funct3(data)
switch funct3 {
case 0x0:
instruction.iType = InsnFsgnjD
return
case 0x1:
instruction.iType = InsnFsgnjnD
return
case 0x2:
instruction.iType = InsnFsgnjxD
return
default:
UnReachable()
}
UnReachable()
case 0x14:
funct3 := Funct3(data)
switch funct3 {
case 0x0:
instruction.iType = InsnFminS
return
case 0x1:
instruction.iType = InsnFmaxS
return
default:
UnReachable()
}
UnReachable()
case 0x15:
funct3 := Funct3(data)
switch funct3 {
case 0x0:
instruction.iType = InsnFminD
return
case 0x1:
instruction.iType = InsnFmaxD
return
default:
UnReachable()
}
UnReachable()
case 0x20:
assert(Rs2(data) == 1, "FCVT.S.D rs2 is not zero")
instruction.iType = InsnFcvtSD
return
case 0x21:
assert(Rs2(data) == 0, "FCVT.D.S rs2 is not zero")
instruction.iType = InsnFcvtDS
return
case 0x2c:
assert(Rs2(data) == 0, "FSQRT.S rs2 is not zero")
instruction.iType = InsnFsqrtS
return
case 0x2d:
assert(Rs2(data) == 0, "FSQRT.D rs2 is not zero")
instruction.iType = InsnFsqrtD
return
case 0x50:
funct3 := Funct3(data)
switch funct3 {
case 0x0:
instruction.iType = InsnFleS
return
case 0x1:
instruction.iType = InsnFltS
return
case 0x2:
instruction.iType = InsnFeqS
return
default:
UnReachable()
}
UnReachable()
case 0x51:
funct3 := Funct3(data)
switch funct3 {
case 0x0:
instruction.iType = InsnFleD
return
case 0x1:
instruction.iType = InsnFltD
return
case 0x2:
instruction.iType = InsnFeqD
return
default:
UnReachable()
}
UnReachable()
case 0x60:
rs2 := Rs2(data)
switch rs2 {
case 0x0:
instruction.iType = InsnFcvtWS
return
case 0x1:
instruction.iType = InsnFcvtWuS
return
case 0x2:
instruction.iType = InsnFcvtLS
return
case 0x3:
instruction.iType = InsnFcvtLuS
return
default:
UnReachable()
}
UnReachable()
case 0x61:
rs2 := Rs2(data)
switch rs2 {
case 0x0:
instruction.iType = InsnFcvtWD
return
case 0x1:
instruction.iType = InsnFcvtWud
return
case 0x2:
instruction.iType = InsnFcvtLD
return
case 0x3:
instruction.iType = InsnFcvtLuD
return
default:
UnReachable()
}
UnReachable()
case 0x68:
rs2 := Rs2(data)
switch rs2 {
case 0x0:
instruction.iType = InsnFcvtSW
return
case 0x1:
instruction.iType = InsnFcvtSWu
return
case 0x2:
instruction.iType = InsnFcvtSL
return
case 0x3:
instruction.iType = InscFcvtSLu
return
default:
UnReachable()
}
UnReachable()
case 0x69:
rs2 := Rs2(data)
switch rs2 {
case 0x0:
instruction.iType = InsnFcvtDW
return
case 0x1:
instruction.iType = InsnFcvtDWu
return
case 0x2:
instruction.iType = InsnFcvtDL
return
case 0x3:
instruction.iType = InsnFcvtDLu
return
default:
UnReachable()
}
UnReachable()
case 0x70:
assert(Rs2(data) == 0, "case 0x70 rs2 is not zero")
funct3 := Funct3(data)
switch funct3 {
case 0x0:
instruction.iType = InsnFmvXW
return
case 0x1:
instruction.iType = InsnFclassS
return
default:
UnReachable()
}
UnReachable()
case 0x71:
assert(Rs2(data) == 0, "case 0x71 rs2 is not zero")
funct3 := Funct3(data)
switch funct3 {
case 0x0:
instruction.iType = InsnFmvXD
return
case 0x1:
instruction.iType = InsnFclassD
return
default:
UnReachable()
}
UnReachable()
case 0x78:
assert(Rs2(data) == 0, "FMV.W.X rs2 is not zero")
instruction.iType = InsnFmvWX
return
case 0x79:
assert(Rs2(data) == 0, "FMV.D.X rs2 is not zero")
instruction.iType = InsnFmvDX
return
default:
UnReachable()
}
UnReachable()
case 0x18:
InstrBTypeRead(instruction, data)
funct3 := Funct3(data)
switch funct3 {
case 0x0: /* BEQ */
instruction.iType = InsnBeq
return
case 0x1: /* BNE */
instruction.iType = InsnBne
return
case 0x4: /* BLT */
instruction.iType = InsnBlt
return
case 0x5: /* BGE */
instruction.iType = InsnBge
return
case 0x6: /* BLTU */
instruction.iType = InsnBltu
return
case 0x7:
instruction.iType = InsnBgeu
default:
UnReachable()
}
UnReachable()
case 0x19: /* JALR */
InstrITypeRead(instruction, data)
instruction.iType = InsnJalr
instruction.cont = true
return
case 0x1b:
InstrJTypeRead(instruction, data)
instruction.iType = InsnJal
instruction.cont = true
return
case 0x1c:
if data == 0x73 {
instruction.iType = InsnEcall
instruction.cont = true
return
}
funct3 := Funct3(data)
InstrCsrTypeRead(instruction, data)
switch funct3 {
case 0x1:
instruction.iType = InsnCsrrw
return
case 0x2:
instruction.iType = InsnCsrrs
return
case 0x3:
instruction.iType = InsnCsrrc
return
case 0x5:
instruction.iType = InsnCsrrwi
return
case 0x6:
instruction.iType = InsnCsrrsi
return
case 0x7:
instruction.iType = InsnCsrrci
return
default:
UnReachable()
}
UnReachable()
default:
UnReachable()
}
UnReachable()
default:
UnReachable()
}
}