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-07-05 16:02:38 +02:00
|
|
|
open Obc
|
|
|
|
open Format
|
|
|
|
open Pp_tools
|
2010-07-08 17:17:00 +02:00
|
|
|
open Types
|
2010-07-23 19:45:19 +02:00
|
|
|
open Idents
|
2010-07-08 17:17:00 +02:00
|
|
|
open Names
|
2010-09-10 14:29:13 +02:00
|
|
|
open Global_printer
|
2010-07-05 16:02:38 +02:00
|
|
|
|
|
|
|
let print_vd ff vd =
|
|
|
|
fprintf ff "@[<v>";
|
2011-04-27 08:26:40 +02:00
|
|
|
if vd.v_mutable then
|
|
|
|
fprintf ff "mutable ";
|
2010-07-05 16:02:38 +02:00
|
|
|
print_ident ff vd.v_ident;
|
|
|
|
fprintf ff ": ";
|
|
|
|
print_type ff vd.v_type;
|
|
|
|
fprintf ff "@]"
|
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
let print_obj ff o =
|
2011-01-20 23:05:18 +01:00
|
|
|
fprintf ff "@[<v>"; print_ident ff o.o_ident;
|
2010-09-09 00:35:06 +02:00
|
|
|
fprintf ff " : "; print_qualname ff o.o_class;
|
2010-07-22 09:44:57 +02:00
|
|
|
fprintf ff "@[<2>%a@]" (print_list_r print_static_exp "<<"","">>") o.o_params;
|
2010-07-08 17:17:00 +02:00
|
|
|
(match o.o_size with
|
2011-06-27 10:58:14 +02:00
|
|
|
| Some se -> fprintf ff "%a" (print_list_r print_static_exp "[" "][" "]") se
|
2010-07-08 17:17:00 +02:00
|
|
|
| None -> ());
|
2010-08-18 23:45:07 +02:00
|
|
|
fprintf ff "@]"
|
2010-07-05 16:02:38 +02:00
|
|
|
|
|
|
|
let rec print_lhs ff e =
|
2010-11-05 15:36:11 +01:00
|
|
|
match e.pat_desc with
|
2010-07-08 17:17:00 +02:00
|
|
|
| Lvar x -> print_ident ff x
|
|
|
|
| Lmem x -> fprintf ff "mem("; print_ident ff x; fprintf ff ")"
|
|
|
|
| Lfield (l, f) -> print_lhs ff l; fprintf ff ".%s" (shortname f)
|
|
|
|
| Larray(x, idx) ->
|
2010-07-05 16:02:38 +02:00
|
|
|
print_lhs ff x;
|
|
|
|
fprintf ff "[";
|
|
|
|
print_exp ff idx;
|
|
|
|
fprintf ff "]"
|
|
|
|
|
2011-05-30 10:06:16 +02:00
|
|
|
and print_ext_value ff w = match w.w_desc with
|
|
|
|
| Wvar x -> print_ident ff x
|
|
|
|
| Wconst c -> print_static_exp ff c
|
|
|
|
| Wmem x -> fprintf ff "mem("; print_ident ff x; fprintf ff ")"
|
|
|
|
| Wfield (l, f) -> print_ext_value ff l; fprintf ff ".%s" (shortname f)
|
|
|
|
| Warray(x, idx) ->
|
|
|
|
print_ext_value ff x;
|
|
|
|
fprintf ff "[";
|
|
|
|
print_exp ff idx;
|
|
|
|
fprintf ff "]"
|
|
|
|
|
|
|
|
|
2010-07-05 16:02:38 +02:00
|
|
|
and print_exps ff e_list = print_list_r print_exp "" "," "" ff e_list
|
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
and print_exp ff e =
|
|
|
|
match e.e_desc with
|
2011-05-30 10:06:16 +02:00
|
|
|
| Eextvalue lhs -> print_ext_value ff lhs
|
2010-07-08 17:17:00 +02:00
|
|
|
| Eop(op, e_list) -> print_op ff op e_list
|
|
|
|
| Estruct(_,f_e_list) ->
|
|
|
|
fprintf ff "@[<v 1>";
|
|
|
|
print_list_r
|
2010-09-09 00:35:06 +02:00
|
|
|
(fun ff (field, e) -> print_qualname ff field;fprintf ff " = ";
|
2010-07-08 17:17:00 +02:00
|
|
|
print_exp ff e)
|
|
|
|
"{" ";" "}" ff f_e_list;
|
|
|
|
fprintf ff "@]"
|
|
|
|
| Earray e_list ->
|
|
|
|
fprintf ff "@[";
|
|
|
|
print_list_r print_exp "[" ";" "]" ff e_list;
|
|
|
|
fprintf ff "@]"
|
2010-07-05 16:02:38 +02:00
|
|
|
|
2010-08-18 23:45:07 +02:00
|
|
|
and print_op ff op e_list = match e_list with
|
|
|
|
| [l; r] ->
|
2010-09-09 00:35:06 +02:00
|
|
|
fprintf ff "(@[%a@ %a %a@])" print_qualname op print_exp l print_exp r
|
2010-08-18 23:45:07 +02:00
|
|
|
| _ ->
|
2010-09-09 00:35:06 +02:00
|
|
|
print_qualname ff op;
|
2010-08-18 23:45:07 +02:00
|
|
|
print_list_l print_exp "(" "," ")" ff e_list
|
2010-07-05 16:02:38 +02:00
|
|
|
|
|
|
|
let print_asgn ff pref x e =
|
|
|
|
fprintf ff "@[%s" pref; print_lhs ff x; fprintf ff " = ";
|
|
|
|
fprintf ff "@["; print_exp ff e; fprintf ff "@]";
|
|
|
|
fprintf ff "@]"
|
|
|
|
|
|
|
|
let print_obj_call ff = function
|
2011-01-20 23:05:18 +01:00
|
|
|
| Oobj o -> print_ident ff o
|
2010-07-08 17:17:00 +02:00
|
|
|
| Oarray (o, i) ->
|
2011-06-27 10:58:14 +02:00
|
|
|
fprintf ff "%a%a"
|
2011-01-20 23:05:18 +01:00
|
|
|
print_ident o
|
2011-06-27 10:58:14 +02:00
|
|
|
(print_list_r print_lhs "[" "][" "]") i
|
2010-07-05 16:02:38 +02:00
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
let print_method_name ff = function
|
|
|
|
| Mstep -> fprintf ff "step"
|
|
|
|
| Mreset -> fprintf ff "reset"
|
|
|
|
|
2011-01-05 15:51:55 +01:00
|
|
|
|
2010-07-05 16:02:38 +02:00
|
|
|
let rec print_act ff a =
|
2011-01-05 15:47:53 +01:00
|
|
|
let print_lhs_tuple ff var_list = match var_list with
|
|
|
|
| [] -> ()
|
|
|
|
| _ -> fprintf ff "@[(%a)@] =@ " (print_list print_lhs "" "," "") var_list in
|
2010-07-05 16:02:38 +02:00
|
|
|
match a with
|
2010-07-08 17:17:00 +02:00
|
|
|
| Aassgn (x, e) -> print_asgn ff "" x e
|
|
|
|
| Acase(e, tag_act_list) ->
|
2010-12-14 19:34:09 +01:00
|
|
|
fprintf ff "@[<v>@[<v 2>switch (";
|
2010-08-18 23:45:07 +02:00
|
|
|
print_exp ff e; fprintf ff ") {@ ";
|
2010-07-05 16:02:38 +02:00
|
|
|
print_tag_act_list ff tag_act_list;
|
|
|
|
fprintf ff "@]@,}@]"
|
2010-07-08 17:17:00 +02:00
|
|
|
| Afor(x, i1, i2, act_list) ->
|
2011-01-24 16:07:26 +01:00
|
|
|
fprintf ff "@[<v>@[<v 2>for %a = %a to %a {@ %a @]@,}@]"
|
|
|
|
print_vd x
|
2011-04-18 15:38:42 +02:00
|
|
|
print_exp i1
|
|
|
|
print_exp i2
|
2010-08-18 23:45:07 +02:00
|
|
|
print_block act_list
|
2011-04-28 15:20:21 +02:00
|
|
|
| Aop (op, es) ->
|
|
|
|
print_op ff op es
|
2010-07-08 17:17:00 +02:00
|
|
|
| Acall (var_list, o, meth, es) ->
|
2010-08-18 23:45:07 +02:00
|
|
|
fprintf ff "@[<2>%a%a.%a(%a)@]"
|
|
|
|
print_lhs_tuple var_list
|
|
|
|
print_obj_call o
|
2011-01-05 15:51:55 +01:00
|
|
|
print_method_name meth
|
|
|
|
print_exps es
|
2011-03-08 13:41:28 +01:00
|
|
|
| Ablock b ->
|
|
|
|
fprintf ff "do@\n %a@\ndone" print_block b
|
2010-08-18 23:45:07 +02:00
|
|
|
|
|
|
|
and print_var_dec_list ff var_dec_list = match var_dec_list with
|
|
|
|
| [] -> ()
|
|
|
|
| _ ->
|
|
|
|
fprintf ff "@[<hov 4>%a@]@ "
|
|
|
|
(print_list_r print_vd "var " ";" ";") var_dec_list
|
|
|
|
|
|
|
|
and print_block ff b =
|
|
|
|
fprintf ff "@[<v>%a%a@]"
|
|
|
|
print_var_dec_list b.b_locals
|
|
|
|
(print_list_r print_act "" ";" "") b.b_body
|
2010-07-05 16:02:38 +02:00
|
|
|
|
|
|
|
and print_tag_act_list ff tag_act_list =
|
|
|
|
print_list
|
|
|
|
(fun ff (tag, a) ->
|
2010-08-18 23:45:07 +02:00
|
|
|
fprintf ff "@[<v 2>case %a:@ %a@]"
|
2010-09-09 00:35:06 +02:00
|
|
|
print_qualname tag
|
2010-08-18 23:45:07 +02:00
|
|
|
print_block a)
|
|
|
|
"" "" "" ff tag_act_list
|
2010-07-05 16:02:38 +02:00
|
|
|
|
2010-07-13 08:38:51 +02:00
|
|
|
let print_method_name ff = function
|
|
|
|
| Mreset -> fprintf ff "reset"
|
|
|
|
| Mstep -> fprintf ff "step"
|
|
|
|
|
2010-08-18 23:45:07 +02:00
|
|
|
let print_arg_list ff var_list =
|
|
|
|
fprintf ff "(@[%a@])" (print_list_r print_vd "" "," "") var_list
|
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
let print_method ff md =
|
2010-08-18 23:45:07 +02:00
|
|
|
fprintf ff "@[<v 2>@[%a%a@ returns %a {@]@ %a@]@\n}"
|
|
|
|
print_method_name md.m_name
|
|
|
|
print_arg_list md.m_inputs
|
|
|
|
print_arg_list md.m_outputs
|
|
|
|
print_block md.m_body
|
|
|
|
|
|
|
|
let print_class_def ff
|
2010-07-08 17:17:00 +02:00
|
|
|
{ cd_name = id; cd_mems = mem; cd_objs = objs; cd_methods = m_list } =
|
2010-09-13 09:03:15 +02:00
|
|
|
fprintf ff "@[<v 2>machine "; print_qualname ff id; fprintf ff " =@,";
|
2010-07-05 16:02:38 +02:00
|
|
|
if mem <> [] then begin
|
|
|
|
fprintf ff "@[<hov 4>var ";
|
|
|
|
print_list_r print_vd "" ";" "" ff mem;
|
|
|
|
fprintf ff ";@]@,"
|
|
|
|
end;
|
|
|
|
if objs <> [] then begin
|
|
|
|
fprintf ff "@[<hov 4>obj ";
|
|
|
|
print_list print_obj "" ";" "" ff objs;
|
|
|
|
fprintf ff ";@]@,"
|
|
|
|
end;
|
2010-08-18 23:45:07 +02:00
|
|
|
if mem <> [] || objs <> [] then fprintf ff "@,";
|
2010-07-08 17:17:00 +02:00
|
|
|
print_list_r print_method "" "\n" "" ff m_list;
|
2010-07-05 16:02:38 +02:00
|
|
|
fprintf ff "@]"
|
|
|
|
|
2011-03-21 14:30:19 +01:00
|
|
|
|
2010-07-05 16:02:38 +02:00
|
|
|
let print_type_def ff { t_name = name; t_desc = tdesc } =
|
|
|
|
match tdesc with
|
2010-09-13 09:03:15 +02:00
|
|
|
| Type_abs -> fprintf ff "@[type %a@\n@]" print_qualname name
|
2010-07-26 17:41:52 +02:00
|
|
|
| Type_alias ty ->
|
2010-09-13 09:03:15 +02:00
|
|
|
fprintf ff "@[type %a@ = %a@\n@]" print_qualname name print_type ty
|
2010-07-05 16:02:38 +02:00
|
|
|
| Type_enum(tag_name_list) ->
|
2010-09-13 09:03:15 +02:00
|
|
|
fprintf ff "@[type %a = " print_qualname name;
|
2010-09-13 13:44:26 +02:00
|
|
|
print_list_r print_qualname "" "|" "" ff tag_name_list;
|
2010-07-05 16:02:38 +02:00
|
|
|
fprintf ff "@\n@]"
|
|
|
|
| Type_struct(f_ty_list) ->
|
2010-09-13 09:03:15 +02:00
|
|
|
fprintf ff "@[type %a = " print_qualname name;
|
2010-07-05 16:02:38 +02:00
|
|
|
fprintf ff "@[<v 1>";
|
|
|
|
print_list
|
2010-07-13 14:03:39 +02:00
|
|
|
(fun ff { Signature.f_name = field; Signature.f_type = ty } ->
|
2010-09-09 00:35:06 +02:00
|
|
|
print_qualname ff field;
|
2010-07-05 16:02:38 +02:00
|
|
|
fprintf ff ": ";
|
|
|
|
print_type ff ty) "{" ";" "}" ff f_ty_list;
|
|
|
|
fprintf ff "@]@.@]"
|
|
|
|
|
|
|
|
let print_open_module ff name =
|
2011-02-07 14:24:17 +01:00
|
|
|
fprintf ff "open %s@." (modul_to_string name)
|
2010-07-05 16:02:38 +02:00
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
let print_const_dec ff c =
|
2010-09-13 09:03:15 +02:00
|
|
|
fprintf ff "const %a = %a@." print_qualname c.c_name
|
2010-07-08 17:17:00 +02:00
|
|
|
print_static_exp c.c_value
|
|
|
|
|
2011-04-19 09:49:00 +02:00
|
|
|
let print_prog_desc ff pd = match pd with
|
|
|
|
| Pclass cd -> print_class_def ff cd; fprintf ff "@\n@\n"
|
2011-04-19 18:45:56 +02:00
|
|
|
| Pconst cd -> print_const_dec ff cd
|
|
|
|
| Ptype td -> print_type_def ff td
|
2011-04-19 09:49:00 +02:00
|
|
|
|
|
|
|
let print_prog ff { p_opened = modules; p_desc = descs } =
|
2010-07-05 16:02:38 +02:00
|
|
|
List.iter (print_open_module ff) modules;
|
2011-04-19 09:49:00 +02:00
|
|
|
List.iter (print_prog_desc ff) descs
|
2010-07-05 16:02:38 +02:00
|
|
|
|
|
|
|
let print oc p =
|
|
|
|
let ff = formatter_of_out_channel oc in
|
2010-08-18 23:45:07 +02:00
|
|
|
fprintf ff "@[-- Code generated by the MiniLucid Compiler@.";
|
|
|
|
fprintf ff "@[<v>"; print_prog ff p; fprintf ff "@]@]@."
|
2010-07-05 16:02:38 +02:00
|
|
|
|