| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import List. Import ListNotations. |
| 3 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 4 |
Require Import Crypto.Util.Tuple. |
| 5 |
Require Import Crypto.Util.Notations. |
| 6 |
Local Open Scope Z_scope. |
| 7 |
|
| 8 |
(* Condition code flags *) |
| 9 |
Module CC. |
| 10 |
Inductive code : Type := |
| 11 |
| C : code |
| 12 |
| M : code |
| 13 |
| L : code |
| 14 |
| Z : code |
| 15 |
. |
| 16 |
|
| 17 |
Record state := |
| 18 |
{ cc_c : bool; cc_m : bool; cc_l : bool; cc_z : bool }. |
| 19 |
|
| 20 |
Definition code_dec (x y : code) : {x = y} + {x <> y}. |
| 21 |
Proof. destruct x, y; try apply (left eq_refl); right; congruence. Defined. |
| 22 |
|
| 23 |
Definition update (to_write : list code) (result : BinInt.Z) (cc_spec : code -> BinInt.Z -> bool) (old_state : state) |
| 24 |
: state := |
| 25 |
{| |
| 26 |
cc_c := if (In_dec code_dec C to_write) |
| 27 |
then cc_spec C result |
| 28 |
else old_state.(cc_c); |
| 29 |
cc_m := if (In_dec code_dec M to_write) |
| 30 |
then cc_spec M result |
| 31 |
else old_state.(cc_m); |
| 32 |
cc_l := if (In_dec code_dec L to_write) |
| 33 |
then cc_spec L result |
| 34 |
else old_state.(cc_l); |
| 35 |
cc_z := if (In_dec code_dec Z to_write) |
| 36 |
then cc_spec Z result |
| 37 |
else old_state.(cc_z) |
| 38 |
|}. |
| 39 |
|
| 40 |
End CC. |
| 41 |
|
| 42 |
Section Language. |
| 43 |
Record instruction := |
| 44 |
{ |
| 45 |
num_source_regs : nat; |
| 46 |
writes_conditions : list CC.code; |
| 47 |
spec : tuple Z num_source_regs -> CC.state -> Z |
| 48 |
}. |
| 49 |
|
| 50 |
Section expr. |
| 51 |
Context {name : Type} (name_eqb : name -> name -> bool) (wordmax : Z) (cc_spec : CC.code -> Z -> bool). |
| 52 |
|
| 53 |
Inductive expr := |
| 54 |
| Ret : name -> expr |
| 55 |
| Instr (i : instruction) |
| 56 |
(rd : name) (* destination register *) |
| 57 |
(args : tuple name i.(num_source_regs)) (* source registers *) |
| 58 |
(cont : expr) (* next line *) |
| 59 |
: expr |
| 60 |
. |
| 61 |
|
| 62 |
Fixpoint interp (e : expr) (cc : CC.state) (ctx : name -> Z) : Z := |
| 63 |
match e with |
| 64 |
| Ret n => ctx n |
| 65 |
| Instr i rd args cont => |
| 66 |
let result := i.(spec) (Tuple.map ctx args) cc in |
| 67 |
let new_cc := CC.update i.(writes_conditions) result cc_spec cc in |
| 68 |
let new_ctx := (fun n => if name_eqb n rd then result mod wordmax else ctx n) in |
| 69 |
interp cont new_cc new_ctx |
| 70 |
end. |
| 71 |
End expr. |
| 72 |
End Language. |
| 73 |
|
| 74 |
Section Instructions. |
| 75 |
Import CC. |
| 76 |
|
| 77 |
Definition cc_spec (x : CC.code) (result : BinInt.Z) : bool := |
| 78 |
match x with |
| 79 |
| C => Z.testbit result 256 (* carry bit *) |
| 80 |
| M => Z.testbit result 255 (* most significant bit *) |
| 81 |
| L => Z.testbit result 0 (* least significant bit *) |
| 82 |
| Z => result =? 0 (* whether equal to zero *) |
| 83 |
end. |
| 84 |
|
| 85 |
Definition lower128 x := (Z.land x (Z.ones 128)). |
| 86 |
Definition upper128 x := (Z.shiftr x 128). |
| 87 |
Local Notation "x '[C]'" := (if x.(cc_c) then 1 else 0) (at level 20). |
| 88 |
Local Notation "x '[M]'" := (if x.(cc_m) then 1 else 0) (at level 20). |
| 89 |
Local Notation "x '[L]'" := (if x.(cc_l) then 1 else 0) (at level 20). |
| 90 |
Local Notation "x '[Z]'" := (if x.(cc_z) then 1 else 0) (at level 20). |
| 91 |
Local Notation "'int'" := (BinInt.Z). |
| 92 |
Local Notation "x << y" := ((Z.shiftl x y) mod (2^256)) : Z_scope. (* truncating left shift *) |
| 93 |
Local Notation "x >> y" := (Z.shiftr x y) : Z_scope. |
| 94 |
|
| 95 |
|
| 96 |
(* Note: In the specification document, argument order gets a bit |
| 97 |
confusing. Like here, r0 is always the first argument "source 0" |
| 98 |
and r1 the second. But the specification of MUL128LU is: |
| 99 |
(R[RS1][127:0] * R[RS0][255:128]) |
| 100 |
|
| 101 |
while the specification of SUB is: |
| 102 |
(R[RS0] - shift(R[RS1], imm)) |
| 103 |
|
| 104 |
In the SUB case, r0 is really treated the first argument, but in |
| 105 |
MUL128LU the order seems to be reversed; rather than low-high, we |
| 106 |
take the high part of the first argument r0 and the low parts of |
| 107 |
r1. This is also true for MUL128UL. *) |
| 108 |
|
| 109 |
Definition ADD (imm : int) : instruction := |
| 110 |
{| |
| 111 |
num_source_regs := 2; |
| 112 |
writes_conditions := [C; M; L; Z]; |
| 113 |
spec := (fun '(r0, r1) cc => |
| 114 |
r0 + (r1 << imm)) |
| 115 |
|}. |
| 116 |
|
| 117 |
Definition ADDC (imm : int) : instruction := |
| 118 |
{| |
| 119 |
num_source_regs := 2; |
| 120 |
writes_conditions := [C; M; L; Z]; |
| 121 |
spec := (fun '(r0, r1) cc => |
| 122 |
r0 + (r1 << imm) + cc[C]) |
| 123 |
|}. |
| 124 |
|
| 125 |
Definition SUB (imm : int) : instruction := |
| 126 |
{| |
| 127 |
num_source_regs := 2; |
| 128 |
writes_conditions := [C; M; L; Z]; |
| 129 |
spec := (fun '(r0, r1) cc => |
| 130 |
r0 - (r1 << imm)) |
| 131 |
|}. |
| 132 |
|
| 133 |
Definition SUBC (imm : int) : instruction := |
| 134 |
{| |
| 135 |
num_source_regs := 2; |
| 136 |
writes_conditions := [C; M; L; Z]; |
| 137 |
spec := (fun '(r0, r1) cc => |
| 138 |
r0 - (r1 << imm) - cc[C]) |
| 139 |
|}. |
| 140 |
|
| 141 |
|
| 142 |
Definition MUL128LL : instruction := |
| 143 |
{| |
| 144 |
num_source_regs := 2; |
| 145 |
writes_conditions := [M; L; Z]; |
| 146 |
spec := (fun '(r0, r1) cc => |
| 147 |
(lower128 r0) * (lower128 r1)) |
| 148 |
|}. |
| 149 |
|
| 150 |
Definition MUL128LU : instruction := |
| 151 |
{| |
| 152 |
num_source_regs := 2; |
| 153 |
writes_conditions := [M; L; Z]; |
| 154 |
spec := (fun '(r0, r1) cc => |
| 155 |
(lower128 r1) * (upper128 r0)) (* see note *) |
| 156 |
|}. |
| 157 |
|
| 158 |
Definition MUL128UL : instruction := |
| 159 |
{| |
| 160 |
num_source_regs := 2; |
| 161 |
writes_conditions := [M; L; Z]; |
| 162 |
spec := (fun '(r0, r1) cc => |
| 163 |
(upper128 r1) * (lower128 r0)) (* see note *) |
| 164 |
|}. |
| 165 |
|
| 166 |
Definition MUL128UU : instruction := |
| 167 |
{| |
| 168 |
num_source_regs := 2; |
| 169 |
writes_conditions := [M; L; Z]; |
| 170 |
spec := (fun '(r0, r1) cc => |
| 171 |
(upper128 r0) * (upper128 r1)) |
| 172 |
|}. |
| 173 |
|
| 174 |
(* Note : Unlike the other operations, the output of RSHI is |
| 175 |
truncated in the specification. This is not strictly necessary, |
| 176 |
since the interpretation function truncates the output |
| 177 |
anyway. However, it is useful to make the definition line up |
| 178 |
exactly with Z.rshi. *) |
| 179 |
Definition RSHI (imm : int) : instruction := |
| 180 |
{| |
| 181 |
num_source_regs := 2; |
| 182 |
writes_conditions := [M; L; Z]; |
| 183 |
spec := (fun '(r0, r1) cc => |
| 184 |
(((2^256 * r0) + r1) >> imm) mod (2^256)) |
| 185 |
|}. |
| 186 |
|
| 187 |
Definition SELC : instruction := |
| 188 |
{| |
| 189 |
num_source_regs := 2; |
| 190 |
writes_conditions := []; |
| 191 |
spec := (fun '(r0, r1) cc => |
| 192 |
if cc[C] =? 1 then r0 else r1) |
| 193 |
|}. |
| 194 |
|
| 195 |
Definition SELM : instruction := |
| 196 |
{| |
| 197 |
num_source_regs := 2; |
| 198 |
writes_conditions := []; |
| 199 |
spec := (fun '(r0, r1) cc => |
| 200 |
if cc[M] =? 1 then r0 else r1) |
| 201 |
|}. |
| 202 |
|
| 203 |
Definition SELL : instruction := |
| 204 |
{| |
| 205 |
num_source_regs := 2; |
| 206 |
writes_conditions := []; |
| 207 |
spec := (fun '(r0, r1) cc => |
| 208 |
if cc[L] =? 1 then r0 else r1) |
| 209 |
|}. |
| 210 |
|
| 211 |
(* TODO : treat the MOD register specially, like CC *) |
| 212 |
Definition ADDM : instruction := |
| 213 |
{| |
| 214 |
num_source_regs := 3; |
| 215 |
writes_conditions := [M; L; Z]; |
| 216 |
spec := (fun '(r0, r1, MOD) cc => |
| 217 |
let ra := r0 + r1 in |
| 218 |
if ra >=? MOD |
| 219 |
then ra - MOD |
| 220 |
else ra) |
| 221 |
|}. |
| 222 |
|
| 223 |
End Instructions. |
| 224 |
|
| 225 |
Module Registers. |
| 226 |
Inductive register : Type := |
| 227 |
| r0 : register |
| 228 |
| r1 : register |
| 229 |
| r2 : register |
| 230 |
| r3 : register |
| 231 |
| r4 : register |
| 232 |
| r5 : register |
| 233 |
| r6 : register |
| 234 |
| r7 : register |
| 235 |
| r8 : register |
| 236 |
| r9 : register |
| 237 |
| r10 : register |
| 238 |
| r11 : register |
| 239 |
| r12 : register |
| 240 |
| r13 : register |
| 241 |
| r14 : register |
| 242 |
| r15 : register |
| 243 |
| r16 : register |
| 244 |
| r17 : register |
| 245 |
| r18 : register |
| 246 |
| r19 : register |
| 247 |
| r20 : register |
| 248 |
| r21 : register |
| 249 |
| r22 : register |
| 250 |
| r23 : register |
| 251 |
| r24 : register |
| 252 |
| r25 : register |
| 253 |
| r26 : register |
| 254 |
| r27 : register |
| 255 |
| r28 : register |
| 256 |
| r29 : register |
| 257 |
| r30 : register |
| 258 |
| RegZero : register (* r31 *) |
| 259 |
| RegMod : register |
| 260 |
. |
| 261 |
|
| 262 |
Definition reg_dec (x y : register) : {x = y} + {x <> y}. |
| 263 |
Proof. destruct x, y; try (apply left; congruence); right; congruence. Defined. |
| 264 |
Definition reg_eqb x y := if reg_dec x y then true else false. |
| 265 |
|
| 266 |
Lemma reg_eqb_neq x y : x <> y -> reg_eqb x y = false. |
| 267 |
Proof. cbv [reg_eqb]; break_match; congruence. Qed. |
| 268 |
Lemma reg_eqb_refl x : reg_eqb x x = true. |
| 269 |
Proof. cbv [reg_eqb]; break_match; congruence. Qed. |
| 270 |
End Registers. |