Require Export VST.floyd.io_events.
Require Export ITree.ITree.
Inductive format_item :=
| FI_int: format_item
| FI_string: format_item
| FI_text: list byte -> format_item
| FI_error: format_item.
Fixpoint interpret_format_string (s: list Z) :
list format_item :=
match s with
| 37 :: 100 :: s' =>   (* %d *)
FI_int :: interpret_format_string s'
| 37 :: 115 :: s' =>  (* %s *)
FI_string :: interpret_format_string s'
| 37 :: 37 :: s' =>    (* %% *)
match interpret_format_string s' with
| FI_text s1 :: fl => FI_text (Byte.repr 37 :: s1) :: fl
| fl => FI_text (Byte.repr 37 :: nil) :: fl
end
| 37 :: s' => FI_error :: interpret_format_string s'
| c :: s' => match interpret_format_string s' with
| FI_text s1 :: fl => FI_text (Byte.repr c :: s1) :: fl
| fl => FI_text (Byte.repr c :: nil) :: fl
end
| nil => nil
end.
Fixpoint format_stuff (fl: list format_item) : Type :=
match fl with
| FI_int :: fl' => int * format_stuff fl'
| FI_string :: fl' => (share * list byte * val) * format_stuff fl'
| FI_text _ :: fl' => format_stuff fl'
| FI_error :: fl' => format_stuff fl'
| nil => unit
end.
Fixpoint format_argtys (i: positive) (fl: list format_item) : list (ident * type) :=
match fl with
| FI_int :: fl' => (i, tint) :: format_argtys (Pos.succ i) fl'
| FI_string :: fl' => (i, tptr tschar) :: format_argtys (Pos.succ i) fl'
| FI_text _ :: fl' => format_argtys i fl'
| FI_error :: fl' => format_argtys i fl'
| nil => nil
end.
Definition readable_cstring {CS: compspecs} x :=
!! readable_share (fst (fst x)) && cstring (fst (fst x)) (snd (fst x)) (snd x).
Fixpoint SEP_of_format {CS: compspecs}
(fl: list format_item)
(stuff: format_stuff fl) {struct fl} : list mpred.
destruct fl as [ | fi fl'].
apply nil.
destruct fi; simpl in stuff.
- (* FI_int *)
apply (SEP_of_format CS fl' (snd stuff)).
- (* FI_string *)
apply (
(* cstring (fst (fst (fst stuff))) (snd (fst (fst stuff))) (snd (fst stuff)) :: SEP_of_format CS fl' (snd stuff)). *)
match stuff with ((sh,s,p),_) => cstring sh s p end
:: SEP_of_format CS fl' (match stuff with (_,r) => r end)).
- (* FI_text *)
apply (SEP_of_format CS fl' stuff).
- (* FI_error *)
apply (FF::nil).
Defined.
Fixpoint LOCAL_of_format
(fl: list format_item) (id: ident) (stuff: format_stuff fl) {struct fl} : list localdef.
destruct fl as [ | fi fl'].
apply nil.
destruct fi; simpl in stuff.
- (* FI_int *)
apply (temp id (Vint (fst stuff)) :: LOCAL_of_format fl' (Pos.succ id) (snd stuff)).
- (* FI_string *)
apply (temp id (snd (fst stuff)) :: LOCAL_of_format fl' (Pos.succ id) (snd stuff)).
- (* FI_text *)
apply (LOCAL_of_format fl' id stuff).
- (* FI_error *)
apply (LOCAL_of_format fl' id stuff).
Defined.
Fixpoint PROP_of_format (fl: list format_item) (stuff: format_stuff fl) {struct fl} : list Prop.
destruct fl as [ | fi fl'].
apply nil.
destruct fi; simpl in stuff.
- (* FI_int *)
apply (PROP_of_format fl' (snd stuff)).
- (* FI_string *)
apply (readable_share (fst (fst (fst stuff))) :: PROP_of_format fl' (snd stuff)).
- (* FI_text *)
apply (PROP_of_format fl' stuff).
- (* FI_error *)
apply (PROP_of_format fl' stuff).
Defined.

(* Compute the string representation of an int *)
Lemma div_10_dec : forall n, 0 < n ->
(Z.to_nat (n / 10) < Z.to_nat n)%nat.