Skip to content

Commit 852f5b3

Browse files
bgregoirvbgl
authored andcommitted
Introduce siXX and uiXX
remove wint annotation after wi2w, this is for printing
1 parent 7942a73 commit 852f5b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+4016
-999
lines changed

CHANGELOG.md

+26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@
33

44
## New features
55

6+
- Introduction of types siXX and uiXX (XX in [8,16,32,64,128, 256]).
7+
New notation for type uXX/sXX : wXX.
8+
siXX represents signed integer of size XX, i.e in the range [-2^XX/2, 2^XX/2).
9+
uiXX represents unsigned integer of size XX, i.e in the range [0, 2^XX).
10+
Introduction of a new cast operorators:
11+
(sint) e : from wXX/siXX to int, signed interpretation
12+
(uint) e : from wXX/uiXX to int, unsigned interpretation
13+
The previous cast operator (int) e stand for:
14+
(uint) e if e has type wXX/uiXX
15+
(sint) e if e has type siXX
16+
All basic operations on uiXX/siXX perform the corresponding int operation and check
17+
that the result is in the range of uiXX/siXX, if not it is a safety violation.
18+
They can be selected explicitly using notation "e1 +XX(ui/si) e2"
19+
Introduction of new cast operators:
20+
(XXw) e : from siXX/uiXX to wXX
21+
(XXsi) e :
22+
if e has type wXX it the identity
23+
if e has type int ensure that e is in the range of siXX, else it is a safety violation.
24+
(XXui) e :
25+
if e has type wXX it the identity
26+
if e has type int ensure that e is in the range of siXX, else it is a safety violation.
27+
Introduce zquot and zrem operators on int : "e1 /s e2" and "e1 %s e2".
28+
The key feature of this new type are for the extraction to easycrypt.
29+
They are extracted to int, removing the need to deal with modulus 2^XX operations.
30+
([PR #1071](https://github.com/jasmin-lang/jasmin/pull/1071)).
31+
632
- Add support for x86 `VMOVMSKPS` and `VMOVMSKPD` instructions, through the new
733
intrinsics `#MOVEMASK` which also maps to the `VPMOVMSKB` instruction;
834
therefore old intrinsic `#VPMOVMSKB` is deprecated

compiler/entry/commonCLI.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ let parse_and_compile (type reg regx xreg rflag cond asm_op extra_op)
7373
and type rflag = rflag
7474
and type cond = cond
7575
and type asm_op = asm_op
76-
and type extra_op = extra_op) pass file idirs =
76+
and type extra_op = extra_op) ~wi2i pass file idirs =
7777
let _env, pprog, _ast =
7878
try Compile.parse_file Arch.arch_info ~idirs file with
7979
| Annot.AnnotationError (loc, code) ->
@@ -90,6 +90,10 @@ let parse_and_compile (type reg regx xreg rflag cond asm_op extra_op)
9090
hierror ~loc:(Lmore loc) ~kind:"typing error" "%s" code
9191
in
9292

93+
let prog =
94+
if not wi2i then prog else Compile.do_wint_int (module Arch) prog
95+
in
96+
9397
let prog =
9498
if pass <= Compiler.ParamsExpansion then prog
9599
else

compiler/entry/commonCLI.mli

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ val parse_and_compile :
1717
and type regx = 'regx
1818
and type rflag = 'rflag
1919
and type xreg = 'xreg) ->
20+
wi2i:bool ->
21+
(* true => start by replacing wint operation by int operation *)
2022
Compiler.compiler_step ->
2123
string ->
2224
(string * string) list ->

compiler/entry/jasmin2ec.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ let parse_and_extract arch call_conv idirs =
3636
let module A = (val get_arch_module arch call_conv) in
3737

3838
let extract model amodel functions array_dir output pass file =
39-
let prog = parse_and_compile (module A) pass file idirs in
40-
39+
let prog = parse_and_compile (module A) ~wi2i:true pass file idirs in
4140
extract_to_file prog arch A.reg_size A.asmOp model amodel functions
4241
array_dir output
4342
in

compiler/entry/jasmin_ct.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open Utils
66
let parse_and_check arch call_conv idirs =
77
let module A = (val get_arch_module arch call_conv) in
88
let check ~doit infer ct_list speculative pass file =
9-
let prog = parse_and_compile (module A) pass file idirs in
9+
let prog = parse_and_compile (module A) ~wi2i:false pass file idirs in
1010

1111
if speculative then
1212
let prog =

compiler/safetylib/safetyAbsExpr.ml

+70-18
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,46 @@ let wsize_of_ty ty = match ty with
7474
(* Expression Linearization *)
7575
(****************************)
7676

77+
let remove_Owi2 o =
78+
match o with
79+
| E.Owi2(sg, sz, o) ->
80+
begin match o with
81+
| E.WIadd -> E.Oadd(Op_w sz)
82+
| E.WImul -> E.Omul(Op_w sz)
83+
| E.WIsub -> E.Osub(Op_w sz)
84+
| E.WImod -> E.Omod(sg, Op_w sz)
85+
| E.WIdiv -> E.Odiv(sg, Op_w sz)
86+
| E.WIshr -> if sg = Signed then E.Oasr (Op_w sz) else E.Olsr sz
87+
| E.WIshl -> E.Olsl(Op_w sz)
88+
| E.WIeq -> E.Oeq(Op_w sz)
89+
| E.WIneq -> E.Oneq(Op_w sz)
90+
| E.WIlt -> E.Olt(E.Cmp_w(sg, sz))
91+
| E.WIle -> E.Ole(E.Cmp_w(sg, sz))
92+
| E.WIgt -> E.Ogt(E.Cmp_w(sg, sz))
93+
| E.WIge -> E.Oge(E.Cmp_w(sg, sz))
94+
end
95+
| _ -> o
96+
97+
let rec remove_Owi1 sg o e =
98+
match o with
99+
| E.WIwint_of_int sz -> Papp1(E.Oword_of_int sz, e)
100+
| E.WIint_of_wint sz -> Papp1(E.Oint_of_word(sg, sz), e)
101+
| E.WIword_of_wint _ -> remove_Owi e
102+
| E.WIwint_of_word _ -> remove_Owi e
103+
| E.WIwint_ext(szo, szi) ->
104+
let o = if sg = Signed then E.Osignext(szo, szi) else E.Ozeroext(szo, szi) in
105+
Papp1(o, e)
106+
| E.WIneg sz -> Papp1(E.Oneg (Op_w sz), e)
107+
108+
and remove_Owi e =
109+
match e with
110+
| Papp1(E.Owi1(sg, o), e) -> remove_Owi1 sg o e
111+
| Papp2(o, e1, e2) -> Papp2(remove_Owi2 o, e1, e2)
112+
| _ -> e
113+
77114
let op1_to_abs_unop op1 = match op1 with
78115
| E.Oneg _ -> Some Texpr1.Neg
79-
| E.Oword_of_int _ | E.Oint_of_word _ | E.Ozeroext _ -> assert false
116+
| E.Oword_of_int _ | E.Oint_of_word _ | E.Ozeroext _ | E.Owi1 _ -> assert false
80117
| _ -> None
81118

82119
type shift_kind =
@@ -108,10 +145,10 @@ let op2_to_abs_binop op2 = match op2 with
108145
| E.Omul _ -> AB_Arith Texpr1.Mul
109146
| E.Osub _ -> AB_Arith Texpr1.Sub
110147

111-
| E.Omod (Cmp_w (Signed, _)) -> AB_Unknown
148+
| E.Omod (Signed, _) -> AB_Unknown
112149
| E.Omod _ -> AB_Arith Texpr1.Mod
113150

114-
| E.Odiv (Cmp_w (Signed, _)) -> AB_Unknown
151+
| E.Odiv (Signed, _) -> AB_Unknown
115152
| E.Odiv _ -> AB_Arith Texpr1.Div
116153

117154
| E.Olsr _ -> AB_Wop (Wshift Unsigned_right)
@@ -133,6 +170,7 @@ let op2_to_abs_binop op2 = match op2 with
133170
| E.Ovadd (_, _) | E.Ovsub (_, _) | E.Ovmul (_, _)
134171
| E.Ovlsr (_, _) | E.Ovlsl (_, _) | E.Ovasr (_, _) -> AB_Unknown
135172

173+
| E.Owi2 _ -> assert false
136174

137175
(* Return lin_expr mod 2^n *)
138176
let expr_pow_mod n lin_expr =
@@ -313,14 +351,16 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
313351

314352
| Pconst c -> Some (Z.of_string (Z.to_string c))
315353

354+
| Papp1 (E.Owi1(sg, o), e) -> aeval_cst_zint abs (remove_Owi1 sg o e)
355+
316356
| Papp1 (E.Oneg Op_int, e) ->
317357
Option.map Z.neg (aeval_cst_zint abs e)
318358

319359
| Papp1 (E.Oint_of_word _, e) ->
320360
aeval_cst_zint abs e
321361
(* No need to check for overflows because we do not allow word operations. *)
322362

323-
| Papp2 (Oadd Op_int, e1, e2) ->
363+
| Papp2 (E.Oadd Op_int, e1, e2) ->
324364
obind2 (fun x y -> Some (Z.add x y))
325365
(aeval_cst_zint abs e1) (aeval_cst_zint abs e2)
326366

@@ -332,7 +372,7 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
332372
obind2 (fun x y -> Some (Z.mul x y))
333373
(aeval_cst_zint abs e1) (aeval_cst_zint abs e2)
334374

335-
| Papp2 (Odiv Cmp_int, e1, e2) ->
375+
| Papp2 (Odiv (Unsigned, Op_int), e1, e2) ->
336376
obind2 (fun x y -> Some (Z.div x y))
337377
(aeval_cst_zint abs e1) (aeval_cst_zint abs e2)
338378

@@ -354,6 +394,8 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
354394
let rec aeval_cst_w abs e = match e with
355395
| Pvar x -> aeval_cst_var abs x
356396

397+
| Papp1 (E.Owi1(sg, o), e) -> aeval_cst_w abs (remove_Owi1 sg o e)
398+
357399
| Papp1 (E.Oword_of_int ws, e) ->
358400
let c_e = aeval_cst_zint abs e in
359401
let pws = Z.pow (Z.of_int 2) (int_of_ws ws) in
@@ -426,7 +468,8 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
426468

427469
| Pload _ -> raise Expr_contain_load
428470

429-
| Pif (_,_,e1,e2) | Papp2 (_, e1, e2) -> aux (aux acc e1) e2 in
471+
| Pif (_,_,e1,e2) (* FIXME: why the condition is not added ? *)
472+
| Papp2 (_, e1, e2) -> aux (aux acc e1) e2 in
430473

431474
try PtVars (aux [] e) with Expr_contain_load -> PtTopExpr
432475

@@ -451,9 +494,11 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
451494
check_is_int x;
452495
Mtexpr.var (mvar_of_var x)
453496

454-
| Papp1(E.Oint_of_word sz,e1) ->
497+
| Papp1(E.Owi1(sg, o), e1) -> linearize_iexpr abs (remove_Owi1 sg o e1)
498+
499+
| Papp1(E.Oint_of_word(s, sz),e1) ->
455500
let abs_expr1 = linearize_wexpr abs e1 in
456-
wrap_if_overflow abs abs_expr1 Unsigned (int_of_ws sz)
501+
wrap_if_overflow abs abs_expr1 s (int_of_ws sz)
457502

458503
| Papp1 (op1, e1) ->
459504
begin match op1_to_abs_unop op1 with
@@ -463,7 +508,7 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
463508
| None -> raise (Unop_not_supported op1) end
464509

465510
| Papp2 (op2, e1, e2) ->
466-
begin match op2_to_abs_binop op2 with
511+
begin match op2_to_abs_binop (remove_Owi2 op2) with
467512
| AB_Arith absop ->
468513
Mtexpr.(binop absop
469514
(linearize_iexpr abs e1)
@@ -486,6 +531,8 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
486531
let lin = Mtexpr.var (mvar_of_var x) in
487532
wrap_if_overflow abs lin Unsigned (int_of_ws ws_e)
488533

534+
| Papp1(E.Owi1(sg, o), e1) -> linearize_wexpr abs (remove_Owi1 sg o e1)
535+
489536
| Papp1(E.Oword_of_int sz,e1) ->
490537
assert (ty_expr e1 = tint);
491538
let abs_expr1 = linearize_iexpr abs e1 in
@@ -505,7 +552,7 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
505552
| None -> raise (Unop_not_supported op1) end
506553

507554
| Papp2 (op2, e1, e2) ->
508-
begin match op2_to_abs_binop op2 with
555+
begin match op2_to_abs_binop (remove_Owi2 op2) with
509556
| AB_Arith Texpr1.Mod
510557
| AB_Arith Texpr1.Mul as absop->
511558
let lin = Mtexpr.(binop (abget absop)
@@ -521,7 +568,7 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
521568

522569
(* If the expression overflows, we try to rewrite differently *)
523570
if linexpr_overflow abs lin Unsigned ws_out then
524-
let alt_lin = match e2 with
571+
let alt_lin = match remove_Owi e2 with
525572
| Papp1(E.Oword_of_int sz, Pconst z) ->
526573
let z = mpqf_of_z z in
527574
let mz = Mpqf.add (Mpqf.neg z) (mpq_pow (int_of_ws sz)) in
@@ -658,6 +705,7 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
658705
|> map_f (fun ex -> Papp1 (op1,ex))
659706

660707
| Papp2 (op2, e1, e2) ->
708+
let op2 = remove_Owi2 op2 in
661709
begin match remove_if_expr_aux e1 with
662710
| Some _ as e_opt -> map_f (fun ex -> Papp2 (op2, ex, e2)) e_opt
663711
| None -> remove_if_expr_aux e2
@@ -692,7 +740,7 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
692740
| E.Op_int -> E.Cmp_int
693741
| E.Op_w ws -> E.Cmp_w (Unsigned, ws) in
694742

695-
match op2 with
743+
match remove_Owi2 op2 with
696744
| E.Obeq | E.Oand | E.Oor | E.Oadd _ | E.Omul _ | E.Osub _
697745
| E.Odiv _ | E.Omod _ | E.Oland _ | E.Olor _
698746
| E.Oror _ | E.Orol _
@@ -707,11 +755,12 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
707755

708756
| Ovadd (_, _) | Ovsub (_, _) | Ovmul (_, _)
709757
| Ovlsr (_, _) | Ovlsl (_, _) | Ovasr (_, _) -> assert false
758+
| Owi2 _ -> assert false
710759

711760
let swap_op2 op e1 e2 =
712761
match op with
713-
| E.Ogt _ -> e2, e1
714-
| E.Oge _ -> e2, e1
762+
| E.Ogt _ | E.Owi2(_, _, E.WIgt) -> e2, e1
763+
| E.Oge _ | E.Owi2(_, _, E.WIge) -> e2, e1
715764
| _ -> e1, e2
716765

717766
let rec bexpr_to_btcons_aux : AbsDom.t -> Prog.expr -> btcons =
@@ -744,7 +793,7 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
744793
| None -> raise Bop_not_supported end
745794
| _ -> assert false end
746795

747-
| Papp2 (op2, e1, e2) -> begin match op2 with
796+
| Papp2 (op2, e1, e2) -> begin match remove_Owi2 op2 with
748797
| E.Oadd _ | E.Omul _ | E.Osub _
749798
| E.Odiv _ | E.Omod _ | E.Oland _ | E.Olor _
750799
| E.Oror _ | E.Orol _
@@ -761,9 +810,12 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
761810
| E.Oor -> BOr ( aux e1, aux e2 )
762811

763812
| E.Oeq _ | E.Oneq _ | E.Olt _ | E.Ole _ | E.Ogt _ | E.Oge _ ->
764-
match remove_if_expr_aux e with
813+
begin match remove_if_expr_aux e with
765814
| Some (ty,eb,el,er) -> aux (Pif (ty,eb,el,er))
766-
| None -> flat_bexpr_to_btcons abs op2 e1 e2 end
815+
| None -> flat_bexpr_to_btcons abs op2 e1 e2
816+
end
817+
| E.Owi2 _ -> assert false
818+
end
767819

768820
| PappN (Ocombine_flags c, [ eof; ecf; esf; ezf ]) ->
769821
begin match c with
@@ -1061,7 +1113,7 @@ module AbsExpr (AbsDom : AbsNumBoolType) = struct
10611113
apply_offset_expr abs outv info y o
10621114
else aeval_top_offset abs outv
10631115

1064-
| Some outv, Papp2 (op2,el,er) -> begin match op2,el with
1116+
| Some outv, Papp2 (op2,el,er) -> begin match remove_Owi2 op2, el with
10651117
| E.Oadd ( E.Op_w U64), Pvar y ->
10661118
if valid_offset_var abs ws_o y then
10671119
apply_offset_expr abs outv info y er

compiler/safetylib/safetyAbsExpr.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module AbsExpr (AbsDom : SafetyInterfaces.AbsNumBoolType) : sig
5656
Warray_.arr_access -> wsize -> int -> expr ->
5757
mvar list
5858

59-
val linearize_smpl_iexpr : AbsDom.t -> expr -> Mtexpr.t option
59+
val linearize_smpl_iexpr : AbsDom.t -> expr -> Mtexpr.t option
6060
val linearize_smpl_wexpr : AbsDom.t -> expr -> Mtexpr.t option
6161

6262
val bexpr_to_btcons : expr -> AbsDom.t -> btcons option

0 commit comments

Comments
 (0)