Skip to content

Commit

Permalink
Add VNEG and LA pseudoinstructions
Browse files Browse the repository at this point in the history
[Draft]

An implementation is proposed for representing pseudoinstructions in Sail.

The realization is similar to instructions, with a few differences:

1. `union clause ast`: Same.

1. `mapping clause assembly`: Same.

1. `function clause execute`:

   This calls the execute clauses for the respective base instructions as
   defined for each pseudoinstruction.

   Note: There are two pseudoinstruction examples in this PR: LA and VNEG.
   LA maps to two base instructions, and VNEG maps to one.
   The implementation for LA compiles, but the implementation for VNEG fails:

   ```

   Error:
   model/riscv_insts_end.sail:24.16-23:
   24 |function clause execute C_ILLEGAL(s) = { handle_illegal(); RETIRE_FAIL }
      |                ^-----^
      | Function execute calls function marked non-executable

   ```

   ... a message which leaves more questions than answers for me.

1. `mapping clause encdec`: This is undefined for pseudoinstructions because,
   for one thing, some pseudoinstructions map to more than one instruction.
   This is replaced by a new scattered mapping:
   `function clause pseudo_of`: This maps the pseudoinstruction AST to a list
   of strings of the mapped instructions, for example:

   ```

   function clause pseudo_of(LA(rd, imm)) = [|
     "auipc" ^ spc() ^ reg_name(rd) ^ sep() ^ hex_bits_signed_20(imm[31..12]),
     "addi" ^ spc() ^ reg_name(rd) ^ sep()  ^"x0" ^ sep() ^ hex_bits_signed_12(imm[11..0])
   |]

   ```

   This is roughly analogous to `function clause assembly`, but instead
   of representing the instruction syntax, it represents the syntax of the
   mapped instructions.
  • Loading branch information
Linda-Njau authored and ThinkOpenly committed Aug 7, 2024
1 parent 2078d87 commit 36e4854
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions model/riscv_insts_base.sail
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ mapping itype_mnemonic : iop <-> string = {
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 = LA : (regidx, bits(32))

mapping clause assembly = LA(rd, imm)
<-> "la" ^ spc() ^ reg_name(rd) ^sep() ^ hex_bits_32(imm)

function clause pseudo_of(LA(rd, imm)) = [|
"auipc" ^ spc() ^ reg_name(rd) ^ sep() ^ hex_bits_signed_20(imm[31..12]),
"addi" ^ spc() ^ reg_name(rd) ^ sep() ^"x0" ^ sep() ^ hex_bits_signed_12(imm[11..0])
|]

function clause execute LA(rd, imm) = {
if execute(UTYPE(imm[31..12],rd,RISCV_AUIPC)) == RETIRE_SUCCESS &
execute(ITYPE(imm[11..0],reg_name("x0"),rd,RISCV_ADDI)) == RETIRE_SUCCESS then
RETIRE_SUCCESS
else
RETIRE_FAIL
}

/* ****************************************************************** */
union clause ast = SHIFTIOP : (bits(6), regidx, regidx, sop)

Expand Down
3 changes: 3 additions & 0 deletions model/riscv_insts_begin.sail
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ scattered mapping encdec
val encdec_compressed : ast <-> bits(16)
scattered mapping encdec_compressed

val pseudo_of : ast -> list(string)
scattered function pseudo_of

/*
* We declare the ILLEGAL/C_ILLEGAL ast clauses here instead of in
* riscv_insts_end, so that model extensions can make use of them.
Expand Down
2 changes: 2 additions & 0 deletions model/riscv_insts_end.sail
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Put the illegal instructions last to use their wildcard match. */

/* ****************************************************************** */
function clause pseudo_of(s) = [|"illegal"|]

mapping clause encdec = ILLEGAL(s) <-> s

Expand All @@ -27,6 +28,7 @@ mapping clause assembly = C_ILLEGAL(s) <-> "c.illegal" ^ spc() ^ hex_bits_16(s)
/* ****************************************************************** */

/* End definitions */
end pseudo_of
end extension
end extensionEnabled
end ast
Expand Down
15 changes: 15 additions & 0 deletions model/riscv_insts_vext_arith.sail
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,21 @@ mapping vxtype_mnemonic : vxfunct6 <-> string = {
mapping clause assembly = VXTYPE(funct6, vm, vs2, rs1, vd)
<-> vxtype_mnemonic(funct6) ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2) ^ sep() ^ reg_name(rs1) ^ maybe_vmask(vm)

/* ******************************************************************************* */
union clause ast = VNEG : (regidx, regidx)

mapping clause assembly = VNEG(vs, vd)
<-> "vneg.v" ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs)

function clause pseudo_of(VNEG(vs, vd)) = [|
"vrsub.vx" ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs) ^ sep() ^ "x0"
|]

function clause execute VNEG(vs, vd) = {
/* execute(VXTYPE(VX_VRSUB, maybe_vmask(""), vs, reg_name("x0"), vd)) */
RETIRE_FAIL
}

/* ************************** OPIVX (WXTYPE Narrowing) *************************** */
/* ************** Vector Narrowing Integer Right Shift Instructions ************** */
union clause ast = NXSTYPE : (nxsfunct6, bits(1), regidx, regidx, regidx)
Expand Down

0 comments on commit 36e4854

Please sign in to comment.