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:43:15 +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/> *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
2010-09-13 13:32:35 +02:00
|
|
|
open Misc
|
2010-06-16 19:31:51 +02:00
|
|
|
open Names
|
2011-05-09 19:32:12 +02:00
|
|
|
open Signature
|
2010-07-23 19:45:19 +02:00
|
|
|
open Idents
|
2010-06-16 19:31:51 +02:00
|
|
|
open Types
|
2011-04-26 14:07:15 +02:00
|
|
|
open Linearity
|
2010-07-23 22:06:06 +02:00
|
|
|
open Clocks
|
2010-06-16 19:31:51 +02:00
|
|
|
open Static
|
|
|
|
open Format
|
2010-09-09 00:35:06 +02:00
|
|
|
open Global_printer
|
2010-06-16 19:31:51 +02:00
|
|
|
open Pp_tools
|
2010-07-13 14:03:39 +02:00
|
|
|
open Minils
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-06-21 18:19:58 +02:00
|
|
|
(** Every print_ function is boxed, that is it doesn't export break points,
|
2010-08-24 17:13:28 +02:00
|
|
|
Exceptions are [list] class functions *)
|
2010-06-19 18:28:52 +02:00
|
|
|
|
2010-08-24 17:13:28 +02:00
|
|
|
(** Every print_ function is without heading carry return or white space *)
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-06-26 16:53:25 +02:00
|
|
|
let iterator_to_string i =
|
|
|
|
match i with
|
2010-06-17 16:09:32 +02:00
|
|
|
| Imap -> "map"
|
2011-03-22 09:28:41 +01:00
|
|
|
| Imapi -> "mapi"
|
2010-06-17 16:09:32 +02:00
|
|
|
| Ifold -> "fold"
|
2010-07-26 09:33:22 +02:00
|
|
|
| Ifoldi -> "foldi"
|
2010-06-17 16:09:32 +02:00
|
|
|
| Imapfold -> "mapfold"
|
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
let rec print_pat ff = function
|
|
|
|
| Evarpat n -> print_ident ff n
|
|
|
|
| Etuplepat pat_list ->
|
2010-08-18 16:16:50 +02:00
|
|
|
fprintf ff "@[<2>(%a)@]" (print_list_r print_pat """,""") pat_list
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2011-12-12 11:30:18 +01:00
|
|
|
let print_vd ?(show_ck=false) ff { v_ident = n; v_type = ty; v_linearity = lin; v_clock = ck } =
|
|
|
|
if show_ck or !Compiler_options.full_type_info then
|
2011-04-26 14:07:15 +02:00
|
|
|
fprintf ff "%a : %a%a :: %a" print_ident n print_type ty print_linearity lin print_ck ck
|
2011-10-17 15:28:04 +02:00
|
|
|
else fprintf ff "%a : %a%a" print_ident n print_type ty print_linearity lin
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-19 18:28:52 +02:00
|
|
|
let print_local_vars ff = function
|
|
|
|
| [] -> ()
|
|
|
|
| l -> fprintf ff "@[<4>%a@]@\n" (print_list_r print_vd "var "";"";") l
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-07-14 02:30:48 +02:00
|
|
|
let print_const_dec ff c =
|
2010-09-15 09:38:52 +02:00
|
|
|
if !Compiler_options.full_type_info then
|
2010-07-14 02:30:48 +02:00
|
|
|
fprintf ff "const %a : %a = %a"
|
2010-09-09 00:35:06 +02:00
|
|
|
print_qualname c.c_name print_type c.c_type print_static_exp c.c_value
|
2010-07-14 02:30:48 +02:00
|
|
|
else
|
|
|
|
fprintf ff "const %a = %a"
|
2010-09-09 00:35:06 +02:00
|
|
|
print_qualname c.c_name print_static_exp c.c_value;
|
2010-08-17 12:21:21 +02:00
|
|
|
fprintf ff "@."
|
2010-07-14 02:30:48 +02:00
|
|
|
|
|
|
|
|
2010-06-19 18:28:52 +02:00
|
|
|
let rec print_params ff l =
|
2010-06-30 17:20:56 +02:00
|
|
|
fprintf ff "@[<2>%a@]" (print_list_r print_static_exp "<<"","">>") l
|
2010-06-17 16:09:32 +02:00
|
|
|
|
2010-06-19 18:28:52 +02:00
|
|
|
and print_node_params ff l =
|
|
|
|
fprintf ff "@[<2>%a@]" (print_list_r print_param "<<"","">>") l
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-21 01:45:23 +02:00
|
|
|
and print_exp_tuple ff l =
|
2010-08-18 16:16:50 +02:00
|
|
|
fprintf ff "@[<2>(%a)@]" (print_list_r print_exp """,""") l
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2011-04-14 18:06:54 +02:00
|
|
|
and print_w_tuple ff l =
|
|
|
|
fprintf ff "@[<2>(%a)@]" (print_list_r print_extvalue """,""") l
|
|
|
|
|
2010-06-21 01:45:23 +02:00
|
|
|
and print_vd_tuple ff l =
|
2011-12-12 11:30:18 +01:00
|
|
|
fprintf ff "@[<2>%a@]" (print_list_r (print_vd ~show_ck:false) "("";"")") l
|
|
|
|
|
|
|
|
and print_full_vd_tuple ff l =
|
|
|
|
fprintf ff "@[<2>%a@]" (print_list_r (print_vd ~show_ck:true) "("";"")") l
|
2010-06-17 16:09:32 +02:00
|
|
|
|
2010-06-19 18:28:52 +02:00
|
|
|
and print_index ff idx =
|
2010-06-30 17:20:56 +02:00
|
|
|
fprintf ff "@[<2>%a@]" (print_list print_static_exp "[""][""]") idx
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-06-19 18:28:52 +02:00
|
|
|
and print_dyn_index ff idx =
|
2011-04-14 18:06:54 +02:00
|
|
|
fprintf ff "@[<2>%a@]" (print_list print_extvalue "[""][""]") idx
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2011-03-22 22:12:59 +01:00
|
|
|
and print_trunc_index ff idx =
|
2011-04-14 18:06:54 +02:00
|
|
|
fprintf ff "@[<2>%a@]" (print_list print_extvalue "[>""<][>""<]") idx
|
2011-03-22 22:12:59 +01:00
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
and print_exp ff e =
|
2010-09-15 09:38:52 +02:00
|
|
|
if !Compiler_options.full_type_info then
|
2011-04-26 14:07:15 +02:00
|
|
|
fprintf ff "(%a : %a%a :: %a)"
|
2011-07-21 08:50:45 +02:00
|
|
|
print_exp_desc e.e_desc print_type e.e_ty print_linearity e.e_linearity print_ct e.e_ct
|
2010-06-17 16:09:32 +02:00
|
|
|
else fprintf ff "%a" print_exp_desc e.e_desc
|
2010-06-19 18:28:52 +02:00
|
|
|
|
2010-06-17 16:09:32 +02:00
|
|
|
and print_every ff reset =
|
2010-06-26 16:53:25 +02:00
|
|
|
print_opt (fun ff id -> fprintf ff " every %a" print_ident id) ff reset
|
2010-06-17 16:09:32 +02:00
|
|
|
|
2011-04-14 18:06:54 +02:00
|
|
|
and print_extvalue ff w =
|
|
|
|
if !Compiler_options.full_type_info then
|
2011-04-26 14:07:15 +02:00
|
|
|
fprintf ff "(%a : %a%a :: %a)"
|
|
|
|
print_extvalue_desc w.w_desc print_type w.w_ty print_linearity w.w_linearity print_ck w.w_ck
|
2011-04-14 18:06:54 +02:00
|
|
|
else fprintf ff "%a" print_extvalue_desc w.w_desc
|
|
|
|
|
|
|
|
|
|
|
|
and print_extvalue_desc ff = function
|
|
|
|
| Wconst c -> print_static_exp ff c
|
|
|
|
| Wvar x -> print_ident ff x
|
|
|
|
| Wfield (w,f) -> fprintf ff "%a.%a" print_extvalue w print_qualname f
|
|
|
|
| Wwhen (w, c, n) ->
|
|
|
|
fprintf ff "@[<2>(%a@ when %a(%a))@]" print_extvalue w print_qualname c print_ident n
|
2011-10-17 15:28:04 +02:00
|
|
|
| Wreinit (w1, w2) ->
|
|
|
|
fprintf ff "@[reinit@,(%a, %a)@]" print_extvalue w1 print_extvalue w2
|
2011-04-14 18:06:54 +02:00
|
|
|
|
2010-06-17 16:09:32 +02:00
|
|
|
and print_exp_desc ff = function
|
2011-04-14 18:06:54 +02:00
|
|
|
| Eextvalue w -> print_extvalue ff w
|
|
|
|
| Efby ((Some c), w) -> fprintf ff "@[<2>%a fby@ %a@]" print_static_exp c print_extvalue w
|
|
|
|
| Efby (None, w) -> fprintf ff "pre %a" print_extvalue w
|
2010-07-13 14:03:39 +02:00
|
|
|
| Eapp (app, args, reset) ->
|
2011-04-14 18:06:54 +02:00
|
|
|
fprintf ff "@[<2>%a@,%a@]" print_app (app, args) print_every reset
|
|
|
|
| Emerge (x, tag_w_list) ->
|
|
|
|
fprintf ff "@[<2>merge %a@ %a@]" print_ident x print_tag_w_list tag_w_list
|
2011-05-18 09:59:21 +02:00
|
|
|
| Ewhen (e,c,x) ->
|
|
|
|
fprintf ff "@[<2>(%a@ when %a(%a))@]" print_exp e print_qualname c print_ident x
|
2011-04-14 18:06:54 +02:00
|
|
|
| Estruct f_w_list ->
|
|
|
|
print_record (print_couple print_qualname print_extvalue """ = """) ff f_w_list
|
2011-06-27 10:58:14 +02:00
|
|
|
| Eiterator (it, f, params, pargs, args, reset) ->
|
2011-03-21 17:22:03 +01:00
|
|
|
fprintf ff "@[<2>(%s (%a)<<%a>>)@,(%a)%a@]%a"
|
2010-06-26 16:53:25 +02:00
|
|
|
(iterator_to_string it)
|
2010-07-13 14:03:39 +02:00
|
|
|
print_app (f, [])
|
2011-06-27 10:58:14 +02:00
|
|
|
(print_list_r print_static_exp """, """) params
|
2011-04-14 18:06:54 +02:00
|
|
|
print_w_tuple pargs
|
|
|
|
print_w_tuple args
|
2010-07-14 02:30:48 +02:00
|
|
|
print_every reset
|
|
|
|
|
2011-01-05 15:51:55 +01:00
|
|
|
and print_app ff (app, args) =
|
|
|
|
match app.a_op with
|
|
|
|
| Eequal ->
|
|
|
|
let e1, e2 = assert_2 args in
|
2011-04-14 18:06:54 +02:00
|
|
|
fprintf ff "@[<2>%a@ = %a@]" print_extvalue e1 print_extvalue e2
|
2011-01-05 15:51:55 +01:00
|
|
|
| Efun f | Enode f ->
|
|
|
|
fprintf ff "@[%a@,%a@,%a@]"
|
2011-04-14 18:06:54 +02:00
|
|
|
print_qualname f print_params app.a_params print_w_tuple args
|
2011-01-05 15:51:55 +01:00
|
|
|
| Eifthenelse ->
|
|
|
|
let e1, e2, e3 = assert_3 args in
|
|
|
|
fprintf ff "@[<hv>if %a@ then %a@ else %a@]"
|
2011-04-14 18:06:54 +02:00
|
|
|
print_extvalue e1 print_extvalue e2 print_extvalue e3
|
2011-01-05 15:51:55 +01:00
|
|
|
| Efield_update ->
|
|
|
|
let r,e = assert_2 args in
|
|
|
|
let f = assert_1 app.a_params in
|
|
|
|
fprintf ff "@[<2>{%a with .%a =@ %a}@]"
|
2011-04-14 18:06:54 +02:00
|
|
|
print_extvalue r print_static_exp f print_extvalue e
|
|
|
|
| Earray -> fprintf ff "@[<2>%a@]" (print_list_r print_extvalue "["";""]") args
|
2011-01-05 15:51:55 +01:00
|
|
|
| Earray_fill ->
|
|
|
|
let e = assert_1 args in
|
2011-05-26 18:39:33 +02:00
|
|
|
let n_list = app.a_params in
|
|
|
|
fprintf ff "%a@[<2>%a@]" print_extvalue e (print_list print_static_exp "^""^""") n_list
|
2011-01-05 15:51:55 +01:00
|
|
|
| Eselect ->
|
|
|
|
let e = assert_1 args in
|
2011-04-14 18:06:54 +02:00
|
|
|
fprintf ff "%a%a" print_extvalue e print_index app.a_params
|
2011-01-05 15:51:55 +01:00
|
|
|
| Eselect_slice ->
|
|
|
|
let e = assert_1 args in
|
|
|
|
let idx1, idx2 = assert_2 app.a_params in
|
|
|
|
fprintf ff "%a[%a..%a]"
|
2011-04-14 18:06:54 +02:00
|
|
|
print_extvalue e print_static_exp idx1 print_static_exp idx2
|
2011-01-05 15:51:55 +01:00
|
|
|
| Eselect_dyn ->
|
|
|
|
let r, d, e = assert_2min args in
|
|
|
|
fprintf ff "%a%a default %a"
|
2011-04-14 18:06:54 +02:00
|
|
|
print_extvalue r print_dyn_index e print_extvalue d
|
2011-03-22 22:12:59 +01:00
|
|
|
| Eselect_trunc ->
|
|
|
|
let e, idx_list = assert_1min args in
|
2011-04-14 18:06:54 +02:00
|
|
|
fprintf ff "%a%a" print_extvalue e print_trunc_index idx_list
|
2011-01-05 15:51:55 +01:00
|
|
|
| Eupdate ->
|
|
|
|
let e1, e2, idx = assert_2min args in
|
|
|
|
fprintf ff "@[<2>(%a with %a =@ %a)@]"
|
2011-04-14 18:06:54 +02:00
|
|
|
print_extvalue e1 print_dyn_index idx print_extvalue e2
|
2011-01-05 15:51:55 +01:00
|
|
|
| Econcat ->
|
|
|
|
let e1, e2 = assert_2 args in
|
2011-04-14 18:06:54 +02:00
|
|
|
fprintf ff "@[<2>%a@ @@ %a@]" print_extvalue e1 print_extvalue e2
|
2010-07-13 14:03:39 +02:00
|
|
|
|
2010-06-29 19:04:40 +02:00
|
|
|
and print_handler ff c =
|
2011-04-14 18:06:54 +02:00
|
|
|
fprintf ff "@[<2>%a@]" (print_couple print_qualname print_extvalue "("" -> "")") c
|
2010-06-29 19:04:40 +02:00
|
|
|
|
2011-04-14 18:06:54 +02:00
|
|
|
and print_tag_w_list ff tag_w_list =
|
|
|
|
fprintf ff "@[%a@]" (print_list print_handler """""") tag_w_list
|
2010-06-17 16:09:32 +02:00
|
|
|
|
|
|
|
|
2012-01-25 09:34:58 +01:00
|
|
|
and print_eq ff { eq_lhs = p; eq_rhs = e; eq_base_ck = base_ck } =
|
2010-09-15 09:38:52 +02:00
|
|
|
if !Compiler_options.full_type_info
|
2010-06-19 18:28:52 +02:00
|
|
|
then fprintf ff "@[<2>%a :: %a =@ %a@]"
|
2012-01-25 09:34:58 +01:00
|
|
|
print_pat p print_ck base_ck print_exp e
|
2010-06-21 01:45:23 +02:00
|
|
|
else fprintf ff "@[<2>%a =@ %a@]" print_pat p print_exp e
|
2010-06-17 16:09:32 +02:00
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-07-21 16:00:06 +02:00
|
|
|
and print_eqs ff = function
|
2010-06-19 18:28:52 +02:00
|
|
|
| [] -> ()
|
|
|
|
| l -> fprintf ff "@[<v2>let@ %a@]@\ntel" (print_list_r print_eq """;""") l
|
2010-06-17 16:09:32 +02:00
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
let print_open_module ff name = fprintf ff "open %s@." (modul_to_string name)
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-07-14 02:30:48 +02:00
|
|
|
let rec print_type_dec ff { t_name = name; t_desc = tdesc } =
|
2010-08-24 17:13:28 +02:00
|
|
|
let print_type_desc ff = function
|
|
|
|
| Type_abs -> ()
|
|
|
|
| Type_alias ty -> fprintf ff " =@ %a" print_type ty
|
|
|
|
| Type_enum tag_name_list ->
|
2010-09-09 00:35:06 +02:00
|
|
|
fprintf ff " =@ %a" (print_list print_qualname """|""") tag_name_list
|
2010-08-24 17:13:28 +02:00
|
|
|
| Type_struct f_ty_list ->
|
|
|
|
fprintf ff " =@ %a" (print_record print_field) f_ty_list in
|
2010-09-09 00:35:06 +02:00
|
|
|
fprintf ff "@[<2>type %a%a@]@." print_qualname name print_type_desc tdesc
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-08-18 16:16:50 +02:00
|
|
|
let print_contract ff { c_local = l; c_eq = eqs;
|
2010-12-06 18:13:15 +01:00
|
|
|
c_assume = e_a; c_enforce = e_g;
|
2010-12-14 18:29:55 +01:00
|
|
|
c_controllables = c;} =
|
2011-08-04 13:37:33 +02:00
|
|
|
fprintf ff "@[<v2>contract@\n%a%a@ assume %a@ enforce %a@ with %a@\n@]"
|
2010-06-26 16:53:25 +02:00
|
|
|
print_local_vars l
|
|
|
|
print_eqs eqs
|
2011-05-09 19:32:12 +02:00
|
|
|
print_extvalue e_a
|
|
|
|
print_extvalue e_g
|
2010-12-06 18:13:15 +01:00
|
|
|
print_vd_tuple c
|
2010-06-26 16:53:25 +02:00
|
|
|
|
|
|
|
|
2010-08-18 16:16:50 +02:00
|
|
|
let print_node ff { n_name = n; n_input = ni; n_output = no;
|
|
|
|
n_contract = contract; n_local = nl;
|
|
|
|
n_equs = ne; n_params = params } =
|
2010-09-09 00:35:06 +02:00
|
|
|
fprintf ff "@[node %a%a%a@ returns %a@]@\n%a%a%a@]@\n@."
|
|
|
|
print_qualname n
|
2010-06-17 16:09:32 +02:00
|
|
|
print_node_params params
|
2010-06-21 01:45:23 +02:00
|
|
|
print_vd_tuple ni
|
|
|
|
print_vd_tuple no
|
2010-06-17 16:09:32 +02:00
|
|
|
(print_opt print_contract) contract
|
2010-06-19 18:28:52 +02:00
|
|
|
print_local_vars nl
|
2010-06-17 16:09:32 +02:00
|
|
|
print_eqs ne
|
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2011-04-18 19:20:03 +02:00
|
|
|
let print oc { p_opened = pm; p_desc = pd } =
|
|
|
|
let print_program_desc ff pd = match pd with
|
2011-04-19 18:45:56 +02:00
|
|
|
| Pnode n -> print_node ff n
|
|
|
|
| Ptype t -> print_type_dec ff t
|
|
|
|
| Pconst c -> print_const_dec ff c
|
|
|
|
in
|
|
|
|
let ff = formatter_of_out_channel oc in
|
2010-08-29 22:30:51 +02:00
|
|
|
List.iter (print_open_module ff) pm;
|
2011-04-18 19:20:03 +02:00
|
|
|
List.iter (print_program_desc ff) pd;
|
2010-09-09 00:35:06 +02:00
|
|
|
fprintf ff "@?"
|