forked from riscv/sail-riscv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riscv_insts_base.sail
725 lines (605 loc) · 25.8 KB
/
riscv_insts_base.sail
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
/*=======================================================================================*/
/* This Sail RISC-V architecture model, comprising all files and */
/* directories except where otherwise noted is subject the BSD */
/* two-clause license in the LICENSE file. */
/* */
/* SPDX-License-Identifier: BSD-2-Clause */
/*=======================================================================================*/
/* ****************************************************************** */
/* This file specifies the instructions in the base integer set. */
enum clause extension = Ext_C
function clause extensionEnabled(Ext_C) = misa[C] == 0b1
enum clause extension = Ext_Zca
function clause extensionEnabled(Ext_Zca) = extensionEnabled(Ext_C)
/* ****************************************************************** */
union clause ast = UTYPE : (bits(20), regidx, uop)
mapping encdec_uop : uop <-> bits(7) = {
RISCV_LUI <-> 0b0110111,
RISCV_AUIPC <-> 0b0010111
}
mapping clause encdec = UTYPE(imm, rd, op)
<-> imm @ rd @ encdec_uop(op)
function clause execute UTYPE(imm, rd, op) = {
let off : xlenbits = sign_extend(imm @ 0x000);
let ret : xlenbits = match op {
RISCV_LUI => off,
RISCV_AUIPC => get_arch_pc() + off
};
X(rd) = ret;
RETIRE_SUCCESS
}
mapping utype_mnemonic : uop <-> string = {
RISCV_LUI <-> "lui",
RISCV_AUIPC <-> "auipc"
}
mapping clause assembly = UTYPE(imm, rd, op)
<-> utype_mnemonic(op) ^ spc() ^ reg_name(rd) ^ sep() ^ hex_bits_signed_20(imm)
/* ****************************************************************** */
union clause ast = RISCV_JAL : (bits(21), regidx)
mapping clause encdec = RISCV_JAL(imm_19 @ imm_7_0 @ imm_8 @ imm_18_13 @ imm_12_9 @ 0b0, rd)
<-> imm_19 : bits(1) @ imm_18_13 : bits(6) @ imm_12_9 : bits(4) @ imm_8 : bits(1) @ imm_7_0 : bits(8) @ rd @ 0b1101111
/*
ideally we want some syntax like
mapping clause encdec = RISCV_JAL(imm @ 0b0, rd) <-> imm[19] @ imm[9..0] @ imm[10] @ imm[18..11] @ rd @ 0b1101111
match bv {
imm[19] @ imm[9..0] @ imm[10] @ imm[18..11] -> imm @ 0b0
}
but this is difficult
*/
function clause execute (RISCV_JAL(imm, rd)) = {
let t : xlenbits = PC + sign_extend(imm);
/* Extensions get the first checks on the prospective target address. */
match ext_control_check_pc(t) {
Ext_ControlAddr_Error(e) => {
ext_handle_control_check_error(e);
RETIRE_FAIL
},
Ext_ControlAddr_OK(target) => {
/* Perform standard alignment check */
if bit_to_bool(target[1]) & not(extensionEnabled(Ext_Zca))
then {
handle_mem_exception(target, E_Fetch_Addr_Align());
RETIRE_FAIL
} else {
X(rd) = get_next_pc();
set_next_pc(target);
RETIRE_SUCCESS
}
}
}
}
/* TODO: handle 2-byte-alignment in mappings */
mapping clause assembly = RISCV_JAL(imm, rd)
<-> "jal" ^ spc() ^ reg_name(rd) ^ sep() ^ hex_bits_signed_21(imm)
/* ****************************************************************** */
union clause ast = RISCV_JALR : (bits(12), regidx, regidx)
mapping clause encdec = RISCV_JALR(imm, rs1, rd)
<-> imm @ rs1 @ 0b000 @ rd @ 0b1100111
mapping clause assembly = RISCV_JALR(imm, rs1, rd)
<-> "jalr" ^ spc() ^ reg_name(rd) ^ sep() ^ hex_bits_signed_12(imm) ^ "(" ^ reg_name(rs1) ^ ")"
/* see riscv_jalr_seq.sail or riscv_jalr_rmem.sail for the execute clause. */
/* ****************************************************************** */
union clause ast = BTYPE : (bits(13), regidx, regidx, bop)
mapping encdec_bop : bop <-> bits(3) = {
RISCV_BEQ <-> 0b000,
RISCV_BNE <-> 0b001,
RISCV_BLT <-> 0b100,
RISCV_BGE <-> 0b101,
RISCV_BLTU <-> 0b110,
RISCV_BGEU <-> 0b111
}
mapping clause encdec = BTYPE(imm7_6 @ imm5_0 @ imm7_5_0 @ imm5_4_1 @ 0b0, rs2, rs1, op)
<-> imm7_6 : bits(1) @ imm7_5_0 : bits(6) @ rs2 @ rs1 @ encdec_bop(op) @ imm5_4_1 : bits(4) @ imm5_0 : bits(1) @ 0b1100011
function clause execute (BTYPE(imm, rs2, rs1, op)) = {
let rs1_val = X(rs1);
let rs2_val = X(rs2);
let taken : bool = match op {
RISCV_BEQ => rs1_val == rs2_val,
RISCV_BNE => rs1_val != rs2_val,
RISCV_BLT => rs1_val <_s rs2_val,
RISCV_BGE => rs1_val >=_s rs2_val,
RISCV_BLTU => rs1_val <_u rs2_val,
RISCV_BGEU => rs1_val >=_u rs2_val
};
let t : xlenbits = PC + sign_extend(imm);
if taken then {
/* Extensions get the first checks on the prospective target address. */
match ext_control_check_pc(t) {
Ext_ControlAddr_Error(e) => {
ext_handle_control_check_error(e);
RETIRE_FAIL
},
Ext_ControlAddr_OK(target) => {
if bit_to_bool(target[1]) & not(extensionEnabled(Ext_Zca)) then {
handle_mem_exception(target, E_Fetch_Addr_Align());
RETIRE_FAIL;
} else {
set_next_pc(target);
RETIRE_SUCCESS
}
}
}
} else RETIRE_SUCCESS
}
mapping btype_mnemonic : bop <-> string = {
RISCV_BEQ <-> "beq",
RISCV_BNE <-> "bne",
RISCV_BLT <-> "blt",
RISCV_BGE <-> "bge",
RISCV_BLTU <-> "bltu",
RISCV_BGEU <-> "bgeu"
}
mapping clause assembly = BTYPE(imm, rs2, rs1, op)
<-> btype_mnemonic(op) ^ spc() ^ reg_name(rs1) ^ sep() ^ reg_name(rs2) ^ sep() ^ hex_bits_signed_13(imm)
/* ****************************************************************** */
union clause ast = ITYPE : (bits(12), regidx, regidx, iop)
mapping encdec_iop : iop <-> bits(3) = {
RISCV_ADDI <-> 0b000,
RISCV_SLTI <-> 0b010,
RISCV_SLTIU <-> 0b011,
RISCV_ANDI <-> 0b111,
RISCV_ORI <-> 0b110,
RISCV_XORI <-> 0b100
}
mapping clause encdec = ITYPE(imm, rs1, rd, op)
<-> imm @ rs1 @ encdec_iop(op) @ rd @ 0b0010011
function clause execute (ITYPE (imm, rs1, rd, op)) = {
let rs1_val = X(rs1);
let immext : xlenbits = sign_extend(imm);
let result : xlenbits = match op {
RISCV_ADDI => rs1_val + immext,
RISCV_SLTI => zero_extend(bool_to_bits(rs1_val <_s immext)),
RISCV_SLTIU => zero_extend(bool_to_bits(rs1_val <_u immext)),
RISCV_ANDI => rs1_val & immext,
RISCV_ORI => rs1_val | immext,
RISCV_XORI => rs1_val ^ immext
};
X(rd) = result;
RETIRE_SUCCESS
}
mapping itype_mnemonic : iop <-> string = {
RISCV_ADDI <-> "addi",
RISCV_SLTI <-> "slti",
RISCV_SLTIU <-> "sltiu",
RISCV_XORI <-> "xori",
RISCV_ORI <-> "ori",
RISCV_ANDI <-> "andi"
}
mapping clause assembly = ITYPE(imm, rs1, rd, op)
<-> itype_mnemonic(op) ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ hex_bits_signed_12(imm)
/* ****************************************************************** */
union clause ast = SHIFTIOP : (bits(6), regidx, regidx, sop)
mapping encdec_sop : sop <-> bits(3) = {
RISCV_SLLI <-> 0b001,
RISCV_SRLI <-> 0b101,
RISCV_SRAI <-> 0b101
}
mapping clause encdec = SHIFTIOP(shamt, rs1, rd, RISCV_SLLI) <-> 0b000000 @ shamt @ rs1 @ 0b001 @ rd @ 0b0010011 if xlen == 64 | shamt[5] == bitzero
mapping clause encdec = SHIFTIOP(shamt, rs1, rd, RISCV_SRLI) <-> 0b000000 @ shamt @ rs1 @ 0b101 @ rd @ 0b0010011 if xlen == 64 | shamt[5] == bitzero
mapping clause encdec = SHIFTIOP(shamt, rs1, rd, RISCV_SRAI) <-> 0b010000 @ shamt @ rs1 @ 0b101 @ rd @ 0b0010011 if xlen == 64 | shamt[5] == bitzero
function clause execute (SHIFTIOP(shamt, rs1, rd, op)) = {
let rs1_val = X(rs1);
/* the decoder guard should ensure that shamt[5] = 0 for RV32 */
let result : xlenbits = match op {
RISCV_SLLI => if xlen == 32
then rs1_val << shamt[4..0]
else rs1_val << shamt,
RISCV_SRLI => if xlen == 32
then rs1_val >> shamt[4..0]
else rs1_val >> shamt,
RISCV_SRAI => if xlen == 32
then shift_right_arith32(rs1_val, shamt[4..0])
else shift_right_arith64(rs1_val, shamt)
};
X(rd) = result;
RETIRE_SUCCESS
}
mapping shiftiop_mnemonic : sop <-> string = {
RISCV_SLLI <-> "slli",
RISCV_SRLI <-> "srli",
RISCV_SRAI <-> "srai"
}
mapping clause assembly = SHIFTIOP(shamt, rs1, rd, op)
<-> shiftiop_mnemonic(op) ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ hex_bits_6(shamt)
/* ****************************************************************** */
union clause ast = RTYPE : (regidx, regidx, regidx, rop)
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_ADD) <-> 0b0000000 @ rs2 @ rs1 @ 0b000 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_SLT) <-> 0b0000000 @ rs2 @ rs1 @ 0b010 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_SLTU) <-> 0b0000000 @ rs2 @ rs1 @ 0b011 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_AND) <-> 0b0000000 @ rs2 @ rs1 @ 0b111 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_OR) <-> 0b0000000 @ rs2 @ rs1 @ 0b110 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_XOR) <-> 0b0000000 @ rs2 @ rs1 @ 0b100 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_SLL) <-> 0b0000000 @ rs2 @ rs1 @ 0b001 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_SRL) <-> 0b0000000 @ rs2 @ rs1 @ 0b101 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_SUB) <-> 0b0100000 @ rs2 @ rs1 @ 0b000 @ rd @ 0b0110011
mapping clause encdec = RTYPE(rs2, rs1, rd, RISCV_SRA) <-> 0b0100000 @ rs2 @ rs1 @ 0b101 @ rd @ 0b0110011
function clause execute (RTYPE(rs2, rs1, rd, op)) = {
let rs1_val = X(rs1);
let rs2_val = X(rs2);
let result : xlenbits = match op {
RISCV_ADD => rs1_val + rs2_val,
RISCV_SLT => zero_extend(bool_to_bits(rs1_val <_s rs2_val)),
RISCV_SLTU => zero_extend(bool_to_bits(rs1_val <_u rs2_val)),
RISCV_AND => rs1_val & rs2_val,
RISCV_OR => rs1_val | rs2_val,
RISCV_XOR => rs1_val ^ rs2_val,
RISCV_SLL => if xlen == 32
then rs1_val << (rs2_val[4..0])
else rs1_val << (rs2_val[5..0]),
RISCV_SRL => if xlen == 32
then rs1_val >> (rs2_val[4..0])
else rs1_val >> (rs2_val[5..0]),
RISCV_SUB => rs1_val - rs2_val,
RISCV_SRA => if xlen == 32
then shift_right_arith32(rs1_val, rs2_val[4..0])
else shift_right_arith64(rs1_val, rs2_val[5..0])
};
X(rd) = result;
RETIRE_SUCCESS
}
mapping rtype_mnemonic : rop <-> string = {
RISCV_ADD <-> "add",
RISCV_SLT <-> "slt",
RISCV_SLTU <-> "sltu",
RISCV_AND <-> "and",
RISCV_OR <-> "or",
RISCV_XOR <-> "xor",
RISCV_SLL <-> "sll",
RISCV_SRL <-> "srl",
RISCV_SUB <-> "sub",
RISCV_SRA <-> "sra"
}
mapping clause assembly = RTYPE(rs2, rs1, rd, op)
<-> rtype_mnemonic(op) ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ reg_name(rs2)
/* ****************************************************************** */
union clause ast = LOAD : (bits(12), regidx, regidx, bool, word_width, bool, bool)
/* unsigned loads are only present for widths strictly less than xlen,
signed loads also present for widths equal to xlen */
mapping clause encdec = LOAD(imm, rs1, rd, is_unsigned, size, false, false) if (size_bytes(size) < xlen_bytes) | (not(is_unsigned) & size_bytes(size) <= xlen_bytes)
<-> imm @ rs1 @ bool_bits(is_unsigned) @ size_enc(size) @ rd @ 0b0000011 if (size_bytes(size) < xlen_bytes) | (not(is_unsigned) & size_bytes(size) <= xlen_bytes)
val extend_value : forall 'n, 0 < 'n <= xlen. (bool, bits('n)) -> xlenbits
function extend_value(is_unsigned, value) = if is_unsigned then zero_extend(value) else sign_extend(value)
function is_aligned(vaddr : xlenbits, width : word_width) -> bool =
match width {
BYTE => true,
HALF => vaddr[0..0] == zeros(),
WORD => vaddr[1..0] == zeros(),
DOUBLE => vaddr[2..0] == zeros(),
}
// Return true if the address is misaligned and we don't support misaligned access.
function check_misaligned(vaddr : xlenbits, width : word_width) -> bool =
not(plat_enable_misaligned_access()) & not(is_aligned(vaddr, width))
function clause execute(LOAD(imm, rs1, rd, is_unsigned, width, aq, rl)) = {
let offset : xlenbits = sign_extend(imm);
let width_bytes = size_bytes(width);
// This is checked during decoding.
assert(width_bytes <= xlen_bytes);
/* Get the address, X(rs1) + offset.
Some extensions perform additional checks on address validity. */
match ext_data_get_addr(rs1, offset, Read(Data), width_bytes) {
Ext_DataAddr_Error(e) => { ext_handle_data_check_error(e); RETIRE_FAIL },
Ext_DataAddr_OK(vaddr) => {
if check_misaligned(vaddr, width)
then { handle_mem_exception(vaddr, E_Load_Addr_Align()); RETIRE_FAIL }
else match translateAddr(vaddr, Read(Data)) {
TR_Failure(e, _) => { handle_mem_exception(vaddr, e); RETIRE_FAIL },
TR_Address(paddr, _) =>
match mem_read(Read(Data), paddr, width_bytes, aq, rl, false) {
MemValue(result) => { X(rd) = extend_value(is_unsigned, result); RETIRE_SUCCESS },
MemException(e) => { handle_mem_exception(vaddr, e); RETIRE_FAIL },
},
}
},
}
}
val maybe_aq : bool <-> string
mapping maybe_aq = {
true <-> ".aq",
false <-> ""
}
val maybe_rl : bool <-> string
mapping maybe_rl = {
true <-> ".rl",
false <-> ""
}
val maybe_u : bool <-> string
mapping maybe_u = {
true <-> "u",
false <-> ""
}
mapping clause assembly = LOAD(imm, rs1, rd, is_unsigned, size, aq, rl)
<-> "l" ^ size_mnemonic(size) ^ maybe_u(is_unsigned) ^ maybe_aq(aq) ^ maybe_rl(rl) ^ spc() ^ reg_name(rd) ^ sep() ^ hex_bits_signed_12(imm) ^ "(" ^ reg_name(rs1) ^ ")"
/* ****************************************************************** */
union clause ast = STORE : (bits(12), regidx, regidx, word_width, bool, bool)
mapping clause encdec = STORE(imm7 @ imm5, rs2, rs1, size, false, false) if size_bytes(size) <= xlen_bytes
<-> imm7 : bits(7) @ rs2 @ rs1 @ 0b0 @ size_enc(size) @ imm5 : bits(5) @ 0b0100011 if size_bytes(size) <= xlen_bytes
/* NOTE: Currently, we only EA if address translation is successful.
This may need revisiting. */
function clause execute (STORE(imm, rs2, rs1, width, aq, rl)) = {
let offset : xlenbits = sign_extend(imm);
let width_bytes = size_bytes(width);
// This is checked during decoding.
assert(width_bytes <= xlen_bytes);
/* Get the address, X(rs1) + offset.
Some extensions perform additional checks on address validity. */
match ext_data_get_addr(rs1, offset, Write(Data), width_bytes) {
Ext_DataAddr_Error(e) => { ext_handle_data_check_error(e); RETIRE_FAIL },
Ext_DataAddr_OK(vaddr) =>
if check_misaligned(vaddr, width)
then { handle_mem_exception(vaddr, E_SAMO_Addr_Align()); RETIRE_FAIL }
else match translateAddr(vaddr, Write(Data)) {
TR_Failure(e, _) => { handle_mem_exception(vaddr, e); RETIRE_FAIL },
TR_Address(paddr, _) => {
let eares = mem_write_ea(paddr, width_bytes, aq, rl, false);
match (eares) {
MemException(e) => { handle_mem_exception(vaddr, e); RETIRE_FAIL },
MemValue(_) => {
let rs2_val = X(rs2);
match mem_write_value(paddr, width_bytes, rs2_val[width_bytes * 8 - 1 .. 0], aq, rl, false) {
MemValue(true) => RETIRE_SUCCESS,
MemValue(false) => internal_error(__FILE__, __LINE__, "store got false from mem_write_value"),
MemException(e) => { handle_mem_exception(vaddr, e); RETIRE_FAIL }
}
}
}
}
}
}
}
mapping clause assembly = STORE(imm, rs2, rs1, size, aq, rl)
<-> "s" ^ size_mnemonic(size) ^ maybe_aq(aq) ^ maybe_rl(rl) ^ spc() ^ reg_name(rs2) ^ sep() ^ hex_bits_signed_12(imm) ^ opt_spc() ^ "(" ^ opt_spc() ^ reg_name(rs1) ^ opt_spc() ^ ")"
/* ****************************************************************** */
union clause ast = ADDIW : (bits(12), regidx, regidx)
mapping clause encdec = ADDIW(imm, rs1, rd)
if xlen == 64
<-> imm @ rs1 @ 0b000 @ rd @ 0b0011011
if xlen == 64
function clause execute (ADDIW(imm, rs1, rd)) = {
let result : xlenbits = sign_extend(imm) + X(rs1);
X(rd) = sign_extend(result[31..0]);
RETIRE_SUCCESS
}
mapping clause assembly = ADDIW(imm, rs1, rd)
if xlen == 64
<-> "addiw" ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ hex_bits_signed_12(imm)
if xlen == 64
/* ****************************************************************** */
union clause ast = RTYPEW : (regidx, regidx, regidx, ropw)
mapping clause encdec = RTYPEW(rs2, rs1, rd, RISCV_ADDW)
if xlen == 64
<-> 0b0000000 @ rs2 @ rs1 @ 0b000 @ rd @ 0b0111011
if xlen == 64
mapping clause encdec = RTYPEW(rs2, rs1, rd, RISCV_SUBW)
if xlen == 64
<-> 0b0100000 @ rs2 @ rs1 @ 0b000 @ rd @ 0b0111011
if xlen == 64
mapping clause encdec = RTYPEW(rs2, rs1, rd, RISCV_SLLW)
if xlen == 64
<-> 0b0000000 @ rs2 @ rs1 @ 0b001 @ rd @ 0b0111011
if xlen == 64
mapping clause encdec = RTYPEW(rs2, rs1, rd, RISCV_SRLW)
if xlen == 64
<-> 0b0000000 @ rs2 @ rs1 @ 0b101 @ rd @ 0b0111011
if xlen == 64
mapping clause encdec = RTYPEW(rs2, rs1, rd, RISCV_SRAW)
if xlen == 64
<-> 0b0100000 @ rs2 @ rs1 @ 0b101 @ rd @ 0b0111011
if xlen == 64
function clause execute (RTYPEW(rs2, rs1, rd, op)) = {
let rs1_val = (X(rs1))[31..0];
let rs2_val = (X(rs2))[31..0];
let result : bits(32) = match op {
RISCV_ADDW => rs1_val + rs2_val,
RISCV_SUBW => rs1_val - rs2_val,
RISCV_SLLW => rs1_val << (rs2_val[4..0]),
RISCV_SRLW => rs1_val >> (rs2_val[4..0]),
RISCV_SRAW => shift_right_arith32(rs1_val, rs2_val[4..0])
};
X(rd) = sign_extend(result);
RETIRE_SUCCESS
}
mapping rtypew_mnemonic : ropw <-> string = {
RISCV_ADDW <-> "addw",
RISCV_SUBW <-> "subw",
RISCV_SLLW <-> "sllw",
RISCV_SRLW <-> "srlw",
RISCV_SRAW <-> "sraw"
}
mapping clause assembly = RTYPEW(rs2, rs1, rd, op)
if xlen == 64
<-> rtypew_mnemonic(op) ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ reg_name(rs2)
if xlen == 64
/* ****************************************************************** */
union clause ast = SHIFTIWOP : (bits(5), regidx, regidx, sopw)
mapping clause encdec = SHIFTIWOP(shamt, rs1, rd, RISCV_SLLIW)
if xlen == 64
<-> 0b0000000 @ shamt @ rs1 @ 0b001 @ rd @ 0b0011011
if xlen == 64
mapping clause encdec = SHIFTIWOP(shamt, rs1, rd, RISCV_SRLIW)
if xlen == 64
<-> 0b0000000 @ shamt @ rs1 @ 0b101 @ rd @ 0b0011011
if xlen == 64
mapping clause encdec = SHIFTIWOP(shamt, rs1, rd, RISCV_SRAIW)
if xlen == 64
<-> 0b0100000 @ shamt @ rs1 @ 0b101 @ rd @ 0b0011011
if xlen == 64
function clause execute (SHIFTIWOP(shamt, rs1, rd, op)) = {
let rs1_val = (X(rs1))[31..0];
let result : bits(32) = match op {
RISCV_SLLIW => rs1_val << shamt,
RISCV_SRLIW => rs1_val >> shamt,
RISCV_SRAIW => shift_right_arith32(rs1_val, shamt)
};
X(rd) = sign_extend(result);
RETIRE_SUCCESS
}
mapping shiftiwop_mnemonic : sopw <-> string = {
RISCV_SLLIW <-> "slliw",
RISCV_SRLIW <-> "srliw",
RISCV_SRAIW <-> "sraiw"
}
mapping clause assembly = SHIFTIWOP(shamt, rs1, rd, op)
if xlen == 64
<-> shiftiwop_mnemonic(op) ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ hex_bits_5(shamt)
if xlen == 64
/* ****************************************************************** */
union clause ast = FENCE : (bits(4), bits(4))
mapping clause encdec = FENCE(pred, succ)
<-> 0b0000 @ pred @ succ @ 0b00000 @ 0b000 @ 0b00000 @ 0b0001111
function effective_fence_set(set : bits(4), fiom : bool) -> bits(4) = {
// The bits are IORW. If FIOM is set then I implies R and O implies W.
if fiom then {
set[3 .. 2] @ (set[1 .. 0] | set[3 .. 2])
} else set
}
function clause execute (FENCE(pred, succ)) = {
// If the FIOM bit in menvcfg/senvcfg is set then the I/O bits can imply R/W.
let fiom = is_fiom_active();
let pred = effective_fence_set(pred, fiom);
let succ = effective_fence_set(succ, fiom);
match (pred, succ) {
(_ : bits(2) @ 0b11, _ : bits(2) @ 0b11) => sail_barrier(Barrier_RISCV_rw_rw),
(_ : bits(2) @ 0b10, _ : bits(2) @ 0b11) => sail_barrier(Barrier_RISCV_r_rw),
(_ : bits(2) @ 0b10, _ : bits(2) @ 0b10) => sail_barrier(Barrier_RISCV_r_r),
(_ : bits(2) @ 0b11, _ : bits(2) @ 0b01) => sail_barrier(Barrier_RISCV_rw_w),
(_ : bits(2) @ 0b01, _ : bits(2) @ 0b01) => sail_barrier(Barrier_RISCV_w_w),
(_ : bits(2) @ 0b01, _ : bits(2) @ 0b11) => sail_barrier(Barrier_RISCV_w_rw),
(_ : bits(2) @ 0b11, _ : bits(2) @ 0b10) => sail_barrier(Barrier_RISCV_rw_r),
(_ : bits(2) @ 0b10, _ : bits(2) @ 0b01) => sail_barrier(Barrier_RISCV_r_w),
(_ : bits(2) @ 0b01, _ : bits(2) @ 0b10) => sail_barrier(Barrier_RISCV_w_r),
(_ : bits(4) , _ : bits(2) @ 0b00) => (),
(_ : bits(2) @ 0b00, _ : bits(4) ) => (),
_ => { print("FIXME: unsupported fence");
() }
};
RETIRE_SUCCESS
}
mapping bit_maybe_r : bits(1) <-> string = {
0b1 <-> "r",
0b0 <-> ""
}
mapping bit_maybe_w : bits(1) <-> string = {
0b1 <-> "w",
0b0 <-> ""
}
mapping bit_maybe_i : bits(1) <-> string = {
0b1 <-> "i",
0b0 <-> ""
}
mapping bit_maybe_o : bits(1) <-> string = {
0b1 <-> "o",
0b0 <-> ""
}
mapping fence_bits : bits(4) <-> string = {
i : bits(1) @ o : bits(1) @ r : bits(1) @ w : bits(1) <-> bit_maybe_i(i) ^ bit_maybe_o(o) ^ bit_maybe_r(r) ^ bit_maybe_w(w)
}
mapping clause assembly = FENCE(pred, succ)
<-> "fence" ^ spc() ^ fence_bits(pred) ^ sep() ^ fence_bits(succ)
/* ****************************************************************** */
union clause ast = FENCE_TSO : (bits(4), bits(4))
mapping clause encdec = FENCE_TSO(pred, succ)
<-> 0b1000 @ pred @ succ @ 0b00000 @ 0b000 @ 0b00000 @ 0b0001111
function clause execute (FENCE_TSO(pred, succ)) = {
match (pred, succ) {
(_ : bits(2) @ 0b11, _ : bits(2) @ 0b11) => sail_barrier(Barrier_RISCV_tso),
(_ : bits(2) @ 0b00, _ : bits(2) @ 0b00) => (),
_ => { print("FIXME: unsupported fence");
() }
};
RETIRE_SUCCESS
}
mapping clause assembly = FENCE_TSO(pred, succ)
<-> "fence.tso" ^ spc() ^ fence_bits(pred) ^ sep() ^ fence_bits(succ)
/* ****************************************************************** */
union clause ast = ECALL : unit
mapping clause encdec = ECALL()
<-> 0b000000000000 @ 0b00000 @ 0b000 @ 0b00000 @ 0b1110011
function clause execute ECALL() = {
let t : sync_exception =
struct { trap = match (cur_privilege) {
User => E_U_EnvCall(),
Supervisor => E_S_EnvCall(),
Machine => E_M_EnvCall()
},
excinfo = (None() : option(xlenbits)),
ext = None() };
set_next_pc(exception_handler(cur_privilege, CTL_TRAP(t), PC));
RETIRE_FAIL
}
mapping clause assembly = ECALL() <-> "ecall"
/* ****************************************************************** */
union clause ast = MRET : unit
mapping clause encdec = MRET()
<-> 0b0011000 @ 0b00010 @ 0b00000 @ 0b000 @ 0b00000 @ 0b1110011
function clause execute MRET() = {
if cur_privilege != Machine
then { handle_illegal(); RETIRE_FAIL }
else if not(ext_check_xret_priv (Machine))
then { ext_fail_xret_priv(); RETIRE_FAIL }
else {
set_next_pc(exception_handler(cur_privilege, CTL_MRET(), PC));
RETIRE_SUCCESS
}
}
mapping clause assembly = MRET() <-> "mret"
/* ****************************************************************** */
union clause ast = SRET : unit
mapping clause encdec = SRET()
<-> 0b0001000 @ 0b00010 @ 0b00000 @ 0b000 @ 0b00000 @ 0b1110011
function clause execute SRET() = {
let sret_illegal : bool = match cur_privilege {
User => true,
Supervisor => not(extensionEnabled(Ext_S)) | mstatus[TSR] == 0b1,
Machine => not(extensionEnabled(Ext_S))
};
if sret_illegal
then { handle_illegal(); RETIRE_FAIL }
else if not(ext_check_xret_priv (Supervisor))
then { ext_fail_xret_priv(); RETIRE_FAIL }
else {
set_next_pc(exception_handler(cur_privilege, CTL_SRET(), PC));
RETIRE_SUCCESS
}
}
mapping clause assembly = SRET() <-> "sret"
/* ****************************************************************** */
union clause ast = EBREAK : unit
mapping clause encdec = EBREAK()
<-> 0b000000000001 @ 0b00000 @ 0b000 @ 0b00000 @ 0b1110011
function clause execute EBREAK() = {
handle_mem_exception(PC, E_Breakpoint());
RETIRE_FAIL
}
mapping clause assembly = EBREAK() <-> "ebreak"
/* ****************************************************************** */
union clause ast = WFI : unit
mapping clause encdec = WFI()
<-> 0b000100000101 @ 0b00000 @ 0b000 @ 0b00000 @ 0b1110011
function clause execute WFI() =
match cur_privilege {
Machine => { platform_wfi(); RETIRE_SUCCESS },
Supervisor => if mstatus[TW] == 0b1
then { handle_illegal(); RETIRE_FAIL }
else { platform_wfi(); RETIRE_SUCCESS },
User => { handle_illegal(); RETIRE_FAIL }
}
mapping clause assembly = WFI() <-> "wfi"
/* ****************************************************************** */
union clause ast = SFENCE_VMA : (regidx, regidx)
mapping clause encdec = SFENCE_VMA(rs1, rs2)
<-> 0b0001001 @ rs2 @ rs1 @ 0b000 @ 0b00000 @ 0b1110011
function clause execute SFENCE_VMA(rs1, rs2) = {
let addr : option(xlenbits) = if rs1 == 0b00000 then None() else Some(X(rs1));
let asid : option(xlenbits) = if rs2 == 0b00000 then None() else Some(X(rs2));
match cur_privilege {
User => { handle_illegal(); RETIRE_FAIL },
Supervisor => match (architecture(get_mstatus_SXL(mstatus)), mstatus[TVM]) {
(Some(_), 0b1) => { handle_illegal(); RETIRE_FAIL },
(Some(_), 0b0) => { flush_TLB(asid, addr); RETIRE_SUCCESS },
(_, _) => internal_error(__FILE__, __LINE__, "unimplemented sfence architecture")
},
Machine => { flush_TLB(asid, addr); RETIRE_SUCCESS }
}
}
mapping clause assembly = SFENCE_VMA(rs1, rs2)
<-> "sfence.vma" ^ spc() ^ reg_name(rs1) ^ sep() ^ reg_name(rs2)