Commit 13c0aa3d authored by Jean-Christophe Filliâtre's avatar Jean-Christophe Filliâtre
Browse files

register_allocation example (wip)

parent 2afdad2c
......@@ -243,3 +243,72 @@ module FiniteNumberOfRegisters
end) (expr_post e r)
end
module OptimalNumberOfRegisters
use import HighOrd
use import int.Int
use import int.MinMax
use import list.List
use import list.Append
use import Spec
use import DWP
(** we have k registers, namely 0,1,...,k-1 *)
constant k: int
(** we assume having at least two registers, otherwise we can't add *)
axiom at_least_two_registers: k >= 2
(** the minimal number of registers needed to evaluate e *)
function n (e: expr) : int =
match e with
| Evar _ -> 1
| Eneg e -> n e
| Eadd e1 e2 -> let n1 = n e1 in let n2 = n e2 in
if n1 = n2 then 1 + n1 else max n1 n2
end
(** Note: This is of course inefficient to recompute function [n] many
times. A realistic implementation would compute [n e] once for
each sub-expression [e], either with a first pass of tree decoration,
or with function [compile] returning the value of [n e] as well,
in a bottom-up way *)
function measure (e: expr) : int =
match e with
| Evar _ -> 0
| Eneg e -> 1 + measure e
| Eadd e1 e2 -> 1 + if n e1 >= n e2 then measure e1 + measure e2
else 1 + measure e1 + measure e2
end
lemma measure_nonneg: forall e. measure e >= 0
(** [compile e r] returns a list of instructions that stores the value
of [e] in register [r], without modifying any register [r' < r]. *)
let rec compile (e: expr) (r: register) : hcode
requires { 0 <= r < k }
variant { measure e }
ensures { hcode_ok result }
ensures { result.post --> expr_post e r }
= wrap (
match e with
| Evar x -> cons (Iload x r) (nil ())
| Eneg e -> $ compile e r -- cons (Ineg r) (nil ())
| Eadd e1 e2 ->
if n e1 >= n e2 then (* we must compile e1 first *)
if r < k-1 then
$ compile e1 r -- $ compile e2 (r + 1) --
cons (Iadd (r + 1) r) (nil ())
else
cons (Ipush (k - 2)) (
$ compile e1 (k - 2) -- $ compile e2 (k - 1) --
cons (Iadd (k - 2) (k - 1)) (
cons (Ipop (k - 2)) (nil ())))
else
$ compile (Eadd e2 e1) r (* compile e2 first *)
end) (expr_post e r)
end
......@@ -2,9 +2,11 @@
<!DOCTYPE why3session PUBLIC "-//Why3//proof session v5//EN"
"http://why3.lri.fr/why3session.dtd">
<why3session shape_version="4">
<prover id="0" name="Alt-Ergo" version="0.99.1" timelimit="6" steplimit="1" memlimit="1000"/>
<prover id="1" name="CVC4" version="1.4" timelimit="6" memlimit="1000"/>
<prover id="5" name="Alt-Ergo" version="1.00.prv" timelimit="5" steplimit="1" memlimit="1000"/>
<file name="../register_allocation.mlw" expanded="true">
<theory name="Spec" sum="84125847b61498f1784a3aedb308613e" expanded="true">
<file name="../register_allocation.mlw">
<theory name="Spec" sum="84125847b61498f1784a3aedb308613e">
<goal name="WP_parameter exec_append" expl="VC for exec_append">
<proof prover="5"><result status="valid" time="0.01" steps="9"/></proof>
</goal>
......@@ -418,5 +420,205 @@
</transf>
</goal>
</theory>
<theory name="OptimalNumberOfRegisters" sum="dbceed8db1f1f7971fe7303221d19fad">
<goal name="measure_nonneg">
<transf name="induction_ty_lex">
<goal name="measure_nonneg.1" expl="1.">
<proof prover="0" steplimit="-1"><result status="valid" time="0.02" steps="17"/></proof>
</goal>
</transf>
</goal>
<goal name="WP_parameter compile" expl="VC for compile">
<transf name="split_goal_wp">
<goal name="WP_parameter compile.1" expl="1. precondition">
<proof prover="0"><result status="valid" time="0.00" steps="6"/></proof>
</goal>
<goal name="WP_parameter compile.2" expl="2. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="8"/></proof>
</goal>
<goal name="WP_parameter compile.3" expl="3. precondition">
<proof prover="1"><result status="valid" time="3.46"/></proof>
</goal>
<goal name="WP_parameter compile.4" expl="4. postcondition">
<proof prover="0"><result status="valid" time="0.02" steps="10"/></proof>
</goal>
<goal name="WP_parameter compile.5" expl="5. postcondition">
<proof prover="0"><result status="valid" time="0.02" steps="10"/></proof>
</goal>
<goal name="WP_parameter compile.6" expl="6. precondition">
<proof prover="0"><result status="valid" time="0.03" steps="6"/></proof>
</goal>
<goal name="WP_parameter compile.7" expl="7. variant decrease">
<proof prover="0"><result status="valid" time="0.05" steps="12"/></proof>
</goal>
<goal name="WP_parameter compile.8" expl="8. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="8"/></proof>
</goal>
<goal name="WP_parameter compile.9" expl="9. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="10"/></proof>
</goal>
<goal name="WP_parameter compile.10" expl="10. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="12"/></proof>
</goal>
<goal name="WP_parameter compile.11" expl="11. precondition">
<proof prover="0"><result status="valid" time="0.03" steps="14"/></proof>
</goal>
<goal name="WP_parameter compile.12" expl="12. precondition">
<proof prover="1"><result status="valid" time="3.74"/></proof>
</goal>
<goal name="WP_parameter compile.13" expl="13. postcondition">
<proof prover="0"><result status="valid" time="0.02" steps="16"/></proof>
</goal>
<goal name="WP_parameter compile.14" expl="14. postcondition">
<proof prover="0"><result status="valid" time="0.02" steps="16"/></proof>
</goal>
<goal name="WP_parameter compile.15" expl="15. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="8"/></proof>
</goal>
<goal name="WP_parameter compile.16" expl="16. variant decrease">
<proof prover="0"><result status="valid" time="0.04" steps="16"/></proof>
</goal>
<goal name="WP_parameter compile.17" expl="17. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="10"/></proof>
</goal>
<goal name="WP_parameter compile.18" expl="18. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="14"/></proof>
</goal>
<goal name="WP_parameter compile.19" expl="19. variant decrease">
<proof prover="0"><result status="valid" time="0.02" steps="22"/></proof>
</goal>
<goal name="WP_parameter compile.20" expl="20. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="16"/></proof>
</goal>
<goal name="WP_parameter compile.21" expl="21. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="18"/></proof>
</goal>
<goal name="WP_parameter compile.22" expl="22. precondition">
<proof prover="0"><result status="valid" time="0.01" steps="20"/></proof>
</goal>
<goal name="WP_parameter compile.23" expl="23. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="22"/></proof>
</goal>
<goal name="WP_parameter compile.24" expl="24. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="24"/></proof>
</goal>
<goal name="WP_parameter compile.25" expl="25. precondition">
<transf name="prop_curry">
<goal name="WP_parameter compile.25.1" expl="1. precondition">
<transf name="compute_specified">
<goal name="WP_parameter compile.25.1.1" expl="1. precondition">
<transf name="simplify_trivial_quantification_in_goal">
<goal name="WP_parameter compile.25.1.1.1" expl="1. VC for compile">
<transf name="compute_specified">
<goal name="WP_parameter compile.25.1.1.1.1" expl="1. VC for compile">
<transf name="introduce_premises">
<goal name="WP_parameter compile.25.1.1.1.1.1" expl="1. VC for compile">
<proof prover="1"><result status="valid" time="0.08"/></proof>
</goal>
</transf>
</goal>
</transf>
</goal>
</transf>
</goal>
</transf>
</goal>
</transf>
</goal>
<goal name="WP_parameter compile.26" expl="26. postcondition">
<proof prover="0"><result status="valid" time="0.02" steps="26"/></proof>
</goal>
<goal name="WP_parameter compile.27" expl="27. postcondition">
<proof prover="0"><result status="valid" time="0.04" steps="26"/></proof>
</goal>
<goal name="WP_parameter compile.28" expl="28. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="8"/></proof>
</goal>
<goal name="WP_parameter compile.29" expl="29. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="10"/></proof>
</goal>
<goal name="WP_parameter compile.30" expl="30. variant decrease">
<proof prover="0"><result status="valid" time="0.03" steps="18"/></proof>
</goal>
<goal name="WP_parameter compile.31" expl="31. precondition">
<proof prover="0"><result status="valid" time="0.03" steps="12"/></proof>
</goal>
<goal name="WP_parameter compile.32" expl="32. precondition">
<proof prover="0"><result status="valid" time="0.03" steps="16"/></proof>
</goal>
<goal name="WP_parameter compile.33" expl="33. variant decrease">
<proof prover="0"><result status="valid" time="0.04" steps="24"/></proof>
</goal>
<goal name="WP_parameter compile.34" expl="34. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="18"/></proof>
</goal>
<goal name="WP_parameter compile.35" expl="35. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="22"/></proof>
</goal>
<goal name="WP_parameter compile.36" expl="36. precondition">
<proof prover="0"><result status="valid" time="0.03" steps="24"/></proof>
</goal>
<goal name="WP_parameter compile.37" expl="37. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="26"/></proof>
</goal>
<goal name="WP_parameter compile.38" expl="38. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="28"/></proof>
</goal>
<goal name="WP_parameter compile.39" expl="39. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="30"/></proof>
</goal>
<goal name="WP_parameter compile.40" expl="40. precondition">
<transf name="prop_curry">
<goal name="WP_parameter compile.40.1" expl="1. precondition">
<transf name="compute_specified">
<goal name="WP_parameter compile.40.1.1" expl="1. precondition">
<transf name="simplify_trivial_quantification_in_goal">
<goal name="WP_parameter compile.40.1.1.1" expl="1. VC for compile">
<transf name="compute_specified">
<goal name="WP_parameter compile.40.1.1.1.1" expl="1. VC for compile">
<transf name="introduce_premises">
<goal name="WP_parameter compile.40.1.1.1.1.1" expl="1. VC for compile">
<proof prover="0" steplimit="-1"><result status="valid" time="0.10" steps="99"/></proof>
</goal>
</transf>
</goal>
</transf>
</goal>
</transf>
</goal>
</transf>
</goal>
</transf>
</goal>
<goal name="WP_parameter compile.41" expl="41. postcondition">
<proof prover="0"><result status="valid" time="0.03" steps="32"/></proof>
</goal>
<goal name="WP_parameter compile.42" expl="42. postcondition">
<proof prover="0"><result status="valid" time="0.02" steps="32"/></proof>
</goal>
<goal name="WP_parameter compile.43" expl="43. variant decrease">
<proof prover="0"><result status="valid" time="0.02" steps="14"/></proof>
</goal>
<goal name="WP_parameter compile.44" expl="44. precondition">
<proof prover="0"><result status="valid" time="0.01" steps="5"/></proof>
</goal>
<goal name="WP_parameter compile.45" expl="45. precondition">
<proof prover="0"><result status="valid" time="0.01" steps="7"/></proof>
</goal>
<goal name="WP_parameter compile.46" expl="46. precondition">
<proof prover="0"><result status="valid" time="0.02" steps="9"/></proof>
</goal>
<goal name="WP_parameter compile.47" expl="47. precondition">
<proof prover="0"><result status="valid" time="0.03" steps="35"/></proof>
</goal>
<goal name="WP_parameter compile.48" expl="48. postcondition">
<proof prover="0"><result status="valid" time="0.00" steps="11"/></proof>
</goal>
<goal name="WP_parameter compile.49" expl="49. postcondition">
<proof prover="0"><result status="valid" time="0.02" steps="11"/></proof>
</goal>
</transf>
</goal>
</theory>
</file>
</why3session>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment