skylabs.lang.cpp.model.simple_pointers_utils

(*
 * Copyright (c) 2020-21 BlueRock Security, Inc.
 * This software is distributed under the terms of the BedRock Open-Source License.
 * See the LICENSE-BedRock file in the repository root for details.
 *)


Support code for simple_pointers.v.

Require Import Stdlib.Sorting.SetoidList.
Require Import stdpp.gmap.
Require Import skylabs.prelude.base.
Require Import skylabs.prelude.addr.
Require Import skylabs.prelude.option.
Require Import skylabs.prelude.avl.
Require Import skylabs.lang.cpp.syntax.
Require Import skylabs.lang.cpp.semantics.types.
Require Import skylabs.lang.cpp.semantics.genv.
Require Import skylabs.lang.cpp.semantics.alloc_id.

Implicit Types (σ : genv).
#[local] Close Scope nat_scope.
#[local] Open Scope Z_scope.

Module canonical_tu.
  Definition im_to_gmap {V} (m : IM.t V) : gmap BS.t V :=
    list_to_map (map_to_list m).
Ordered name-map entries preserve structured names without requiring a countability instance for the mutually recursive syntax.
  Definition symbol_table_canon : Set := list (name * ObjValue).
  Definition type_table_canon : Set := list (name * GlobDecl).

  #[global] Instance symbol_table_canon_eq_dec : EqDecision symbol_table_canon.
  Proof. solve_decision. Qed.
  #[global] Instance type_table_canon_eq_dec : EqDecision type_table_canon.
  Proof. solve_decision. Qed.

  Record translation_unit_canon : Set := Build_translation_unit_canon
  { symbols : symbol_table_canon
  ; globals : type_table_canon
  ; abi : abi.t
  }.
  #[global] Instance translation_unit_canon_eq_dec : EqDecision translation_unit_canon.
  Proof. solve_decision. Qed.

  #[global] Instance symbol_canon_lookup : Lookup obj_name ObjValue translation_unit_canon :=
    fun k m => snd <$> find (fun kv => bool_decide (k = kv.1)) m.(symbols).

  Record genv_canon : Set := Build_genv_canon
  { genv_tu : translation_unit_canon
    (* ^ the translation_unit *)
  ; member_pointer_bitsize : bitsize
  }.
  #[global] Instance genv_canon_eq_dec : EqDecision genv_canon.
  Proof. solve_decision. Qed.

  Definition tu_to_canon (tu : translation_unit) : translation_unit_canon :=
    Build_translation_unit_canon
      (NM.elements tu.(translation_unit.symbols))
      (NM.elements tu.(translation_unit.types))
      tu.(translation_unit.abi).

  Lemma tu_to_canon_lookup (tu : translation_unit) (n : name) :
    tu_to_canon tu !! n = tu.(translation_unit.symbols) !! n.
  Proof.
    change (snd <$> find (fun kv => bool_decide (n = kv.1))
      (NM.elements tu.(translation_unit.symbols)) =
      tu.(translation_unit.symbols) !! n).
    destruct (find (fun kv => bool_decide (n = kv.1))
      (NM.elements tu.(translation_unit.symbols))) as [[k v]|] eqn:Hfind.
    - apply find_some in Hfind as [Hin Hname].
      apply bool_decide_eq_true_1 in Hname. simpl in Hname. subst k.
      simpl. symmetry. apply NM.find_1, NM.elements_2.
      apply InA_alt. exists (n, v). split; last exact Hin.
      split; first apply NM.Key.eq_refl. reflexivity.
    - simpl. destruct (tu.(translation_unit.symbols) !! n) as [v|] eqn:Hlookup; last done.
      exfalso.
      apply NM.find_2, NM.elements_1, InA_alt in Hlookup.
      destruct Hlookup as [[k w] [[Hname Hvalue] Hin]].
      apply NM.eqL in Hname. simpl in Hname, Hvalue. subst k w.
      have Hfalse := find_none _ _ Hfind (n, v) Hin.
      by rewrite /= bool_decide_true in Hfalse.
  Qed.

  #[local] Definition genv_to_canon σ : genv_canon :=
    let '(Build_genv tu sz) := σ in Build_genv_canon (tu_to_canon tu) sz.
End canonical_tu.

Definition null_alloc_id : alloc_id := MkAllocId 0.
Definition invalid_alloc_id : alloc_id := MkAllocId 1.

Compute the actual raw offsets in Z.
Section eval_offset_seg.
  Context σ.
  Definition o_field_off (f : field) : option Z :=
    match f with
    | Nscoped cls fld => offset_of σ cls fld
    | _ => None
    end.
  Definition o_sub_off ty z : option Z := Z.mul z <$> (Z.of_N <$> size_of σ ty).
  Definition o_base_off derived base : option Z :=
    parent_offset σ derived base.
  Definition o_derived_off base derived : option Z :=
    Z.opp <$> parent_offset σ derived base.
End eval_offset_seg.

(*
Utility function, used to emulate global_ptr without a linker.
This is a really bad model and it'd fail a bunch of sanity checks.
See comments around global_ptr in simple_pointers.v.
*)


Definition global_ptr_encode_vaddr (o : obj_name) : vaddr.
Proof. (*  exact (Npos (encode o)). *) Admitted. (* Countable *)
Definition global_ptr_encode_aid (o : obj_name) : alloc_id := MkAllocId (global_ptr_encode_vaddr o).

#[global] Instance global_ptr_encode_vaddr_inj : Inj (=) (=) global_ptr_encode_vaddr.
Proof. (* exact _. *) Admitted. (* countable obj_name *)
#[global] Instance global_ptr_encode_aid_inj : Inj (=) (=) global_ptr_encode_aid := _.

Lemma global_ptr_encode_vaddr_nonnull o va : va = global_ptr_encode_vaddr o -> va <> 0%N.
Proof. (* by move->. Qed. *) Admitted. (* ?? *)

Lemma global_ptr_encode_aid_nonnull o aid : aid = global_ptr_encode_aid o -> aid <> null_alloc_id.
Proof.
  intros -> H.
  apply (global_ptr_encode_vaddr_nonnull o _ eq_refl).
  by injection H.
Qed.

(*
A slightly better model might be something like the following, but we don't
bother defining this Countable instance. And this is not a great model
anyway. *)


(*
Declare Instance ObjValue_countable: Countable ObjValue.
Definition global_ptr (tu : translation_unit) (o : obj_name) : ptr :=
  let obj : option ObjValue := tu !! o in
  let p := Npos (encode obj) in (mkptr p p).
*)