2012-06-27 18:09:30 +02:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon *)
|
|
|
|
(* *)
|
|
|
|
(* Gwenael Delaval, LIG/INRIA, UJF *)
|
|
|
|
(* Leonard Gerard, Parkas, ENS *)
|
|
|
|
(* Adrien Guatto, Parkas, ENS *)
|
|
|
|
(* Cedric Pasteur, Parkas, ENS *)
|
2012-06-29 01:41:13 +02:00
|
|
|
(* Marc Pouzet, Parkas, ENS *)
|
2012-06-27 18:09:30 +02:00
|
|
|
(* *)
|
|
|
|
(* Copyright 2012 ENS, INRIA, UJF *)
|
|
|
|
(* *)
|
|
|
|
(* This file is part of the Heptagon compiler. *)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon is free software: you can redistribute it and/or modify it *)
|
|
|
|
(* under the terms of the GNU General Public License as published by *)
|
|
|
|
(* the Free Software Foundation, either version 3 of the License, or *)
|
|
|
|
(* (at your option) any later version. *)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon is distributed in the hope that it will be useful, *)
|
|
|
|
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
|
|
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
|
|
|
|
(* GNU General Public License for more details. *)
|
|
|
|
(* *)
|
|
|
|
(* You should have received a copy of the GNU General Public License *)
|
|
|
|
(* along with Heptagon. If not, see <http://www.gnu.org/licenses/> *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
2011-03-16 23:25:04 +01:00
|
|
|
|
|
|
|
(* Translation from the source language to Sigali polynomial systems *)
|
|
|
|
|
|
|
|
(* $Id: dynamic_system.ml 2652 2011-03-11 16:26:17Z delaval $ *)
|
|
|
|
|
|
|
|
open Compiler_utils
|
|
|
|
open Names
|
|
|
|
open Idents
|
|
|
|
open Types
|
|
|
|
open Clocks
|
|
|
|
open Sigali
|
2012-08-08 18:16:33 +02:00
|
|
|
open Location
|
2011-03-16 23:25:04 +01:00
|
|
|
|
|
|
|
type mtype = Tint | Tbool | Tother
|
|
|
|
|
2012-08-08 18:16:33 +02:00
|
|
|
exception Untranslatable
|
|
|
|
|
|
|
|
let untranslatable_warn e =
|
|
|
|
if e.Minils.e_loc <> no_location then
|
|
|
|
Format.eprintf "Warning: abstracted expression:@.%a"
|
|
|
|
Location.print_location e.Minils.e_loc
|
|
|
|
else
|
|
|
|
Format.eprintf "Warning: abstracted expression: @[<hov 2>%a@]@."
|
|
|
|
Mls_printer.print_exp e
|
|
|
|
|
|
|
|
let actual_ty ty =
|
|
|
|
match (Modules.unalias_type ty) with
|
2011-04-20 11:42:03 +02:00
|
|
|
| Tid({ qual = Pervasives; name = "bool"}) -> Tbool
|
|
|
|
| Tid({ qual = Pervasives; name = "int"}) -> Tint
|
2011-03-16 23:25:04 +01:00
|
|
|
| _ -> Tother
|
|
|
|
|
2012-08-08 18:16:33 +02:00
|
|
|
let current_inputs : IdentSet.t ref = ref IdentSet.empty
|
2012-11-22 18:37:32 +01:00
|
|
|
let current_locals : IdentSet.t ref = ref IdentSet.empty
|
2011-03-16 23:25:04 +01:00
|
|
|
|
|
|
|
let translate_static_exp se =
|
2013-04-11 16:52:17 +02:00
|
|
|
match (Static.simplify QualEnv.empty se).se_desc with
|
2011-03-16 23:25:04 +01:00
|
|
|
| Sint(v) -> Cint(v)
|
2012-08-08 18:16:33 +02:00
|
|
|
| Sfloat(_) -> raise Untranslatable
|
2012-06-06 15:59:08 +02:00
|
|
|
| Sbool(true)
|
|
|
|
| Sconstructor { qual = Pervasives; name = "true" }
|
|
|
|
-> Ctrue
|
|
|
|
| Sbool(false)
|
|
|
|
| Sconstructor { qual = Pervasives; name = "false" }
|
2013-03-12 16:52:27 +01:00
|
|
|
-> Cfalse
|
2011-08-04 13:37:33 +02:00
|
|
|
| Sop({ qual = Pervasives; name = "~-" },[{se_desc = Sint(v)}]) ->
|
|
|
|
Cint(-v)
|
2012-08-08 18:16:33 +02:00
|
|
|
| _ -> raise Untranslatable
|
2011-03-16 23:25:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
let rec translate_pat = function
|
2012-06-06 15:59:08 +02:00
|
|
|
| Minils.Evarpat(x) -> [x]
|
2011-03-16 23:25:04 +01:00
|
|
|
| Minils.Etuplepat(pat_list) ->
|
|
|
|
List.fold_right (fun pat acc -> (translate_pat pat) @ acc) pat_list []
|
|
|
|
|
|
|
|
let rec translate_ck pref e = function
|
|
|
|
| Cbase | Cvar { contents = Cindex _ } ->
|
|
|
|
e
|
|
|
|
| Cvar { contents = Clink ck } -> translate_ck pref e ck
|
|
|
|
| Con(ck,c,var) ->
|
|
|
|
let e = translate_ck pref e ck in
|
|
|
|
Swhen(e,
|
2011-11-21 02:03:45 +01:00
|
|
|
match (shortname c) with
|
|
|
|
"true" -> Svar(pref ^ (name var))
|
|
|
|
| "false" -> Snot(Svar(pref ^ (name var)))
|
|
|
|
| _ -> assert false)
|
2011-03-16 23:25:04 +01:00
|
|
|
|
2011-07-27 11:21:34 +02:00
|
|
|
|
2012-06-06 15:59:08 +02:00
|
|
|
let rec translate_ext prefix ({ Minils.w_desc = desc; Minils.w_ty = ty }) =
|
2011-03-16 23:25:04 +01:00
|
|
|
match desc with
|
2012-08-08 18:16:33 +02:00
|
|
|
| Minils.Wconst(v) ->
|
|
|
|
begin match (actual_ty ty) with
|
|
|
|
| Tbool -> Sconst(translate_static_exp v)
|
|
|
|
| Tint -> a_const (Sconst(translate_static_exp v))
|
|
|
|
| Tother -> raise Untranslatable
|
|
|
|
end
|
|
|
|
| Minils.Wvar(n) ->
|
|
|
|
(* get variable iff it is Boolean or local *)
|
|
|
|
begin match (actual_ty ty) with
|
2013-03-12 16:52:27 +01:00
|
|
|
| Tbool ->
|
2012-11-22 18:37:32 +01:00
|
|
|
Svar(prefix ^ (name n))
|
2013-03-12 16:52:27 +01:00
|
|
|
| Tint when (IdentSet.mem n !current_locals) ->
|
2012-11-22 18:37:32 +01:00
|
|
|
Svar(prefix ^ (name n))
|
2013-03-12 16:52:27 +01:00
|
|
|
| _ ->
|
2012-11-22 18:37:32 +01:00
|
|
|
raise Untranslatable
|
2012-08-08 18:16:33 +02:00
|
|
|
end
|
|
|
|
(* TODO remove if this works without *)
|
|
|
|
(* | Minils.Wwhen(e, c, var) when ((actual_ty e.Minils.w_ty) = Tbool) -> *)
|
|
|
|
(* let e = translate_ext prefix e in *)
|
|
|
|
(* Swhen(e, *)
|
|
|
|
(* match (shortname c) with *)
|
|
|
|
(* "true" -> Svar(prefix ^ (name var)) *)
|
|
|
|
(* | "false" -> Snot(Svar(prefix ^ (name var))) *)
|
|
|
|
(* | _ -> assert false) *)
|
|
|
|
| Minils.Wwhen(e, _c, _var) ->
|
|
|
|
translate_ext prefix e
|
|
|
|
| Minils.Wfield(_) ->
|
|
|
|
raise Untranslatable
|
|
|
|
| Minils.Wreinit _ -> raise Untranslatable
|
2011-07-27 11:21:34 +02:00
|
|
|
|
|
|
|
(* [translate e = c] *)
|
2012-08-08 18:16:33 +02:00
|
|
|
let rec translate prefix ({ Minils.e_desc = desc; Minils.e_ty = ty } as e) =
|
|
|
|
let ty = actual_ty ty in
|
2011-07-27 11:21:34 +02:00
|
|
|
match desc with
|
2012-08-08 18:16:33 +02:00
|
|
|
| Minils.Eextvalue(ext) -> translate_ext prefix ext
|
|
|
|
| Minils.Eapp (* pervasives binary or unary stateless operations *)
|
|
|
|
({ Minils.a_op = Minils.Efun({qual=Pervasives;name=n})},
|
|
|
|
e_list, _) ->
|
|
|
|
begin
|
|
|
|
match n, e_list with
|
|
|
|
| "not", [e] -> Snot(translate_ext prefix e)
|
|
|
|
| "or", [e1;e2] -> Sor((translate_ext prefix e1),
|
|
|
|
(translate_ext prefix e2))
|
|
|
|
| "&", [e1;e2] -> Sand((translate_ext prefix e1),
|
|
|
|
(translate_ext prefix e2))
|
|
|
|
| ("<="|"<"|">="|">"), [e1;e2] ->
|
|
|
|
let op,modv =
|
|
|
|
begin match n with
|
|
|
|
| "<=" -> a_inf,0
|
|
|
|
| "<" -> a_inf,-1
|
|
|
|
| ">=" -> a_sup,0
|
|
|
|
| _ -> a_sup,1
|
|
|
|
end in
|
|
|
|
let e1 = translate_ext prefix e1 in
|
|
|
|
let sig_e =
|
|
|
|
begin match e2.Minils.w_desc with
|
|
|
|
| Minils.Wconst({se_desc = Sint(v)}) ->
|
|
|
|
op e1 (Sconst(Cint(v+modv)))
|
|
|
|
| _ ->
|
|
|
|
let e2 = translate_ext prefix e2 in
|
|
|
|
op (Sminus(e1,e2)) (Sconst(Cint(modv)))
|
|
|
|
end in
|
|
|
|
(* a_inf and a_sup : +1 to translate ideals to boolean
|
|
|
|
polynomials *)
|
|
|
|
Splus(sig_e,Sconst(Ctrue))
|
|
|
|
| "+", [e1;e2] -> Splus((translate_ext prefix e1),
|
|
|
|
(translate_ext prefix e2))
|
|
|
|
| "-", [e1;e2] -> Splus((translate_ext prefix e1),
|
|
|
|
(Sprod((translate_ext prefix e2),(Sconst(Cint(-1))))))
|
|
|
|
| "*", [e1;e2] -> Sprod((translate_ext prefix e1),
|
|
|
|
(translate_ext prefix e2))
|
|
|
|
| "=", [e1;e2] when (ty = Tbool) ->
|
|
|
|
let e1 = translate_ext prefix e1 in
|
|
|
|
let e2 = translate_ext prefix e2 in
|
|
|
|
(* e1 = e2 iff (e1 and e2) or (not e1 and not e2) *)
|
|
|
|
(e1 &~ e2) |~ ((~~ e1) &~ (~~ e2))
|
|
|
|
| "<>", [e1;e2] when ty = Tbool ->
|
|
|
|
let e1 = translate_ext prefix e1 in
|
|
|
|
let e2 = translate_ext prefix e2 in
|
|
|
|
(* e1 <> e2 iff (e1 and not e2) or (not e1 and e2) *)
|
|
|
|
(e1 &~ (~~ e2)) |~ ((~~ e1) &~ e2)
|
|
|
|
| _ -> raise Untranslatable
|
|
|
|
end
|
|
|
|
(* | Minils.Ewhen(e, c, var) when ((actual_ty e.Minils.e_ty) = Tbool) -> *)
|
|
|
|
(* let e = translate prefix e in *)
|
|
|
|
(* Swhen(e, *)
|
|
|
|
(* match (shortname c) with *)
|
|
|
|
(* "true" -> Svar(prefix ^ (name var)) *)
|
|
|
|
(* | "false" -> Snot(Svar(prefix ^ (name var))) *)
|
|
|
|
(* | _ -> assert false) *)
|
|
|
|
| Minils.Ewhen(e, _c, _var) ->
|
|
|
|
translate prefix e
|
|
|
|
| Minils.Emerge(ck,[(c1,e1);(_c2,e2)]) ->
|
|
|
|
let e1 = translate_ext prefix e1 in
|
|
|
|
let e2 = translate_ext prefix e2 in
|
|
|
|
let e1,e2 =
|
2011-11-21 02:03:45 +01:00
|
|
|
begin
|
2012-08-08 18:16:33 +02:00
|
|
|
match (shortname c1) with
|
|
|
|
"true" -> e1,e2
|
|
|
|
| "false" -> e2,e1
|
2011-11-21 02:03:45 +01:00
|
|
|
| _ -> assert false
|
2012-08-08 18:16:33 +02:00
|
|
|
end in
|
|
|
|
let var_ck = Svar(prefix ^ (name ck)) in
|
|
|
|
begin match (actual_ty e.Minils.e_ty) with
|
|
|
|
| Tbool -> Sdefault(Swhen(e1,var_ck),e2)
|
|
|
|
| Tint -> a_part var_ck (a_const (Sconst(Cint(0)))) e1 e2
|
|
|
|
| Tother -> assert false
|
|
|
|
end
|
|
|
|
| Minils.Eapp({Minils.a_op = Minils.Eifthenelse},[e1;e2;e3],_) ->
|
|
|
|
let e1 = translate_ext prefix e1 in
|
|
|
|
let e2 = translate_ext prefix e2 in
|
|
|
|
let e3 = translate_ext prefix e3 in
|
|
|
|
begin match (actual_ty e.Minils.e_ty) with
|
|
|
|
| Tbool -> Sdefault(Swhen(e2,e1),e3)
|
|
|
|
| Tint -> a_part e1 (a_const (Sconst(Cint(0)))) e2 e3
|
|
|
|
| Tother -> assert false
|
|
|
|
end
|
|
|
|
| Minils.Estruct(_)
|
|
|
|
| Minils.Eiterator(_,_,_,_,_,_) ->
|
|
|
|
raise Untranslatable
|
|
|
|
| Minils.Eapp({Minils.a_op = Minils.Enode(_)},_,_) ->
|
|
|
|
failwith("Sigali: node in expressions; programs should be normalized")
|
|
|
|
| Minils.Efby(_,_) ->
|
|
|
|
failwith("Sigali: fby in expressions; programs should be normalized")
|
|
|
|
| Minils.Eapp(_,_,_) ->
|
|
|
|
raise Untranslatable
|
|
|
|
| Minils.Emerge(_, _) -> assert false
|
2011-03-16 23:25:04 +01:00
|
|
|
|
2013-03-12 16:52:27 +01:00
|
|
|
let translate_eq f
|
2012-06-06 15:59:08 +02:00
|
|
|
(acc_states,acc_init,acc_inputs,acc_eqs)
|
2012-01-25 09:34:58 +01:00
|
|
|
{ Minils.eq_lhs = pat; Minils.eq_rhs = e; eq_base_ck = ck } =
|
2011-10-17 15:28:04 +02:00
|
|
|
|
2011-03-16 23:25:04 +01:00
|
|
|
let prefix = f ^ "_" in
|
|
|
|
|
|
|
|
let prefixed n = prefix ^ n in
|
2011-10-17 15:28:04 +02:00
|
|
|
|
2012-01-25 09:34:58 +01:00
|
|
|
let { Minils.e_desc = desc } = e in
|
2011-03-16 23:25:04 +01:00
|
|
|
match pat, desc with
|
2012-08-08 18:16:33 +02:00
|
|
|
| Minils.Evarpat(n), Minils.Efby(opt_c, e') ->
|
|
|
|
begin match (actual_ty e.Minils.e_ty) with
|
|
|
|
| Tbool ->
|
|
|
|
begin try
|
|
|
|
let sn = prefixed (name n) in
|
|
|
|
let acc_eqs,acc_init =
|
|
|
|
match opt_c with
|
|
|
|
| None -> acc_eqs,Cfalse::acc_init
|
|
|
|
| Some(c) ->
|
|
|
|
let c = translate_static_exp c in
|
|
|
|
(extend
|
|
|
|
initialisations
|
|
|
|
(Slist[Sequal(Svar(sn),Sconst(c))]))::acc_eqs,
|
|
|
|
c::acc_init
|
|
|
|
in
|
|
|
|
let e_next = translate_ext prefix e' in
|
|
|
|
let e_next = translate_ck prefix e_next ck in
|
2012-11-22 18:37:32 +01:00
|
|
|
current_locals := IdentSet.add n !current_locals;
|
2012-08-08 18:16:33 +02:00
|
|
|
(n,sn)::acc_states,
|
|
|
|
acc_init,acc_inputs,
|
2011-11-21 02:03:45 +01:00
|
|
|
(extend
|
2012-08-08 18:16:33 +02:00
|
|
|
evolutions
|
|
|
|
(Slist[Sdefault(e_next,Svar(sn))]))
|
|
|
|
::acc_eqs
|
|
|
|
with Untranslatable ->
|
|
|
|
untranslatable_warn e;
|
|
|
|
(* e is abstracted ; n is Boolean and can be added as
|
|
|
|
uncontrollable input *)
|
|
|
|
current_inputs := IdentSet.add n !current_inputs;
|
|
|
|
acc_states,acc_init,
|
|
|
|
(acc_inputs @ [(n,(prefixed (name n)))]),
|
|
|
|
acc_eqs
|
|
|
|
end
|
|
|
|
| _ ->
|
|
|
|
untranslatable_warn e;
|
|
|
|
(* Mark n as input: unusable as local variable *)
|
2012-11-22 18:37:32 +01:00
|
|
|
Format.printf "Adding non-bool variable %s in current_inputs@\n" (name n);
|
2012-08-08 18:16:33 +02:00
|
|
|
current_inputs := IdentSet.add n !current_inputs;
|
|
|
|
acc_states,acc_init,acc_inputs,acc_eqs
|
|
|
|
end
|
2013-07-09 21:16:30 +02:00
|
|
|
| pat, Minils.Eapp({ Minils.a_op = (Minils.Enode f|Minils.Efun f); },
|
|
|
|
_e_list, None) when f.qual <> Pervasives ->
|
2011-03-16 23:25:04 +01:00
|
|
|
(*
|
2012-06-06 15:59:08 +02:00
|
|
|
(y1,...,yp) = f(x1,...,xn)
|
2011-11-21 02:03:45 +01:00
|
|
|
|
2012-06-06 15:59:08 +02:00
|
|
|
add inputs y1,...,yp. Link between yi and f's contract has
|
|
|
|
been done in the pass "Contracts".
|
2011-03-16 23:25:04 +01:00
|
|
|
*)
|
2012-06-06 15:59:08 +02:00
|
|
|
let ident_list = translate_pat pat in
|
2013-03-12 16:52:27 +01:00
|
|
|
let acc_inputs =
|
2012-06-06 15:59:08 +02:00
|
|
|
acc_inputs @
|
2012-08-08 18:16:33 +02:00
|
|
|
(List.map
|
|
|
|
(fun id ->
|
|
|
|
current_inputs := IdentSet.add id !current_inputs;
|
|
|
|
(id,(prefixed (name id))))
|
|
|
|
ident_list) in
|
2012-06-06 15:59:08 +02:00
|
|
|
acc_states,acc_init,
|
|
|
|
acc_inputs,acc_eqs
|
2013-08-04 22:38:03 +02:00
|
|
|
| Minils.Evarpat(n), _ ->
|
2012-08-08 18:16:33 +02:00
|
|
|
begin try
|
|
|
|
(* assert : no fby, no node application in e *)
|
|
|
|
let e' = translate prefix e in
|
|
|
|
(* let e' = *)
|
|
|
|
(* begin match (actual_ty e.Minils.e_ty) with *)
|
|
|
|
(* | Tbool -> translate_ck prefix e' ck *)
|
|
|
|
(* | _ -> e' *)
|
|
|
|
(* end in *)
|
2012-11-22 18:37:32 +01:00
|
|
|
current_locals := IdentSet.add n !current_locals;
|
2012-08-08 18:16:33 +02:00
|
|
|
acc_states,acc_init,
|
|
|
|
acc_inputs,
|
|
|
|
{ stmt_name = prefixed (name n);
|
|
|
|
stmt_def = e' }::acc_eqs
|
2013-03-12 16:52:27 +01:00
|
|
|
with Untranslatable ->
|
|
|
|
untranslatable_warn e;
|
|
|
|
current_inputs := IdentSet.add n !current_inputs;
|
2013-08-04 22:38:03 +02:00
|
|
|
let acc_inputs =
|
|
|
|
match actual_ty e.Minils.e_ty with
|
|
|
|
| Tbool -> acc_inputs @ [(n,(prefixed (name n)))]
|
|
|
|
| _ -> acc_inputs
|
|
|
|
in
|
|
|
|
acc_states,acc_init,acc_inputs,acc_eqs
|
2012-08-08 18:16:33 +02:00
|
|
|
end
|
2011-03-16 23:25:04 +01:00
|
|
|
| _ -> assert false
|
2011-10-17 15:28:04 +02:00
|
|
|
|
2012-06-06 15:59:08 +02:00
|
|
|
let translate_eq_list f eq_list =
|
2011-10-17 15:28:04 +02:00
|
|
|
List.fold_left
|
2012-06-06 15:59:08 +02:00
|
|
|
(fun (acc_states,acc_init, acc_inputs,acc_eqs) eq ->
|
|
|
|
translate_eq f (acc_states,acc_init,acc_inputs,acc_eqs) eq)
|
|
|
|
([],[],[],[])
|
2011-03-16 23:25:04 +01:00
|
|
|
eq_list
|
|
|
|
|
2012-06-06 15:59:08 +02:00
|
|
|
let translate_contract f contract =
|
2011-03-16 23:25:04 +01:00
|
|
|
let prefix = f ^ "_" in
|
|
|
|
let var_a = prefix ^ "assume" in
|
|
|
|
let var_g = prefix ^ "guarantee" in
|
|
|
|
match contract with
|
2011-10-17 15:28:04 +02:00
|
|
|
| None ->
|
|
|
|
let body =
|
2011-11-21 02:03:45 +01:00
|
|
|
[{ stmt_name = var_g; stmt_def = Sconst(Ctrue) };
|
|
|
|
{ stmt_name = var_a; stmt_def = Sconst(Ctrue) }] in
|
2012-06-06 15:59:08 +02:00
|
|
|
[],[],body,(Svar(var_a),Svar(var_g)),[],[],[]
|
|
|
|
| Some {Minils.c_local = locals;
|
2011-11-21 02:03:45 +01:00
|
|
|
Minils.c_eq = l_eqs;
|
|
|
|
Minils.c_assume = e_a;
|
|
|
|
Minils.c_enforce = e_g;
|
2012-06-06 15:59:08 +02:00
|
|
|
Minils.c_assume_loc = e_a_loc;
|
|
|
|
Minils.c_enforce_loc = e_g_loc;
|
2011-11-21 02:03:45 +01:00
|
|
|
Minils.c_controllables = cl} ->
|
2012-06-06 15:59:08 +02:00
|
|
|
let states,init,inputs,body = translate_eq_list f l_eqs in
|
2011-03-16 23:25:04 +01:00
|
|
|
assert (inputs = []);
|
2011-08-04 13:37:33 +02:00
|
|
|
let e_a = translate_ext prefix e_a in
|
|
|
|
let e_g = translate_ext prefix e_g in
|
2012-06-06 15:59:08 +02:00
|
|
|
let e_a_loc = translate_ext prefix e_a_loc in
|
|
|
|
let e_g_loc = translate_ext prefix e_g_loc in
|
2011-03-16 23:25:04 +01:00
|
|
|
let body =
|
2012-06-06 15:59:08 +02:00
|
|
|
{stmt_name = var_g; stmt_def = e_g &~ e_g_loc } ::
|
|
|
|
{stmt_name = var_a; stmt_def = e_a &~ e_a_loc } ::
|
2011-11-21 02:03:45 +01:00
|
|
|
body in
|
2011-10-17 15:28:04 +02:00
|
|
|
let controllables =
|
2012-06-06 15:59:08 +02:00
|
|
|
List.map
|
|
|
|
(fun ({ Minils.v_ident = id } as v) -> v,(prefix ^ (name id))) cl in
|
|
|
|
states,init,body,(Svar(var_a),Svar(var_g)),controllables,(locals@cl),l_eqs
|
2011-03-16 23:25:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-06 15:59:08 +02:00
|
|
|
let translate_node
|
2011-03-16 23:25:04 +01:00
|
|
|
({ Minils.n_name = {name = f};
|
|
|
|
Minils.n_input = i_list; Minils.n_output = o_list;
|
|
|
|
Minils.n_local = _d_list; Minils.n_equs = eq_list;
|
|
|
|
Minils.n_contract = contract } as node) =
|
2012-08-08 18:16:33 +02:00
|
|
|
current_inputs :=
|
|
|
|
List.fold_left
|
|
|
|
(fun set v_d -> IdentSet.add v_d.Minils.v_ident set)
|
|
|
|
IdentSet.empty
|
|
|
|
i_list;
|
2012-11-22 18:37:32 +01:00
|
|
|
current_locals := IdentSet.empty;
|
2012-08-08 18:16:33 +02:00
|
|
|
(* keep only Boolean inputs *)
|
|
|
|
let i_list =
|
|
|
|
List.filter
|
|
|
|
(fun { Minils.v_type = ty } -> (actual_ty ty) = Tbool) i_list in
|
2011-03-16 23:25:04 +01:00
|
|
|
(* every variable is prefixed by its node name *)
|
|
|
|
let inputs =
|
|
|
|
List.map
|
2012-06-06 15:59:08 +02:00
|
|
|
(fun { Minils.v_ident = v } -> v,(f ^ "_" ^ (name v))) i_list in
|
|
|
|
let sig_outputs =
|
2011-03-16 23:25:04 +01:00
|
|
|
List.map
|
|
|
|
(fun { Minils.v_ident = v } -> f ^ "_" ^ (name v)) o_list in
|
2012-06-06 15:59:08 +02:00
|
|
|
let states,init,add_inputs,body =
|
|
|
|
translate_eq_list f eq_list in
|
|
|
|
let states_c,init_c,body_c,(a_c,g_c),controllables,locals_c,eqs_c =
|
|
|
|
translate_contract f contract in
|
2011-03-16 23:25:04 +01:00
|
|
|
let inputs = inputs @ add_inputs in
|
|
|
|
let body = List.rev body in
|
|
|
|
let states = List.rev states in
|
|
|
|
let body_c = List.rev body_c in
|
|
|
|
let states_c = List.rev states_c in
|
2012-06-06 15:59:08 +02:00
|
|
|
let mls_inputs,sig_inputs = List.split inputs in
|
|
|
|
let mls_states,sig_states = List.split (states@states_c) in
|
|
|
|
let mls_ctrl,sig_ctrl = List.split controllables in
|
2011-10-17 15:28:04 +02:00
|
|
|
let constraints =
|
2011-03-16 23:25:04 +01:00
|
|
|
List.map
|
|
|
|
(fun v -> Sequal(Ssquare(Svar(v)),Sconst(Ctrue)))
|
2012-06-06 15:59:08 +02:00
|
|
|
(sig_inputs@sig_ctrl) in
|
|
|
|
let constraints = constraints @ [Sequal (a_c,Sconst(Ctrue))] in
|
2012-11-23 12:23:17 +01:00
|
|
|
let body_sink, sig_states_full, obj_exp =
|
|
|
|
if !Compiler_options.nosink then
|
|
|
|
([], sig_states, g_c)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
(* Sink state when the guarantee part of the contract becomes false *)
|
|
|
|
(* f_error_state state variable initialized to true; become false
|
|
|
|
the instant after the guarantee part is false *)
|
|
|
|
let error_state_name = f ^ "_error_state" in
|
|
|
|
let sig_states_full = sig_states @ [error_state_name] in
|
|
|
|
let body_sink =
|
|
|
|
[(extend
|
|
|
|
initialisations
|
|
|
|
(Slist[Sequal(Svar(error_state_name),Sconst(Ctrue))]));
|
|
|
|
(extend
|
|
|
|
evolutions
|
|
|
|
(Slist[g_c]))] in
|
|
|
|
(body_sink, sig_states_full, Svar(error_state_name))
|
|
|
|
end in
|
|
|
|
let obj = Security(obj_exp) in
|
2012-06-06 15:59:08 +02:00
|
|
|
let p = { proc_dep = [];
|
2011-11-21 02:03:45 +01:00
|
|
|
proc_name = f;
|
2012-06-06 15:59:08 +02:00
|
|
|
proc_inputs = sig_inputs@sig_ctrl;
|
|
|
|
proc_uncont_inputs = sig_inputs;
|
|
|
|
proc_outputs = sig_outputs;
|
|
|
|
proc_controllables = sig_ctrl;
|
2012-06-07 17:48:31 +02:00
|
|
|
proc_states = sig_states_full;
|
2011-11-21 02:03:45 +01:00
|
|
|
proc_init = init@init_c;
|
|
|
|
proc_constraints = constraints;
|
2012-06-07 17:48:31 +02:00
|
|
|
proc_body = body@body_c@body_sink;
|
2011-11-21 02:03:45 +01:00
|
|
|
proc_objectives = [obj] } in
|
2012-11-26 09:44:23 +01:00
|
|
|
if !Compiler_options.nbvars then
|
|
|
|
begin
|
|
|
|
(* Print out nb of vars *)
|
|
|
|
let nbs = List.length p.proc_states in
|
|
|
|
let nbi = List.length inputs in
|
|
|
|
let nbc = List.length controllables in
|
|
|
|
Printf.printf "%s %d %d %d %d\n" f nbs nbi nbc (nbs+nbi+nbc);
|
|
|
|
end;
|
|
|
|
|
2013-03-12 16:52:27 +01:00
|
|
|
let ctrlr_call =
|
2012-06-29 01:41:13 +02:00
|
|
|
begin
|
|
|
|
match controllables with
|
|
|
|
[] -> [] (* no controllable => no controller call *)
|
|
|
|
| _ :: _ ->
|
|
|
|
let ctrlr_pat = Minils.Etuplepat(List.map (fun { Minils.v_ident = v } ->
|
|
|
|
Minils.Evarpat(v))
|
|
|
|
mls_ctrl) in
|
|
|
|
let ctrlr_name = f ^ "_controller" in
|
|
|
|
let ctrlr_fun_name = { qual = Module (String.capitalize ctrlr_name);
|
|
|
|
name = ctrlr_name } in
|
2013-03-12 16:52:27 +01:00
|
|
|
let ctrlr_exp =
|
2012-06-29 01:41:13 +02:00
|
|
|
Minils.mk_exp
|
2013-03-12 16:52:27 +01:00
|
|
|
Cbase
|
2012-06-29 01:41:13 +02:00
|
|
|
(Tprod (List.map (fun _ -> Initial.tbool) mls_ctrl))
|
|
|
|
~linearity:Linearity.Ltop
|
|
|
|
(Minils.Eapp(Minils.mk_app (Minils.Efun ctrlr_fun_name),
|
2013-03-12 16:52:27 +01:00
|
|
|
(List.map
|
2012-06-29 01:41:13 +02:00
|
|
|
(fun v ->
|
2013-03-12 16:52:27 +01:00
|
|
|
Minils.mk_extvalue
|
|
|
|
~ty:Initial.tbool
|
2012-06-29 01:41:13 +02:00
|
|
|
~linearity:Linearity.Ltop
|
|
|
|
~clock:Cbase
|
|
|
|
(Minils.Wvar v))
|
|
|
|
(mls_inputs@mls_states))
|
|
|
|
@ (List.map
|
|
|
|
(fun _ ->
|
|
|
|
Minils.mk_extvalue
|
2013-03-12 16:52:27 +01:00
|
|
|
~ty:Initial.tbool
|
2012-06-29 01:41:13 +02:00
|
|
|
~linearity:Linearity.Ltop
|
|
|
|
~clock:Cbase
|
|
|
|
(Minils.Wconst(Initial.mk_static_bool true)))
|
|
|
|
mls_ctrl),
|
|
|
|
None))
|
|
|
|
in
|
|
|
|
let ctrlr_call =
|
|
|
|
Minils.mk_equation ~base_ck:Cbase false ctrlr_pat ctrlr_exp in
|
2012-06-06 15:59:08 +02:00
|
|
|
|
2012-06-29 01:41:13 +02:00
|
|
|
let ctrlr_inputs =
|
|
|
|
(List.map
|
2013-03-12 16:52:27 +01:00
|
|
|
(fun v ->
|
2012-06-29 01:41:13 +02:00
|
|
|
Signature.mk_arg (Some v) Initial.tbool Linearity.Ltop Signature.Cbase)
|
|
|
|
(sig_inputs@sig_states))
|
|
|
|
@ (List.map
|
|
|
|
(fun v ->
|
2013-03-12 16:52:27 +01:00
|
|
|
Signature.mk_arg
|
2012-06-29 01:41:13 +02:00
|
|
|
(Some ("p_" ^ v)) Initial.tbool Linearity.Ltop Signature.Cbase)
|
|
|
|
sig_ctrl) in
|
|
|
|
let ctrlr_outputs =
|
|
|
|
List.map
|
|
|
|
(fun v ->
|
|
|
|
Signature.mk_arg (Some v) Initial.tbool Linearity.Ltop Signature.Cbase)
|
|
|
|
sig_ctrl in
|
2013-03-12 16:52:27 +01:00
|
|
|
let ctrlr_signature =
|
|
|
|
Signature.mk_node Location.no_location ~extern:false
|
2012-06-29 01:41:13 +02:00
|
|
|
ctrlr_inputs ctrlr_outputs false false [] in
|
|
|
|
(* Add controller into modules *)
|
|
|
|
Modules.add_value ctrlr_fun_name ctrlr_signature;
|
|
|
|
[ctrlr_call]
|
|
|
|
end in
|
2013-03-12 16:52:27 +01:00
|
|
|
|
2012-06-06 15:59:08 +02:00
|
|
|
let node =
|
|
|
|
{ node with
|
|
|
|
Minils.n_contract = None;
|
|
|
|
Minils.n_local = node.Minils.n_local @ locals_c;
|
2012-06-29 01:41:13 +02:00
|
|
|
Minils.n_equs = ctrlr_call @ (node.Minils.n_equs @ eqs_c);
|
2012-06-06 15:59:08 +02:00
|
|
|
} in
|
|
|
|
node, p
|
2011-03-16 23:25:04 +01:00
|
|
|
|
|
|
|
let program p =
|
2012-06-06 15:59:08 +02:00
|
|
|
let acc_proc, acc_p_desc =
|
2011-03-16 23:25:04 +01:00
|
|
|
List.fold_left
|
2012-06-06 15:59:08 +02:00
|
|
|
(fun (acc_proc,acc_p_desc) p_desc ->
|
2011-11-21 02:03:45 +01:00
|
|
|
match p_desc with
|
|
|
|
| Minils.Pnode(node) ->
|
2012-06-06 15:59:08 +02:00
|
|
|
let (node,proc) = translate_node node in
|
|
|
|
(proc::acc_proc),((Minils.Pnode(node))::acc_p_desc)
|
|
|
|
| p -> (acc_proc,p::acc_p_desc))
|
|
|
|
([],[])
|
2011-07-27 11:21:34 +02:00
|
|
|
p.Minils.p_desc in
|
2011-03-16 23:25:04 +01:00
|
|
|
let procs = List.rev acc_proc in
|
2011-04-20 11:42:03 +02:00
|
|
|
let filename = filename_of_name (modul_to_string p.Minils.p_modname) in
|
2011-03-16 23:25:04 +01:00
|
|
|
let dirname = build_path (filename ^ "_z3z") in
|
|
|
|
let dir = clean_dir dirname in
|
2012-06-06 15:59:08 +02:00
|
|
|
Sigali.Printer.print dir procs;
|
|
|
|
{ p with Minils.p_desc = List.rev acc_p_desc }
|