Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

links: add handling of constants #654

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions CoqOfRust/M.v
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,11 @@ Module Pointer.
Definition t : Set := list Index.t.
End Path.

Module Mutable.
Inductive t (Value A : Set) : Set :=
| Make {Address Big_A : Set}
(address : Address)
(path : Path.t)
(big_to_value : Big_A -> Value)
(projection : Big_A -> option A)
(injection : Big_A -> A -> option Big_A).
Arguments Make {_ _ _ _}.
End Mutable.

Module Core.
Inductive t (Value A : Set) : Set :=
Inductive t (Value : Set) : Set :=
| Immediate (value : Value)
| Mutable (mutable : Mutable.t Value A).
Arguments Immediate {_ _}.
| Mutable {Address : Set} (address : Address) (path : Path.t).
Arguments Immediate {_}.
Arguments Mutable {_ _}.
End Core.

Expand All @@ -159,14 +148,17 @@ Module Pointer.
end.
End Kind.

Inductive t (Value : Set) : Set :=
| Make {A : Set} (kind : Kind.t) (ty : Ty.t) (to_value : A -> Value) (core : Core.t Value A).
Arguments Make {_ _}.
Record t {Value : Set} : Set := {
kind : Kind.t;
core : Core.t Value;
}.
Arguments t : clear implicits.

Definition deref {Value : Set} (pointer : t Value) : t Value :=
match pointer with
| Make _ ty to_value core => Make Kind.Raw ty to_value core
end.
{|
kind := Kind.Raw;
core :=pointer.(core);
|}.
End Pointer.

Module Value.
Expand Down Expand Up @@ -607,7 +599,7 @@ Definition get_sub_pointer (pointer : Value.t) (index : Pointer.Index.t) : M :=
Definition are_equal (value1 value2 : Value.t) : M :=
call_primitive (Primitive.AreEqual value1 value2).

Parameter get_constant : string -> M.
Parameter get_constant : string -> Value.t.

Definition get_function (path : string) (generic_consts : list Value.t) (generic_tys : list Ty.t) :
M :=
Expand Down Expand Up @@ -778,8 +770,8 @@ Definition is_struct_tuple (value : Value.t) (constructor : string) : M :=

Definition borrow (kind : Pointer.Kind.t) (value : Value.t) : M :=
match value with
| Value.Pointer (Pointer.Make Pointer.Kind.Raw ty to_value core) =>
pure (Value.Pointer (Pointer.Make kind ty to_value core))
| Value.Pointer {| Pointer.kind := Pointer.Kind.Raw; Pointer.core := core |} =>
pure (Value.Pointer {| Pointer.kind := kind; Pointer.core := core |})
| _ => impossible "expected a raw pointer"
end.

Expand Down Expand Up @@ -808,3 +800,36 @@ Parameter struct_record_update : Value.t -> list (string * Value.t) -> Value.t.
Parameter unevaluated_const : Value.t -> Value.t.

Parameter yield : Value.t -> M.

Fixpoint run_constant (constant : M) : Value.t :=
match constant with
| LowM.Pure value =>
match value with
| inl value => value
| inr _ => Value.Error "expected a success value"
end
| LowM.CallPrimitive primitive k =>
let value :=
match primitive with
| Primitive.StateAlloc value =>
Value.Pointer {|
Pointer.kind := Pointer.Kind.Raw;
Pointer.core := Pointer.Core.Immediate value;
|}
| Primitive.StateRead pointer =>
match pointer with
| Value.Pointer {|
Pointer.kind := Pointer.Kind.Raw;
Pointer.core := Pointer.Core.Immediate value;
|} =>
value
| _ => Value.Error "expected an immediate raw pointer"
end
| _ => Value.Error "unhandled primitive"
end in
run_constant (k value)
| LowM.CallClosure _ _ _ => Value.Error "unexpected closure call"
| LowM.Let _ _ => Value.Error "unexpected let"
| LowM.Loop _ _ => Value.Error "unexpected loop"
| LowM.Impossible _ => Value.Error "impossible"
end.
16 changes: 12 additions & 4 deletions CoqOfRust/alloc/alloc.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Module alloc.

Parameter __rust_no_alloc_shim_is_unstable : Value.t.

Axiom Constant___rust_no_alloc_shim_is_unstable :
(M.get_constant "alloc::alloc::__rust_no_alloc_shim_is_unstable") =
__rust_no_alloc_shim_is_unstable.

(* StructTuple
{
name := "Global";
Expand Down Expand Up @@ -136,7 +140,7 @@ Module alloc.
Pointer.Kind.Ref,
M.deref (|
M.read (|
M.get_constant (| "alloc::alloc::__rust_no_alloc_shim_is_unstable" |)
M.get_constant "alloc::alloc::__rust_no_alloc_shim_is_unstable"
|)
|)
|)
Expand Down Expand Up @@ -277,7 +281,7 @@ Module alloc.
Pointer.Kind.Ref,
M.deref (|
M.read (|
M.get_constant (| "alloc::alloc::__rust_no_alloc_shim_is_unstable" |)
M.get_constant "alloc::alloc::__rust_no_alloc_shim_is_unstable"
|)
|)
|)
Expand Down Expand Up @@ -2270,9 +2274,8 @@ Module alloc.
M.read (|
M.deref (|
M.read (|
M.get_constant (|
M.get_constant
"alloc::alloc::__alloc_error_handler::__rdl_oom::__rust_alloc_error_handler_should_panic"
|)
|)
|)
|),
Expand Down Expand Up @@ -2412,6 +2415,11 @@ Module alloc.

Module __rdl_oom.
Parameter __rust_alloc_error_handler_should_panic : Value.t.

Axiom Constant___rust_alloc_error_handler_should_panic :
(M.get_constant
"alloc::alloc::__alloc_error_handler::__rdl_oom::__rust_alloc_error_handler_should_panic") =
__rust_alloc_error_handler_should_panic.
End __rdl_oom.
End __alloc_error_handler.
End alloc.
22 changes: 9 additions & 13 deletions CoqOfRust/alloc/boxed.v
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,7 @@ Module boxed.
fun γ =>
ltac:(M.monadic
(let γ :=
M.use
(M.get_constant (| "core::mem::SizedTypeProperties::IS_ZST" |)) in
M.use (M.get_constant "core::mem::SizedTypeProperties::IS_ZST") in
let _ :=
M.is_constant_or_break_match (| M.read (| γ |), Value.Bool true |) in
M.alloc (|
Expand Down Expand Up @@ -990,8 +989,7 @@ Module boxed.
fun γ =>
ltac:(M.monadic
(let γ :=
M.use
(M.get_constant (| "core::mem::SizedTypeProperties::IS_ZST" |)) in
M.use (M.get_constant "core::mem::SizedTypeProperties::IS_ZST") in
let _ :=
M.is_constant_or_break_match (| M.read (| γ |), Value.Bool true |) in
M.alloc (|
Expand Down Expand Up @@ -2050,7 +2048,7 @@ Module boxed.
(M.alloc (|
LogicalOp.or (|
M.read (|
M.get_constant (| "core::mem::SizedTypeProperties::IS_ZST" |)
M.get_constant "core::mem::SizedTypeProperties::IS_ZST"
|),
ltac:(M.monadic
(BinOp.eq (|
Expand Down Expand Up @@ -2356,7 +2354,7 @@ Module boxed.
(M.alloc (|
LogicalOp.or (|
M.read (|
M.get_constant (| "core::mem::SizedTypeProperties::IS_ZST" |)
M.get_constant "core::mem::SizedTypeProperties::IS_ZST"
|),
ltac:(M.monadic
(BinOp.eq (|
Expand Down Expand Up @@ -2759,7 +2757,7 @@ Module boxed.
(M.alloc (|
LogicalOp.or (|
M.read (|
M.get_constant (| "core::mem::SizedTypeProperties::IS_ZST" |)
M.get_constant "core::mem::SizedTypeProperties::IS_ZST"
|),
ltac:(M.monadic
(BinOp.eq (|
Expand Down Expand Up @@ -3058,7 +3056,7 @@ Module boxed.
(M.alloc (|
LogicalOp.or (|
M.read (|
M.get_constant (| "core::mem::SizedTypeProperties::IS_ZST" |)
M.get_constant "core::mem::SizedTypeProperties::IS_ZST"
|),
ltac:(M.monadic
(BinOp.eq (|
Expand Down Expand Up @@ -5616,9 +5614,7 @@ Module boxed.
|);
M.borrow (|
Pointer.Kind.Ref,
M.get_constant (|
"alloc::boxed::boxed_slice_as_array_unchecked::N"
|)
M.get_constant "alloc::boxed::boxed_slice_as_array_unchecked::N"
|)
]
|),
Expand Down Expand Up @@ -5810,7 +5806,7 @@ Module boxed.
|)
]
|),
M.read (| M.get_constant (| "alloc::boxed::N" |) |)
M.read (| M.get_constant "alloc::boxed::N" |)
|)
|)) in
let _ := M.is_constant_or_break_match (| M.read (| γ |), Value.Bool true |) in
Expand Down Expand Up @@ -5914,7 +5910,7 @@ Module boxed.
|),
[ M.borrow (| Pointer.Kind.Ref, vec |) ]
|),
M.read (| M.get_constant (| "alloc::boxed::N" |) |)
M.read (| M.get_constant "alloc::boxed::N" |)
|)
|)) in
let _ := M.is_constant_or_break_match (| M.read (| γ |), Value.Bool true |) in
Expand Down
10 changes: 3 additions & 7 deletions CoqOfRust/alloc/boxed/thin.v
Original file line number Diff line number Diff line change
Expand Up @@ -1140,16 +1140,14 @@ Module boxed.
|),
ltac:(M.monadic
(M.read (|
M.get_constant (|
M.get_constant
"core::mem::SizedTypeProperties::IS_ZST"
|)
|)))
|),
ltac:(M.monadic
(M.read (|
M.get_constant (|
M.get_constant
"core::mem::SizedTypeProperties::IS_ZST"
|)
|)))
|)
|)
Expand Down Expand Up @@ -1810,9 +1808,7 @@ Module boxed.
]
|) in
let~ alloc :=
M.copy (|
M.get_constant (| "alloc::boxed::thin::new_unsize_zst_discriminant" |)
|) in
M.copy (| M.get_constant "alloc::boxed::thin::new_unsize_zst_discriminant" |) in
let~ value_ptr :=
M.alloc (|
M.call_closure (|
Expand Down
6 changes: 2 additions & 4 deletions CoqOfRust/alloc/collections/btree/append.v
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,8 @@ Module collections.
]
|),
M.read (|
M.get_constant (|
M.get_constant
"alloc::collections::btree::node::CAPACITY"
|)
|)
|)
|)) in
Expand Down Expand Up @@ -518,9 +517,8 @@ Module collections.
]
|),
M.read (|
M.get_constant (|
M.get_constant
"alloc::collections::btree::node::CAPACITY"
|)
|)
|)
|)) in
Expand Down
Loading
Loading