Skip to content

Commit 2664d83

Browse files
authored
Merge branch 'master' into sb/yosys
2 parents 0c1b98b + cd3f141 commit 2664d83

File tree

11 files changed

+1589
-177
lines changed

11 files changed

+1589
-177
lines changed

heapster-saw/doc/tutorial/tutorial.md

+1,265
Large diffs are not rendered by default.

heapster-saw/examples/tutorial_c.bc

4.23 KB
Binary file not shown.

heapster-saw/examples/tutorial_c.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdint.h>
2+
3+
// Simple function that adds it's two inputs.
4+
uint64_t add (uint64_t x, uint64_t y) { return x + y; }
5+
6+
// A copy of `add`, that we will use to miss-type a function
7+
uint64_t add_mistyped (uint64_t x, uint64_t y) { return x + y; }
8+
9+
// Simple function that increments the value in a pointer
10+
void incr_ptr (uint64_t *x) { *x += 1; }
11+
12+
// Struct that represents the three coordinates for a 3D vector
13+
typedef struct { uint64_t x; uint64_t y; uint64_t z; } vector3d;
14+
15+
// function that computes the norm of a 3D vector
16+
// || (x,y,z) || = x^2+y^2+z^2
17+
uint64_t norm_vector (vector3d *v) { return (v->x * v->x + v->y * v->y + v->z * v->z); }

heapster-saw/examples/tutorial_c.saw

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
enable_experimental;
2+
env <- heapster_init_env "tutorial_c" "tutorial_c.bc";
3+
print "File loaded";
4+
5+
heapster_define_perm env "int64" " " "llvmptr 64" "exists x:bv 64.eq(llvmword(x))";
6+
heapster_typecheck_fun env "add" "().arg0:int64<>, arg1:int64<> -o arg0:true, arg1:true, ret:int64<>";
7+
print "Type checked add.";
8+
9+
heapster_typecheck_fun env "add_mistyped" "().arg0:true, arg1:int64<> -o arg0:true, arg1:true, ret:int64<>";
10+
print "Type checked add_mistyped. This will produce an error in the output.";
11+
12+
heapster_typecheck_fun env "incr_ptr" "(). arg0:ptr((W,0) |-> int64<>) -o arg0:ptr((W,0) |-> int64<>)";
13+
print "Type checked incr_ptr.";
14+
15+
heapster_define_perm env "vec3d" "rw:rwmodality" "llvmptr 64" "ptr((rw,0) |-> int64<>) * ptr((rw,8) |-> int64<>) * ptr((rw,16) |-> int64<>)";
16+
heapster_typecheck_fun env "norm_vector" "(). arg0:vec3d<R> -o arg0:vec3d<R>, ret:int64<>";
17+
print "Type checked norm_vector.";
18+
19+
heapster_export_coq env "tutorial_c_gen.v";
20+
print "Export to Coq.";
21+
22+
print "Done.";
23+

heapster-saw/examples/tutorial_c.v

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
(** Mandatory imports from saw-core-coq *)
3+
From Coq Require Import Lists.List.
4+
From Coq Require Import String.
5+
From Coq Require Import Vectors.Vector.
6+
From CryptolToCoq Require Import SAWCoreScaffolding.
7+
From CryptolToCoq Require Import SAWCoreVectorsAsCoqVectors.
8+
Import ListNotations.
9+
10+
(** Post-preamble section specified by you *)
11+
From CryptolToCoq Require Import SAWCorePrelude.
12+
From CryptolToCoq Require Import SAWCoreBitvectors.
13+
14+
(** Code generated by saw-core-coq *)
15+
16+
Module tutorial_c.
17+
18+
Definition add__tuple_fun : CompM.lrtTupleType (CompM.LRT_Cons (CompM.LRT_Fun (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) (fun (perm0 : SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) => let var__0 := SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool in
19+
CompM.LRT_Fun var__0 (fun (perm1 : var__0) => CompM.LRT_Ret (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool)))) CompM.LRT_Nil) :=
20+
@CompM.multiFixM (CompM.LRT_Cons (CompM.LRT_Fun (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) (fun (perm0 : SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) => let var__0 := SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool in
21+
CompM.LRT_Fun var__0 (fun (perm1 : var__0) => CompM.LRT_Ret (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool)))) CompM.LRT_Nil) (fun (add : CompM.lrtToType (CompM.LRT_Fun (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) (fun (perm0 : SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) => let var__0 := SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool in
22+
CompM.LRT_Fun var__0 (fun (perm1 : var__0) => CompM.LRT_Ret (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool))))) => pair (fun (p0 : SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) (p1 : SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) => let var__0 := SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool in
23+
@CompM.letRecM CompM.LRT_Nil var__0 tt (@returnM CompM _ var__0 (SAWCoreVectorsAsCoqVectors.bvAdd 64 p0 p1))) tt).
24+
25+
Definition add : CompM.lrtToType (CompM.LRT_Fun (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) (fun (perm0 : SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) => let var__0 := SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool in
26+
CompM.LRT_Fun var__0 (fun (perm1 : var__0) => CompM.LRT_Ret (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool)))) :=
27+
SAWCoreScaffolding.fst add__tuple_fun.
28+
29+
End tutorial_c.
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
From CryptolToCoq Require Import SAWCoreScaffolding.
2+
From CryptolToCoq Require Import SAWCorePrelude.
3+
From CryptolToCoq Require Import CompMExtra.
4+
5+
(* The following line enables pretty printing*)
6+
Import CompMExtraNotation. Open Scope fun_syntax.
7+
8+
Require Import Examples.tutorial_c_gen.
9+
Import tutorial_c.
10+
11+
12+
Lemma no_errors_add
13+
: refinesFun add (fun _ _ => noErrorsSpec).
14+
Proof.
15+
unfold add, add__tuple_fun.
16+
prove_refinement.
17+
Qed.
18+
19+
Lemma no_errors_add_mistyped
20+
: refinesFun add_mistyped (fun _ => noErrorsSpec).
21+
Proof.
22+
unfold add_mistyped, add_mistyped__tuple_fun, noErrorsSpec.
23+
prove_refinement.
24+
(* Fails to solve the goal. *)
25+
Abort.
26+
27+
28+
Lemma no_errors_incr_ptr
29+
: refinesFun incr_ptr (fun _ => noErrorsSpec).
30+
Proof.
31+
unfold incr_ptr, incr_ptr__tuple_fun, noErrorsSpec.
32+
prove_refinement.
33+
Qed.
34+
35+
Lemma no_errors_norm_vector
36+
: refinesFun norm_vector (fun _ _ _ => noErrorsSpec).
37+
Proof.
38+
unfold norm_vector, norm_vector__tuple_fun, noErrorsSpec.
39+
prove_refinement.
40+
Qed.

0 commit comments

Comments
 (0)