forked from westerndigitalcorporation/swerv-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instforms.hpp
1216 lines (947 loc) · 40.9 KB
/
instforms.hpp
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2018 Western Digital Corporation or its affiliates.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
#pragma once
#include <cstdint>
#include <type_traits>
namespace WdRiscv
{
// Structures useful for encoding/decoding a risc-v instructions.
/// Pack/unpack an r-form instruction.
union RFormInst
{
/// Constructor: Either pass a valid r-form value or start with any
/// value and then use an encode method.
RFormInst(uint32_t inst)
{ code = inst; }
/// Return top 5-bits of instruction (for atomic insts).
unsigned top5() const
{ return bits.funct7 >> 2; }
/// Return aq (acquire) field for atomic instructions.
bool aq() const
{ return (bits.funct7 >> 1) & 1; }
/// Return rl (release) field for atomic instructions.
bool rl() const
{ return bits.funct7 & 1; }
/// Encode "add rd, rs1, rs2" into this object.
bool encodeAdd(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "sub rd, rs1, rs2" into this object.
bool encodeSub(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "sll rd, rs1, rs2" into this object.
bool encodeSll(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "slt rd, rs1, rs2" into this object.
bool encodeSlt(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "sltu rd, rs1, rs2" into this object.
bool encodeSltu(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "xor rd, rs1, rs2" into this object.
bool encodeXor(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "srl rd, rs1, rs2" into this object.
bool encodeSrl(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "sra rd, rs1, rs2" into this object.
bool encodeSra(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "or rd, rs1, rs2" into this object.
bool encodeOr(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "and rd, rs1, rs2" into this object.
bool encodeAnd(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "addw rd, rs1, rs2" into this object.
bool encodeAddw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "addw rd, rs1, rs2" into this object.
bool encodeSubw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "sllw rd, rs1, rs2" into this object.
bool encodeSllw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "srlw rd, rs1, rs2" into this object.
bool encodeSrlw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "srlw rd, rs1, rs2" into this object.
bool encodeSraw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "mul rd, rs1, rs2" into this object.
bool encodeMul(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "mulh rd, rs1, rs2" into this object.
bool encodeMulh(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "mulhsu rd, rs1, rs2" into this object.
bool encodeMulhsu(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "mulhu rd, rs1, rs2" into this object.
bool encodeMulhu(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "div rd, rs1, rs2" into this object.
bool encodeDiv(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "divu rd, rs1, rs2" into this object.
bool encodeDivu(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "rem rd, rs1, rs2" into this object.
bool encodeRem(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "remu rd, rs1, rs2" into this object.
bool encodeRemu(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "mulw rd, rs1, rs2" into this object.
bool encodeMulw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "divw rd, rs1, rs2" into this object.
bool encodeDivw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "divuw rd, rs1, rs2" into this object.
bool encodeDivuw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "remw rd, rs1, rs2" into this object.
bool encodeRemw(unsigned rd, unsigned rs1, unsigned rs2);
/// Encode "remuw rd, rs1, rs2" into this object.
bool encodeRemuw(unsigned rd, unsigned rs1, unsigned rs2);
uint32_t code;
struct
{
unsigned opcode : 7;
unsigned rd : 5;
unsigned funct3 : 3;
unsigned rs1 : 5;
unsigned rs2 : 5;
unsigned funct7 : 7;
} bits;
};
/// Pack/unpack a b-form instruction.
union BFormInst
{
/// Constructor: Either pass a valid b-form value or start with any
/// value and then use an encode method.
BFormInst(uint32_t inst)
{ code = inst; }
/// Return immediate value as signed.
int32_t immed() const
{ return (bits.imm12 << 12) | (bits.imm11 << 11) | (bits.imm10_5 << 5) |
(bits.imm4_1 << 1); }
/// Encode a "beq rs1, rs2, imm" into this object.
bool encodeBeq(unsigned rs1, unsigned rs2, int imm);
/// Encode a "bne rs1, rs2, imm" into this object.
bool encodeBne(unsigned rs1, unsigned rs2, int imm);
/// Encode a "blt rs1, rs2, imm" into this object.
bool encodeBlt(unsigned rs1, unsigned rs2, int imm);
/// Encode a "bge rs1, rs3, imm" into this object.
bool encodeBge(unsigned rs1, unsigned rs2, int imm);
/// Encode a "bltu rs1, rs2, imm" into this object.
bool encodeBltu(unsigned rs1, unsigned rs2, int imm);
/// Encode a "bgeu rs1, rs2, imm" into this object.
bool encodeBgeu(unsigned rs1, unsigned rs2, int imm);
uint32_t code;
struct
{
unsigned opcode : 7;
unsigned imm11 : 1;
unsigned imm4_1 : 4;
unsigned funct3 : 3;
unsigned rs1 : 5;
unsigned rs2 : 5;
unsigned imm10_5 : 6;
int imm12 : 1; // Note: int for sign extension
} bits;
};
/// Pack/unpack a i-form instruction.
union IFormInst
{
/// Constructor: Either pass a valid i-form value or start with any
/// value and then use an encode method.
IFormInst(uint32_t inst)
{ code = inst; }
/// Return immediate value as signed.
int32_t immed() const
{ return fields.imm; }
/// Return immediate value as unsigned.
uint32_t uimmed() const // Immediate as unsigned.
{ return fields.imm & 0xfff; }
/// Return top-7 bits of instruction.
unsigned top7() const
{ return fields2.top7; }
/// Return pred field (for fence instruction).
unsigned pred() const
{ return (uimmed() >> 4) & 0xf; }
/// Return succ field (for fence instruction).
unsigned succ() const
{ return uimmed() & 0xf; }
/// Return top 4-bits of instruction (for fence).
unsigned top4() const
{ return uimmed() >> 8; }
/// Return the rs2 bits (for sfence.vma).
unsigned rs2() const
{ return fields2.shamt; }
/// Encode "addi rd, rs1, imm" into this object returning
/// true on success and false if any of the parameters are out of
/// range.
bool encodeAddi(unsigned rd, unsigned rs1, int imm);
/// Encode "andi rd, rs1, imm" into this object.
bool encodeAndi(unsigned rd, unsigned rs1, int imm);
/// Encode "ebreak" into this object.
bool encodeEbreak();
/// Encode "ecall" into this object.
bool encodeEcall();
/// Encode "jalr rd, offset(rs1)" into this object.
bool encodeJalr(unsigned rd, unsigned rs1, int offset);
/// Encode "lb rd, offset(rs1)" into this object.
bool encodeLb(unsigned rd, unsigned rs1, int offset);
/// Encode "lh rd, offset(rs1)" into this object.
bool encodeLh(unsigned rd, unsigned rs1, int offset);
/// Encode "lw rd, offset(rs1)" into this object.
bool encodeLw(unsigned rd, unsigned rs1, int offset);
/// Encode "lbu rd, offset(rs1)" into this object.
bool encodeLbu(unsigned rd, unsigned rs1, int offset);
/// Encode "lhu rd, offset(rs1)" into this object.
bool encodeLhu(unsigned rd, unsigned rs1, int offset);
/// Encode "lwu rd, offset(rs1)" into this object.
bool encodeLwu(unsigned rd, unsigned rs1, int offset);
/// Encode "ld rd, offset(rs1) into this obejct.
bool encodeLd(unsigned rd, unsigned rs1, int offset);
/// Encode "flw rd, offset(rs1) into this object.
bool encodeFlw(unsigned rd, unsigned rs1, int offset);
/// Encode "fld rd, offset(rs1) into this object.
bool encodeFld(unsigned rd, unsigned rs1, int offset);
/// Encode "slli rd, rs1, shamt" into this object.
bool encodeSlli(unsigned rd, unsigned rs1, unsigned shamt);
/// Encode "srli rd, rs1, shamt" into this object.
bool encodeSrli(unsigned rd, unsigned rs1, unsigned shamt);
/// Encode "srai rd, rs1, shamt" into this object.
bool encodeSrai(unsigned rd, unsigned rs1, unsigned shamt);
/// Encode "slti rd, rs1, imm" into this object.
bool encodeSlti(unsigned rd, unsigned rs1, int imm);
/// Encode "sltiu rd, rs1, imm" into this object.
bool encodeSltiu(unsigned rd, unsigned rs1, int imm);
/// Encode "xori rd, rs1, imm" into this object.
bool encodeXori(unsigned rd, unsigned rs1, int imm);
/// Encode "ori rd, rs1, imm" into this object.
bool encodeOri(unsigned rd, unsigned rs1, int imm);
/// Encode "addiw rd, rs1, imm" into this object returning
/// true on success and false if any of the parameters are out of
/// range.
bool encodeAddiw(unsigned rd, unsigned rs1, int imm);
/// Encode "slliw rd, rs1, shamt" into this object.
bool encodeSlliw(unsigned rd, unsigned rs1, unsigned shamt);
/// Encode "srliw rd, rs1, shamt" into this object.
bool encodeSrliw(unsigned rd, unsigned rs1, unsigned shamt);
/// Encode "sraiw rd, rs1, shamt" into this object.
bool encodeSraiw(unsigned rd, unsigned rs1, unsigned shamt);
/// Encode "fence.i" into this object.
bool encodeFencei();
/// Encode "fence pred, succ" into this object.
bool encodeFence(uint32_t pred, uint32_t succ);
/// Encode "csrrw rd, csr, rs" into this object.
bool encodeCsrrw(uint32_t rd, uint32_t rs1, uint32_t csr);
/// Encode "csrrw rd, csr, rs" into this object.
bool encodeCsrrs(uint32_t rd, uint32_t rs1, uint32_t csr);
/// Encode "csrrw rd, csr, rs" into this object.
bool encodeCsrrc(uint32_t rd, uint32_t rs1, uint32_t csr);
/// Encode "csrrw rd, csr, rs" into this object.
bool encodeCsrrwi(uint32_t rd, uint32_t imm, uint32_t csr);
/// Encode "csrrw rd, csr, rs" into this object.
bool encodeCsrrsi(uint32_t rd, uint32_t imm, uint32_t csr);
/// Encode "csrrw rd, csr, rs" into this object.
bool encodeCsrrci(uint32_t rd, uint32_t imm, uint32_t csr);
uint32_t code;
struct
{
unsigned opcode : 7;
unsigned rd : 5;
unsigned funct3 : 3;
unsigned rs1 : 5;
int imm : 12;
} fields;
struct
{
unsigned opcode : 7;
unsigned rd : 5;
unsigned funct3 : 3;
unsigned rs1 : 5;
unsigned shamt : 5;
unsigned top7 : 7;
} fields2;
// XLEN=64 variant
struct
{
unsigned opcode : 7;
unsigned rd : 5;
unsigned funct3 : 3;
unsigned rs1 : 5;
unsigned shamt : 6;
unsigned top6 : 6;
} fields3;
};
/// Pack/unpack a s-form instruction.
union SFormInst
{
/// Constructor: Either pass a valid s-form value or start with any
/// value and then use an encode method.
SFormInst(uint32_t inst)
{ code = inst; }
/// Return immediate value as signed.
int32_t immed() const
{ return (bits.imm11_5 << 5) | bits.imm4_0; }
/// Encode "sb rs2, imm(rs1)" into this object.
bool encodeSb(unsigned rs1, unsigned rs2, int imm);
/// Encode "sh rs2, imm(rs1)" into this object.
bool encodeSh(unsigned rs1, unsigned rs2, int imm);
/// Encode "sw rs2, imm(rs1)" into this object.
bool encodeSw(unsigned rs1, unsigned rs2, int imm);
/// Encode "sd rs2, imm(rs1)" into this object.
bool encodeSd(unsigned rs1, unsigned rs2, int imm);
/// Ecnode "fsw rs2, offset(rs1)" into this object
bool encodeFsw(unsigned rs1, unsigned rs2, int imm);
/// Ecnode "fsd rs2, offset(rs1)" into this object
bool encodeFsd(unsigned rs1, unsigned rs2, int imm);
uint32_t code;
struct
{
unsigned opcode : 7;
unsigned imm4_0 : 5;
unsigned funct3 : 3;
unsigned rs1 : 5;
unsigned rs2 : 5;
int imm11_5 : 7;
} bits;
};
/// Pack/unpack a u-form instruction.
union UFormInst
{
/// Constructor: Either pass a valid u-form value or start with
/// any value and then use an encode method.
UFormInst(uint32_t inst)
{ code = inst; }
/// Return immediate value as signed.
int32_t immed() const
{ return bits.imm << 12; }
/// Encode "lui rd, immed" into this object.
bool encodeLui(unsigned rd, int immed);
/// Encode "auipc rd, immed" into this object.
bool encodeAuipc(unsigned rd, int immed);
uint32_t code;
struct
{
unsigned opcode : 7;
unsigned rd : 5;
int imm : 20;
} bits;
};
/// Pack/unpack a j-form instruction.
union JFormInst
{
/// Constructor: Either pass a valid u-form value or start with
/// any value and then use an encode method.
JFormInst(uint32_t inst)
{ code = inst; }
/// Return immediate value as signed.
int32_t immed() const
{ return (bits.imm20 << 20) | (bits.imm19_12 << 12) | (bits.imm11 << 11) |
(bits.imm10_1 << 1); }
/// Encode "jal rd, offset" into this object.
bool encodeJal(unsigned rd, int offset);
uint32_t code;
struct
{
unsigned opcode : 7;
unsigned rd : 5;
unsigned imm19_12 : 8;
unsigned imm11 : 1;
unsigned imm10_1 : 10;
int imm20 : 1;
} bits;
};
/// Pack/unpack a cb-form instruction.
union CbFormInst
{
/// Constructor: Either pass a valid cb-form value or start with
/// any value and then use an encode method.
CbFormInst(uint16_t inst)
{ code = inst; }
/// Return immediate value encoded in this object.
int immed() const
{ return (bits.ic0 << 5) | (bits.ic1 << 1) | (bits.ic2 << 2) |
(bits.ic3 << 6) | (bits.ic4 << 7) | (bits.ic5 << 3) |
(bits. ic6 << 4) | int(bits.ic7 << 8); }
/// Encode "c.beqz rs1p, imm" into this object.
bool encodeCbeqz(unsigned rs1p, int imm);
/// Encode "c.bnez rs1p, imm" into this object.
bool encodeCbnez(unsigned rs1p, int imm);
uint32_t code;
struct
{
unsigned opcode : 2;
unsigned ic0 : 1;
unsigned ic1 : 1;
unsigned ic2 : 1;
unsigned ic3 : 1;
unsigned ic4 : 1;
unsigned rs1p : 3;
unsigned ic5 : 1;
unsigned ic6 : 1;
int ic7 : 1;
unsigned funct3 : 3;
unsigned unused : 16;
} bits;
};
/// \union CaiFormInst
/// \brief Used to pack/unpack c.slri, c.slri64, c.srai, c.srai64, c.andi,
/// c.sub, c.xor, c.or and c.and.
union CaiFormInst
{
CaiFormInst(uint16_t inst)
{ code = inst; }
int andiImmed() const
{
return int(bits.ic5 << 5) | (bits.ic4 << 4) | (bits.ic3 << 3) |
(bits.ic2 << 2) | (bits.ic1 << 1) | bits.ic0;
}
unsigned shiftImmed() const
{ return unsigned(andiImmed()) & 0x3f; }
bool encodeCsrli(unsigned rdpv, unsigned imm);
bool encodeCsrai(unsigned rdpv, unsigned imm);
bool encodeCandi(unsigned rdpv, int imm);
bool encodeCsub(unsigned rdpv, unsigned rs2pv);
bool encodeCxor(unsigned rdpv, unsigned rs2pv);
bool encodeCor(unsigned rdpv, unsigned rs2pv);
bool encodeCand(unsigned rdpv, unsigned rs2pv);
uint32_t code;
struct
{
unsigned opcode : 2;
unsigned ic0 : 1;
unsigned ic1 : 1;
unsigned ic2 : 1;
unsigned ic3 : 1;
unsigned ic4 : 1;
unsigned rdp : 3;
unsigned funct2 : 2;
int ic5 : 1;
unsigned funct3 : 3;
unsigned unused : 16;
} bits;
};
/// \union CiFormINst
/// \brief Pack-unpack ci-form compressed instructions: c.addi, c.addi16sp,
/// c.lui, c.lwsp, c.slli, c.ebreak, c.jalr and c.jr
union CiFormInst
{
CiFormInst(uint16_t inst)
{ code = inst; }
uint32_t code;
int32_t addiImmed() const
{ return int(bits2.ic5 << 5) | bits2.ic4_0; }
int32_t addi16spImmed() const
{ return int(bits.ic5 << 9) | (bits.ic4 << 4) | (bits.ic3 << 6) |
(bits.ic2 << 8) | (bits.ic1 << 7) | (bits.ic0 << 5); }
int32_t luiImmed() const
{ return int(bits.ic5 << 17) | (bits2.ic4_0 << 12); }
uint32_t slliImmed() const
{ return unsigned(addiImmed()) & 0x3f; }
uint32_t lwspImmed() const
{ return (bits.ic0 << 6) | (bits.ic1 << 7) | (bits.ic2 << 2) |
(bits.ic3 << 3) | (bits.ic4 << 4) | ((unsigned(bits.ic5) & 1) << 5); }
uint32_t ldspImmed() const
{ return (bits.ic0 << 6) | (bits.ic1 << 7) | (bits.ic2 << 8) |
(bits.ic3 << 3) | (bits.ic4 << 4) | ((unsigned(bits.ic5) & 1) << 5); }
bool encodeCadd(unsigned rdv, unsigned rs2v);
bool encodeCaddi(unsigned rdv, int imm);
bool encodeCaddi16sp(int imm);
bool encodeClui(unsigned rdv, int imm);
bool encodeClwsp(unsigned rdv, unsigned imm);
bool encodeCslli(unsigned rdv, unsigned shift);
bool encodeCebreak();
bool encodeCjalr(unsigned rs1);
bool encodeCjr(unsigned rs1);
struct
{
unsigned opcode : 2;
unsigned ic0 : 1;
unsigned ic1 : 1;
unsigned ic2 : 1;
unsigned ic3 : 1;
unsigned ic4 : 1;
unsigned rd : 5;
int ic5 : 1;
unsigned funct3 : 3;
unsigned unused : 16;
} bits;
struct
{
unsigned opcode : 2;
unsigned ic4_0 : 5;
unsigned rd : 5;
int ic5 : 1;
unsigned funct3 : 3;
unsigned unused : 16;
} bits2;
};
/// Pack/unpack cl-form instructions: c.lw, c.ld
union ClFormInst
{
ClFormInst(uint16_t inst)
{ code = inst; }
uint32_t code;
/// Return immediate value for c.lw instruction encoded in this
/// object.
unsigned lwImmed() const
{ return (bits.ic0 << 6) | (bits.ic1 << 2) | (bits.ic3 << 3) |
(bits.ic4 << 4) | (bits.ic5 << 5); }
/// Return immediate value for c.ld instruction encoded in this
/// object.
unsigned ldImmed() const
{ return (bits.ic0 << 6) | (bits.ic1 << 7) | (bits.ic3 << 3) |
(bits.ic4 << 4) | (bits.ic5 << 5); }
struct
{
unsigned opcode : 2;
unsigned rdp : 3;
unsigned ic0 : 1;
unsigned ic1 : 1;
unsigned rs1p : 3;
unsigned ic3 : 1;
unsigned ic4 : 1;
unsigned ic5 : 1;
unsigned funct3 : 3;
unsigned unused : 16;
} bits;
};
// Encode c.addi4spn
union CiwFormInst
{
CiwFormInst(uint16_t inst)
{ code = inst; }
uint32_t code;
unsigned immed() const
{ return (bits.ic0 << 3) | (bits.ic1 << 2) | (bits.ic2 << 6) |
(bits.ic3 << 7) | (bits.ic4 << 8) | (bits.ic5 << 9) |
(bits.ic6 << 4) | (bits.ic7 << 5); }
bool encodeCaddi4spn(unsigned rdpv, unsigned immed);
struct
{
unsigned opcode : 2;
unsigned rdp : 3;
unsigned ic0 : 1;
unsigned ic1 : 1;
unsigned ic2 : 1;
unsigned ic3 : 1;
unsigned ic4 : 1;
unsigned ic5 : 1;
unsigned ic6 : 1;
unsigned ic7 : 1;
unsigned funct3 : 3;
unsigned unused : 16;
} bits;
};
/// Pack/unpack compressed cj-form instructions: c.jal and c.j
union CjFormInst
{
CjFormInst(uint16_t inst)
{ code = inst; }
uint32_t code;
int immed() const
{ return (bits.ic0 << 5) | (bits.ic3_to_1 << 1) | (bits.ic4 << 7) |
(bits.ic5 << 6) | (bits.ic6 << 10) | (bits.ic8_7 << 8) |
(bits.ic9 << 4) | (bits.ic10 << 11); }
bool encodeCjal(int imm);
bool encodeCj(int imm);
struct
{
unsigned opcode : 2;
unsigned ic0 : 1;
unsigned ic3_to_1 : 3;
unsigned ic4 : 1;
unsigned ic5 : 1;
unsigned ic6 : 1;
unsigned ic8_7 : 2;
unsigned ic9 : 1;
int ic10 : 1; // Int used for sign extension.
unsigned funct3 : 3;
unsigned unused : 16;
} bits;
};
/// Pack/unpack c.swsp and similar instructions.
union CswspFormInst
{
CswspFormInst(uint16_t inst)
{ code = inst; }
uint32_t code;
unsigned swImmed() const
{ return (bits.ic0 << 6) | (bits.ic1 << 7) | (bits.ic2 << 2) |
(bits.ic3 << 3) | (bits.ic4 << 4) | (bits.ic5 << 5); }
unsigned sdImmed() const
{ return (bits.ic0 << 6) | (bits.ic1 << 7) | (bits.ic2 << 8) |
(bits.ic3 << 3) | (bits.ic4 << 4) | (bits.ic5 << 5); }
bool encodeCswsp(unsigned rs2v, unsigned imm);
struct
{
unsigned opcode : 2;
unsigned rs2 : 5;
unsigned ic0 : 1;
unsigned ic1 : 1;
unsigned ic2 : 1;
unsigned ic3 : 1;
unsigned ic4 : 1;
unsigned ic5 : 1;
unsigned funct3 : 3;
unsigned unused : 16;
} bits;
};
/// Pack/unpack c.sw and similar instructions.
union CsFormInst
{
CsFormInst(uint16_t inst)
{ code = inst; }
uint32_t code;
unsigned swImmed() const
{ return (bits.ic0 << 6) | (bits.ic1 << 2) | (bits.ic2 << 3) |
(bits.ic3 << 4) | (bits.ic4 << 5); }
unsigned sdImmed() const
{ return (bits.ic0 << 6) | (bits.ic1 << 7) | (bits.ic2 << 3) |
(bits.ic3 << 4) | (bits.ic4 << 5); }
bool encodeCsw(unsigned rs1pv, unsigned rs2pv, unsigned imm);
bool encodeCsd(unsigned rs1pv, unsigned rs2pv, unsigned imm);
struct
{
unsigned opcode : 2;
unsigned rs2p : 3;
unsigned ic0 : 1;
unsigned ic1 : 1;
unsigned rs1p : 3;
unsigned ic2 : 1;
unsigned ic3 : 1;
unsigned ic4 : 1;
unsigned funct3 : 3;
unsigned unused : 16;
} bits;
};
// We make all encode functions have the same signature. Instruction
// that do not require certain arguments are passed zero for those
// arguments.
/// Encode "lui rd, immed" into inst: encodeLui(rd, immed, 0, inst).
/// The third argument (x) is ignored.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeLui(uint32_t rd, uint32_t immed, uint32_t x, uint32_t& inst);
/// Encode "auipc rd, immed" into inst: encodeAuipc(rd, immed, 0, inst).
/// The third argument (x) is ignored.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeAuipc(uint32_t rd, uint32_t immed, uint32_t x, uint32_t& inst);
/// Encode "jal rd, offset" into inst: encodeJal(rd, offset, 0, inst).
/// THe third argument (x) is ignored.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeJal(uint32_t rd, uint32_t offset, uint32_t x, uint32_t& inst);
/// Encode "jalr rd, offset(rs1)": encodeJalr(rd, rs1, offset, inst).
/// The third argument (offset) is treaded as signed.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeJalr(uint32_t rd, uint32_t rs1, uint32_t offset, uint32_t& inst);
/// Encode a "beq rs1, rs2, imm" into inst: encodeBeq(rs1, rs2, imm, inst).
/// The third argument (imm) is treated as a signed value even-though
/// the type is uint32_t.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeBeq(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode a "bne rs1, rs2, imm" into inst: encodeBne(rs1, rs2, imm, inst).
/// The third argument (imm) is treated as a signed value even-though
/// the type is uint32_t.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeBne(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode a "blt rs1, rs2, imm" into inst: encodeBlt(rs1, rs2, imm, inst).
/// The third argument (imm) is treated as a signed value even-though
/// the type is uint32_t.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeBlt(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode a "bge rs1, rs2, imm" into inst: encodeBge(rs1, rs2, imm, inst).
/// The third argument (imm) is treated as a signed value even-though
/// the type is uint32_t.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeBge(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode a "bltu rs1, rs2, imm" into inst: encodeBltu(rs1, rs2, imm, inst).
/// The third argument (imm) is treated as a signed value even-though
/// the type is uint32_t.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeBltu(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode a "bgeu rs1, rs2, imm" into inst: encodeBgeu(rs1, rs2, imm, inst).
/// The third argument (imm) is treated as a signed value even-though
/// the type is uint32_t.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeBgeu(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode "lb rd, offset(rs1)": encodeLb(rd, rs1, offset, inst).
/// The third argument (offset) is treaded as signed.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeLb(uint32_t rd, uint32_t rs1, uint32_t offset, uint32_t& inst);
/// Encode "lh rd, offset(rs1)": encodeLh(rd, rs1, offset, inst).
/// The third argument (offset) is treaded as signed.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeLh(uint32_t rd, uint32_t rs1, uint32_t offset, uint32_t& inst);
/// Encode "lw rd, offset(rs1)": encodeLw(rd, rs1, offset, inst).
/// The third argument (offset) is treaded as signed.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeLw(uint32_t rd, uint32_t rs1, uint32_t offset, uint32_t& inst);
/// Encode "lbu rd, offset(rs1)": encodeLbu(rd, rs1, offset, inst).
/// The third argument (offset) is treaded as signed.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeLbu(uint32_t rd, uint32_t rs1, uint32_t offset, uint32_t& inst);
/// Encode "lhu rd, offset(rs1)": encodeLhu(rd, rs1, offset, inst).
/// The third argument (offset) is treaded as signed.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeLhu(uint32_t rd, uint32_t rs1, uint32_t offset, uint32_t& inst);
/// Encode "sb rs2, imm(rs1)" into inst: encodeSb(rs1, rs2, imm, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSb(uint32_t rs1, uint32_t rs2, uint32_t imm, uint32_t& inst);
/// Encode "sh rs2, imm(rs1)" into inst: encodeSh(rs1, rs2, imm, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSh(uint32_t rs1, uint32_t rs2, uint32_t imm, uint32_t& inst);
/// Encode "sw rs2, imm(rs1)" into inst: encodeSw(rs1, rs2, imm, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSw(uint32_t rs1, uint32_t rs2, uint32_t imm, uint32_t& inst);
/// Encode "addi rd, rs1, imm" into inst: encodeAddi(rd, rs1, imm, inst).
/// The third argument (imm) is treated as a signed value even-though
/// the type is uint32_t.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeAddi(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode "slti rd, rs1, imm" into inst: encodeSlti(rd, rs1, imm, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSlti(uint32_t rd, uint32_t rs1, uint32_t imm, uint32_t& inst);
/// Encode "slti rd, rs1, imm" into inst: encodeSltiu(rd, rs1, imm, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSltiu(uint32_t rd, uint32_t rs1, uint32_t imm, uint32_t& inst);
/// Encode "slti rd, rs1, imm" into inst: ecnodeSlti(rd, rs1, imm, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeXori(uint32_t rd, uint32_t rs1, uint32_t imm, uint32_t& inst);
/// Encode "slti rd, rs1, imm" into inst.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeOri(uint32_t rd, uint32_t rs1, uint32_t imm, uint32_t& inst);
/// Encode "andi rd, rs1, imm" into inst: encodeAndi(rd, rs1, imm, inst).
/// The third argument (imm) is treated as a signed value even-though
/// the type is uint32_t.
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeAndi(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode "slli rd, rs1, shamt" into inst: encodeSlli(rd, rs1, shamt, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSlli(uint32_t rd, uint32_t rs1, uint32_t shamt, uint32_t& inst);
/// Encode "srli rd, rs1, shamt" into inst: encodeSrli(rd, rs1, shamt, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSrli(uint32_t rd, uint32_t rs1, uint32_t shamt, uint32_t& inst);
/// Encode "srai rd, rs1, shamt" into inst: encodeSrai(rd, rs1, shamt, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSrai(uint32_t rd, uint32_t rs1, uint32_t shamt, uint32_t& inst);
/// Encode "add rd, rs1, rs2" into inst: encodeAdd(rd, rs1, rs2,
/// inst). Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeAdd(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode "sub rd, rs1, rs2" into inst: encodeSub(rd, rs1, rs2, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSub(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode "sll rd, rs1, rs2" into inst: encodeSll(rd, rs1, rs2, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSll(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode "slt rd, rs1, rs2" into inst: encodeSlt(rd, rs1, rs2, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSlt(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode "sltu rd, rs1, rs2" into inst: encodeSlt(rd, rs1, rs2, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.
bool encodeSltu(uint32_t, uint32_t, uint32_t, uint32_t& inst);
/// Encode "xor rd, rs1, rs2" into inst: encodeXor(rd, rs1, rs2, inst).
/// Return true on success and false if any of the arguments
/// are out of bounds.