Skip to content

Commit 2c04184

Browse files
committed
Add support for shld
1 parent efc121c commit 2c04184

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/Assembly/Symbolic.v

+6
Original file line numberDiff line numberDiff line change
@@ -4351,6 +4351,12 @@ Definition SymexNormalInstruction {opts : symbolic_options_computed_opt} {descr:
43514351
v <- Symeval (or s@(shr s@(lo, cnt), shl s@(hi, cnt')));
43524352
_ <- SetOperand dst v;
43534353
HavocFlags
4354+
| shld, [hi as dst; lo; cnt] =>
4355+
let cnt := andZ@(cnt, (PreApp (const (Z.of_N s-1)%Z) nil)) in
4356+
let cnt' := addZ@(Z.of_N s, PreApp negZ [cnt]) in
4357+
v <- Symeval (or s@(shr s@(lo, cnt'), shl s@(hi, cnt)));
4358+
_ <- SetOperand dst v;
4359+
HavocFlags
43544360
| inc, [dst] =>
43554361
v <- Symeval (add s@(dst, PreARG 1%Z));
43564362
o <- Symeval (addoverflow s@(dst, PreARG 1%Z));

src/Assembly/WithBedrock/Semantics.v

+17
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,23 @@ Definition DenoteNormalInstruction (st : machine_state) (instr : NormalInstructi
329329
let st := if cnt =? 1 then SetFlag st OF signchange else st in
330330
let st := SetFlag st CF (Z.testbit l (cnt-1)) in
331331
Some (HavocFlag st AF)
332+
| shld, [dst as hi; lo; cnt] =>
333+
lv <- DenoteOperand sa s st lo;
334+
hv <- DenoteOperand sa s st hi;
335+
cnt <- DenoteOperand sa s st cnt;
336+
let l := Z.lor lv (Z.shiftl hv (Z.of_N s)) in
337+
let l_shifted := Z.shiftl l (Z.land cnt (Z.of_N s-1)) in
338+
let l_shifted_hi := Z.shiftr l_shifted (Z.of_N s) in
339+
let v := Z.land l_shifted_hi (Z.ones (Z.of_N s)) in
340+
st <- SetOperand sa s st dst v;
341+
if cnt =? 0 then Some st else
342+
if Z.of_N s <? cnt then Some (HavocFlags st) else
343+
let st := HavocFlagsFromResult s st l in
344+
let signchange := xorb (signed s hv <? 0)%Z (signed s v <? 0)%Z in
345+
(* Note: IA-32 SDM does not make it clear what sign change is in question *)
346+
let st := if cnt =? 1 then SetFlag st OF signchange else st in
347+
let st := SetFlag st CF (Z.testbit hv (Z.of_N s - cnt)) in
348+
Some (HavocFlag st AF)
332349
| (and | xor | or) as opc, [dst; src] =>
333350
let f := match opc with and => Z.land | xor => Z.lxor | _ => Z.lor end in
334351
v1 <- DenoteOperand sa s st dst;

src/Assembly/WithBedrock/SymbolicProofs.v

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ From Coq Require Import Lia.
33
Require Import Crypto.Util.ZUtil.Tactics.PullPush.
44
From Coq Require Import NArith.
55
From Coq Require Import ZArith.
6+
Require Import Crypto.Util.ZUtil.Testbit.
67
Require Import Crypto.AbstractInterpretation.ZRange.
78
Require Import Crypto.Util.ErrorT.
89
Import Coq.Lists.List. (* [map] is [List.map] not [ErrorT.map] *)
@@ -1345,6 +1346,18 @@ Proof using Type.
13451346
3: enough (0 <= Z.land v3 (Z.of_N n - 1)) by lia; eapply Z.land_nonneg; right.
13461347
1,2,3:pose_operation_size_cases; intuition (subst; cbn; clear; lia). }
13471348

1349+
Unshelve. all : match goal with H : context[Syntax.shld] |- _ => idtac | _ => shelve end; shelve_unifiable.
1350+
{ repeat match goal with H : ?x = Some _, H' : ?x = Some _ |- _ => rewrite H' in *; Option.inversion_option end.
1351+
progress subst.
1352+
replace (Z.land (Z.of_N n) (Z.ones (Z.of_N n))) with (Z.of_N n)
1353+
by (rewrite Z.land_ones, Z.mod_small; try split; try lia; apply Zpow_facts.Zpower2_lt_lin; lia).
1354+
assert (0 <= Z.of_N n - 1) by (pose_operation_size_cases; intuition (subst; cbn; clear; lia)).
1355+
rewrite <- !Z.shiftl_opp_r.
1356+
rewrite !Z.shiftl_lor.
1357+
rewrite <- !Z.land_lor_distr_l, <- Z.land_assoc, Z.land_diag.
1358+
rewrite !Z.shiftl_shiftl by (try apply Z.land_nonneg; lia).
1359+
f_equal; f_equal; f_equal; try lia. }
1360+
13481361
Unshelve. all : match goal with H : context[Syntax.shlx] |- _ => idtac | _ => shelve end; shelve_unifiable.
13491362
{ rewrite <- Z.land_assoc.
13501363
f_equal; f_equal; [].

0 commit comments

Comments
 (0)