Skip to content

Commit

Permalink
Use extension discriminator for "A" and "M" extensions
Browse files Browse the repository at this point in the history
Using the `extensionEnabled()` function in `mapping clause encdec`
expressions for extensions allows a parser to clearly know when a
function is part of an extension (or set of extensions).
  • Loading branch information
Bakugo90 authored and ThinkOpenly committed Jul 9, 2024
1 parent 484a564 commit e2b8720
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
18 changes: 12 additions & 6 deletions model/riscv_insts_aext.sail
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ function amo_width_valid(size : word_width) -> bool = {
}

/* ****************************************************************** */
enum clause extensions = Ext_Zalrsc
function clause extensionEnabled(Ext_Zalrsc) = misa[A] == 0b1

union clause ast = LOADRES : (bool, bool, regidx, word_width, regidx)

mapping clause encdec = LOADRES(aq, rl, rs1, size, rd) if haveZalrsc() & amo_width_valid(size)
<-> 0b00010 @ bool_bits(aq) @ bool_bits(rl) @ 0b00000 @ rs1 @ 0b0 @ size_enc(size) @ rd @ 0b0101111 if haveZalrsc() & amo_width_valid(size)
mapping clause encdec = LOADRES(aq, rl, rs1, size, rd) if extensionEnabled(Ext_Zalrsc) & amo_width_valid(size)
<-> 0b00010 @ bool_bits(aq) @ bool_bits(rl) @ 0b00000 @ rs1 @ 0b0 @ size_enc(size) @ rd @ 0b0101111 if extensionEnabled(Ext_Zalrsc) & amo_width_valid(size)


/* We could set load-reservations on physical or virtual addresses.
Expand Down Expand Up @@ -88,8 +91,8 @@ mapping clause assembly = LOADRES(aq, rl, rs1, size, rd)
/* ****************************************************************** */
union clause ast = STORECON : (bool, bool, regidx, regidx, word_width, regidx)

mapping clause encdec = STORECON(aq, rl, rs2, rs1, size, rd) if haveZalrsc() & amo_width_valid(size)
<-> 0b00011 @ bool_bits(aq) @ bool_bits(rl) @ rs2 @ rs1 @ 0b0 @ size_enc(size) @ rd @ 0b0101111 if haveZalrsc() & amo_width_valid(size)
mapping clause encdec = STORECON(aq, rl, rs2, rs1, size, rd) if extensionEnabled(Ext_Zalrsc) & amo_width_valid(size)
<-> 0b00011 @ bool_bits(aq) @ bool_bits(rl) @ rs2 @ rs1 @ 0b0 @ size_enc(size) @ rd @ 0b0101111 if extensionEnabled(Ext_Zalrsc) & amo_width_valid(size)

/* NOTE: Currently, we only EA if address translation is successful. This may need revisiting. */
function clause execute (STORECON(aq, rl, rs2, rs1, width, rd)) = {
Expand Down Expand Up @@ -149,6 +152,9 @@ mapping clause assembly = STORECON(aq, rl, rs2, rs1, size, rd)
<-> "sc." ^ size_mnemonic(size) ^ maybe_aq(aq) ^ maybe_rl(rl) ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs2) ^ sep() ^ "(" ^ reg_name(rs1) ^ ")"

/* ****************************************************************** */
enum clause extensions = Ext_Zaamo
function clause extensionEnabled(Ext_Zaamo) = misa[A] == 0b1

union clause ast = AMO : (amoop, bool, bool, regidx, regidx, word_width, regidx)

mapping encdec_amoop : amoop <-> bits(5) = {
Expand All @@ -163,8 +169,8 @@ mapping encdec_amoop : amoop <-> bits(5) = {
AMOMAXU <-> 0b11100
}

mapping clause encdec = AMO(op, aq, rl, rs2, rs1, size, rd) if haveZaamo() & amo_width_valid(size)
<-> encdec_amoop(op) @ bool_bits(aq) @ bool_bits(rl) @ rs2 @ rs1 @ 0b0 @ size_enc(size) @ rd @ 0b0101111 if haveZaamo() & amo_width_valid(size)
mapping clause encdec = AMO(op, aq, rl, rs2, rs1, size, rd) if extensionEnabled(Ext_Zaamo) & amo_width_valid(size)
<-> encdec_amoop(op) @ bool_bits(aq) @ bool_bits(rl) @ rs2 @ rs1 @ 0b0 @ size_enc(size) @ rd @ 0b0101111 if extensionEnabled(Ext_Zaamo) & amo_width_valid(size)

/* NOTE: Currently, we only EA if address translation is successful.
This may need revisiting. */
Expand Down
30 changes: 18 additions & 12 deletions model/riscv_insts_mext.sail
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

/* ****************************************************************** */

enum clause extensions = Ext_M
function clause extensionEnabled(Ext_M) = misa[M] == 0b1
enum clause extensions = Ext_Zmmul
function clause extensionEnabled(Ext_Zmmul) = true


union clause ast = MUL : (regidx, regidx, regidx, mul_op)

mapping encdec_mul_op : mul_op <-> bits(3) = {
Expand All @@ -20,8 +26,8 @@ mapping encdec_mul_op : mul_op <-> bits(3) = {
struct { high = true, signed_rs1 = false, signed_rs2 = false } <-> 0b011
}

mapping clause encdec = MUL(rs2, rs1, rd, mul_op) if haveMulDiv() | haveZmmul()
<-> 0b0000001 @ rs2 @ rs1 @ encdec_mul_op(mul_op) @ rd @ 0b0110011 if haveMulDiv() | haveZmmul()
mapping clause encdec = MUL(rs2, rs1, rd, mul_op) if extensionEnabled(Ext_M) | extensionEnabled(Ext_Zmmul)
<-> 0b0000001 @ rs2 @ rs1 @ encdec_mul_op(mul_op) @ rd @ 0b0110011 if extensionEnabled(Ext_M) | extensionEnabled(Ext_Zmmul)

function clause execute (MUL(rs2, rs1, rd, mul_op)) = {
let rs1_val = X(rs1);
Expand Down Expand Up @@ -49,8 +55,8 @@ mapping clause assembly = MUL(rs2, rs1, rd, mul_op)
/* ****************************************************************** */
union clause ast = DIV : (regidx, regidx, regidx, bool)

mapping clause encdec = DIV(rs2, rs1, rd, s) if haveMulDiv()
<-> 0b0000001 @ rs2 @ rs1 @ 0b10 @ bool_not_bits(s) @ rd @ 0b0110011 if haveMulDiv()
mapping clause encdec = DIV(rs2, rs1, rd, s) if extensionEnabled(Ext_M)
<-> 0b0000001 @ rs2 @ rs1 @ 0b10 @ bool_not_bits(s) @ rd @ 0b0110011 if extensionEnabled(Ext_M)

function clause execute (DIV(rs2, rs1, rd, s)) = {
let rs1_val = X(rs1);
Expand All @@ -75,8 +81,8 @@ mapping clause assembly = DIV(rs2, rs1, rd, s)
/* ****************************************************************** */
union clause ast = REM : (regidx, regidx, regidx, bool)

mapping clause encdec = REM(rs2, rs1, rd, s) if haveMulDiv()
<-> 0b0000001 @ rs2 @ rs1 @ 0b11 @ bool_not_bits(s) @ rd @ 0b0110011 if haveMulDiv()
mapping clause encdec = REM(rs2, rs1, rd, s) if extensionEnabled(Ext_M)
<-> 0b0000001 @ rs2 @ rs1 @ 0b11 @ bool_not_bits(s) @ rd @ 0b0110011 if extensionEnabled(Ext_M)

function clause execute (REM(rs2, rs1, rd, s)) = {
let rs1_val = X(rs1);
Expand All @@ -96,9 +102,9 @@ mapping clause assembly = REM(rs2, rs1, rd, s)
union clause ast = MULW : (regidx, regidx, regidx)

mapping clause encdec = MULW(rs2, rs1, rd)
if sizeof(xlen) == 64 & (haveMulDiv() | haveZmmul())
if sizeof(xlen) == 64 & (extensionEnabled(Ext_M) | extensionEnabled(Ext_Zmmul))
<-> 0b0000001 @ rs2 @ rs1 @ 0b000 @ rd @ 0b0111011
if sizeof(xlen) == 64 & (haveMulDiv() | haveZmmul())
if sizeof(xlen) == 64 & (extensionEnabled(Ext_M) | extensionEnabled(Ext_Zmmul))

function clause execute (MULW(rs2, rs1, rd)) = {
let rs1_val = X(rs1)[31..0];
Expand All @@ -121,9 +127,9 @@ mapping clause assembly = MULW(rs2, rs1, rd)
union clause ast = DIVW : (regidx, regidx, regidx, bool)

mapping clause encdec = DIVW(rs2, rs1, rd, s)
if sizeof(xlen) == 64 & haveMulDiv()
if sizeof(xlen) == 64 & extensionEnabled(Ext_M)
<-> 0b0000001 @ rs2 @ rs1 @ 0b10 @ bool_not_bits(s) @ rd @ 0b0111011
if sizeof(xlen) == 64 & haveMulDiv()
if sizeof(xlen) == 64 & extensionEnabled(Ext_M)

function clause execute (DIVW(rs2, rs1, rd, s)) = {
let rs1_val = X(rs1)[31..0];
Expand All @@ -146,9 +152,9 @@ mapping clause assembly = DIVW(rs2, rs1, rd, s)
union clause ast = REMW : (regidx, regidx, regidx, bool)

mapping clause encdec = REMW(rs2, rs1, rd, s)
if sizeof(xlen) == 64 & haveMulDiv()
if sizeof(xlen) == 64 & extensionEnabled(Ext_M)
<-> 0b0000001 @ rs2 @ rs1 @ 0b11 @ bool_not_bits(s) @ rd @ 0b0111011
if sizeof(xlen) == 64 & haveMulDiv()
if sizeof(xlen) == 64 & extensionEnabled(Ext_M)

function clause execute (REMW(rs2, rs1, rd, s)) = {
let rs1_val = X(rs1)[31..0];
Expand Down
4 changes: 2 additions & 2 deletions model/riscv_insts_zcb.sail
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ function clause execute C_NOT(rsdc) = {
union clause ast = C_MUL : (cregidx, cregidx)

mapping clause encdec_compressed =
C_MUL(rsdc, rs2c) if haveZcb() & (haveMulDiv() | haveZmmul())
<-> 0b100 @ 0b111 @ rsdc : cregidx @ 0b10 @ rs2c : cregidx @ 0b01 if haveZcb() & (haveMulDiv() | haveZmmul())
C_MUL(rsdc, rs2c) if haveZcb() & (extensionEnabled(Ext_M) | extensionEnabled(Ext_Zmmul))
<-> 0b100 @ 0b111 @ rsdc : cregidx @ 0b10 @ rs2c : cregidx @ 0b01 if haveZcb() & (extensionEnabled(Ext_M) | extensionEnabled(Ext_Zmmul))

mapping clause assembly = C_MUL(rsdc, rs2c) <->
"c.mul" ^ spc() ^ creg_name(rsdc) ^ sep() ^ creg_name(rs2c)
Expand Down
8 changes: 0 additions & 8 deletions model/riscv_sys_regs.sail
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ function legalize_misa(m : Misa, v : xlenbits) -> Misa = {

/* helpers to check support for various extensions. */
/* we currently don't model 'E', so always assume 'I'. */
function haveAtomics() -> bool = misa[A] == 0b1
function haveMulDiv() -> bool = misa[M] == 0b1
function haveSupMode() -> bool = misa[S] == 0b1
function haveUsrMode() -> bool = misa[U] == 0b1
function haveNExt() -> bool = misa[N] == 0b1
Expand All @@ -138,12 +136,6 @@ function haveNExt() -> bool = misa[N] == 0b1
function haveZba() -> bool = true
function haveZbb() -> bool = true

function haveZmmul() -> bool = true

/* A extension sub-extensions */
function haveZaamo() -> bool = haveAtomics()
function haveZalrsc() -> bool = haveAtomics()

/*
* Illegal values legalized to least privileged mode supported.
* Note: the only valid combinations of supported modes are M, M+U, M+S+U.
Expand Down

0 comments on commit e2b8720

Please sign in to comment.