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-06-15 10:49:03 +02:00
|
|
|
open Names
|
2010-08-26 13:55:29 +02:00
|
|
|
open Types
|
|
|
|
open Misc
|
2010-06-15 10:49:03 +02:00
|
|
|
open Location
|
2010-06-18 11:20:35 +02:00
|
|
|
open Signature
|
2010-08-26 13:55:29 +02:00
|
|
|
open Modules
|
|
|
|
open Static
|
|
|
|
open Global_mapfold
|
|
|
|
open Mls_mapfold
|
|
|
|
open Minils
|
2010-09-10 14:29:13 +02:00
|
|
|
open Global_printer
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-08-26 13:55:29 +02:00
|
|
|
module Error =
|
|
|
|
struct
|
|
|
|
type error =
|
2010-09-09 00:35:06 +02:00
|
|
|
| Enode_unbound of qualname
|
2011-03-21 14:30:19 +01:00
|
|
|
| Epartial_evaluation of static_exp list
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-08-26 13:55:29 +02:00
|
|
|
let message loc kind =
|
|
|
|
begin match kind with
|
|
|
|
| Enode_unbound ln ->
|
|
|
|
Format.eprintf "%aUnknown node '%s'@."
|
|
|
|
print_location loc
|
|
|
|
(fullname ln)
|
2011-03-21 14:30:19 +01:00
|
|
|
| Epartial_evaluation se_l ->
|
|
|
|
Format.eprintf "%aUnable to fully instanciate the static exps '%a'@."
|
|
|
|
print_location loc
|
|
|
|
print_static_exp_tuple se_l
|
2010-08-26 13:55:29 +02:00
|
|
|
end;
|
2010-09-15 09:38:52 +02:00
|
|
|
raise Errors.Error
|
2010-08-26 13:55:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
module Param_instances :
|
|
|
|
sig
|
|
|
|
type key = private static_exp (** Fully instantiated param *)
|
2010-09-10 14:29:13 +02:00
|
|
|
type env = key QualEnv.t
|
2010-08-26 13:55:29 +02:00
|
|
|
val instantiate: env -> static_exp list -> key list
|
2010-09-09 00:35:06 +02:00
|
|
|
val get_node_instances : QualEnv.key -> key list list
|
|
|
|
val add_node_instance : QualEnv.key -> key list -> unit
|
2010-08-26 13:55:29 +02:00
|
|
|
val build : env -> param list -> key list -> env
|
|
|
|
module Instantiate :
|
|
|
|
sig
|
|
|
|
val program : program -> program
|
|
|
|
end
|
|
|
|
end =
|
|
|
|
struct
|
|
|
|
type key = static_exp
|
2010-09-10 14:29:13 +02:00
|
|
|
type env = key QualEnv.t
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** An instance is a list of instantiated params *)
|
|
|
|
type instance = key list
|
|
|
|
(** two instances are equal if the desc of keys are equal *)
|
|
|
|
let compare_instances =
|
|
|
|
let compare se1 se2 = compare se1.se_desc se2.se_desc in
|
2010-09-30 19:24:41 +02:00
|
|
|
Misc.list_compare compare
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
module S = (** Instances set *)
|
|
|
|
Set.Make(
|
|
|
|
struct
|
|
|
|
type t = instance
|
|
|
|
let compare = compare_instances
|
|
|
|
end)
|
|
|
|
|
|
|
|
module M = (** Map instance to its instantiated node *)
|
|
|
|
Map.Make(
|
|
|
|
struct
|
2010-09-09 00:35:06 +02:00
|
|
|
type t = qualname * instance
|
2010-08-26 13:55:29 +02:00
|
|
|
let compare (l1,i1) (l2,i2) =
|
|
|
|
let cl = compare l1 l2 in
|
|
|
|
if cl = 0 then compare_instances i1 i2 else cl
|
|
|
|
end)
|
|
|
|
|
|
|
|
(** Maps a couple (node name, params) to the name of the instantiated node *)
|
|
|
|
let nodes_names = ref M.empty
|
|
|
|
|
|
|
|
(** Maps a node to its list of instances *)
|
2010-09-09 00:35:06 +02:00
|
|
|
let nodes_instances = ref QualEnv.empty
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** create a params instance *)
|
2011-03-21 14:30:19 +01:00
|
|
|
let instantiate m se_l =
|
|
|
|
try List.map (eval m) se_l
|
|
|
|
with Errors.Error ->
|
|
|
|
Error.message no_location (Error.Epartial_evaluation se_l)
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** @return the name of the node corresponding to the instance of
|
|
|
|
[ln] with the static parameters [params]. *)
|
|
|
|
let node_for_params_call ln params = match params with
|
|
|
|
| [] -> ln
|
|
|
|
| _ -> let ln = M.find (ln,params) !nodes_names in ln
|
|
|
|
|
|
|
|
(** Generates a fresh name for the the instance of
|
|
|
|
[ln] with the static parameters [params] and stores it. *)
|
|
|
|
let generate_new_name ln params = match params with
|
|
|
|
| [] -> nodes_names := M.add (ln, params) ln !nodes_names
|
2010-12-14 18:29:55 +01:00
|
|
|
| _ ->
|
|
|
|
let { qual = q; name = n } = ln in
|
|
|
|
let param_string =
|
|
|
|
List.fold_left
|
|
|
|
(fun s se ->
|
|
|
|
s ^ (Names.print_pp_to_name Global_printer.print_static_exp se))
|
|
|
|
"_params_" params in
|
|
|
|
let new_ln =
|
|
|
|
Modules.fresh_value_in "callgraph" (n^param_string^"_") q in
|
|
|
|
nodes_names := M.add (ln, params) new_ln !nodes_names
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** Adds an instance of a node. *)
|
|
|
|
let add_node_instance ln params =
|
|
|
|
(* get the already defined instances *)
|
2010-09-09 00:35:06 +02:00
|
|
|
let instances = try QualEnv.find ln !nodes_instances
|
2010-08-26 13:55:29 +02:00
|
|
|
with Not_found -> S.empty in
|
|
|
|
if S.mem params instances then () (* nothing to do *)
|
|
|
|
else ( (* it's a new instance *)
|
|
|
|
let instances = S.add params instances in
|
2010-09-09 00:35:06 +02:00
|
|
|
nodes_instances := QualEnv.add ln instances !nodes_instances;
|
2010-08-26 13:55:29 +02:00
|
|
|
generate_new_name ln params )
|
|
|
|
|
|
|
|
(** @return the list of instances of a node. *)
|
|
|
|
let get_node_instances ln =
|
|
|
|
let instances_set =
|
2010-09-09 00:35:06 +02:00
|
|
|
try QualEnv.find ln !nodes_instances
|
2010-08-26 13:55:29 +02:00
|
|
|
with Not_found -> S.empty in
|
|
|
|
S.elements instances_set
|
|
|
|
|
|
|
|
|
|
|
|
(** Build an environment by instantiating the passed params *)
|
|
|
|
let build env params_names params_values =
|
2010-09-10 14:29:13 +02:00
|
|
|
List.fold_left2 (fun m { p_name = n } v -> QualEnv.add (local_qn n) v m)
|
2010-08-26 13:55:29 +02:00
|
|
|
env params_names (instantiate env params_values)
|
|
|
|
|
|
|
|
|
|
|
|
(** This module creates an instance of a node with a given
|
|
|
|
list of static parameters. *)
|
|
|
|
module Instantiate =
|
|
|
|
struct
|
|
|
|
(** Replaces static parameters with their value in the instance. *)
|
|
|
|
let static_exp funs m se =
|
|
|
|
let se, _ = Global_mapfold.static_exp funs m se in
|
|
|
|
let se = match se.se_desc with
|
2010-09-10 14:29:13 +02:00
|
|
|
| Svar q ->
|
2011-02-07 14:24:17 +01:00
|
|
|
(match q.qual with
|
|
|
|
| LocalModule -> (* This var is a static parameter, it has to be instanciated *)
|
|
|
|
(try QualEnv.find q m
|
2011-05-23 09:24:57 +02:00
|
|
|
with Not_found -> Misc.internal_error "callgraph")
|
2011-02-07 14:24:17 +01:00
|
|
|
| _ -> se)
|
2010-08-26 13:55:29 +02:00
|
|
|
| _ -> se in
|
|
|
|
se, m
|
|
|
|
|
|
|
|
(** Replaces nodes call with the call to the correct instance. *)
|
|
|
|
let edesc funs m ed =
|
|
|
|
let ed, _ = Mls_mapfold.edesc funs m ed in
|
|
|
|
let ed = match ed with
|
|
|
|
| Eapp ({ a_op = Efun ln; a_params = params } as app, e_list, r) ->
|
|
|
|
let op = Efun (node_for_params_call ln (instantiate m params)) in
|
|
|
|
Eapp ({ app with a_op = op; a_params = [] }, e_list, r)
|
|
|
|
| Eapp ({ a_op = Enode ln; a_params = params } as app, e_list, r) ->
|
|
|
|
let op = Enode (node_for_params_call ln (instantiate m params)) in
|
|
|
|
Eapp ({ app with a_op = op; a_params = [] }, e_list, r)
|
|
|
|
| Eiterator(it, ({ a_op = Efun ln; a_params = params } as app),
|
2011-03-21 17:22:03 +01:00
|
|
|
n, pe_list, e_list, r) ->
|
2010-08-26 13:55:29 +02:00
|
|
|
let op = Efun (node_for_params_call ln (instantiate m params)) in
|
2011-03-21 17:22:03 +01:00
|
|
|
Eiterator(it, {app with a_op = op; a_params = [] },
|
|
|
|
n, pe_list, e_list, r)
|
2010-08-26 13:55:29 +02:00
|
|
|
| Eiterator(it, ({ a_op = Enode ln; a_params = params } as app),
|
2011-03-21 17:22:03 +01:00
|
|
|
n, pe_list, e_list, r) ->
|
2010-08-26 13:55:29 +02:00
|
|
|
let op = Enode (node_for_params_call ln (instantiate m params)) in
|
2011-03-21 17:22:03 +01:00
|
|
|
Eiterator(it,{app with a_op = op; a_params = [] },
|
|
|
|
n, pe_list, e_list, r)
|
2010-08-26 13:55:29 +02:00
|
|
|
| _ -> ed
|
|
|
|
in ed, m
|
|
|
|
|
2011-01-07 17:16:50 +01:00
|
|
|
let node_dec_instance n params =
|
2010-12-14 18:29:55 +01:00
|
|
|
Idents.enter_node n.n_name;
|
2010-08-26 13:55:29 +02:00
|
|
|
let global_funs =
|
|
|
|
{ Global_mapfold.defaults with static_exp = static_exp } in
|
|
|
|
let funs =
|
|
|
|
{ Mls_mapfold.defaults with edesc = edesc;
|
|
|
|
global_funs = global_funs } in
|
2010-09-10 14:29:13 +02:00
|
|
|
let m = build QualEnv.empty n.n_params params in
|
2010-08-26 13:55:29 +02:00
|
|
|
let n, _ = Mls_mapfold.node_dec_it funs m n in
|
|
|
|
|
|
|
|
(* Add to the global environment the signature of the new instance *)
|
2010-09-10 14:29:13 +02:00
|
|
|
let node_sig = find_value n.n_name in
|
2010-08-26 13:55:29 +02:00
|
|
|
let node_sig, _ = Global_mapfold.node_it global_funs m node_sig in
|
|
|
|
let node_sig = { node_sig with node_params = [];
|
2011-06-09 14:38:58 +02:00
|
|
|
node_param_constraints = [] } in
|
2010-08-26 13:55:29 +02:00
|
|
|
(* Find the name that was associated to this instance *)
|
2010-09-09 00:35:06 +02:00
|
|
|
let ln = node_for_params_call n.n_name params in
|
2010-09-13 09:12:10 +02:00
|
|
|
if not (check_value ln) then
|
|
|
|
Modules.add_value ln node_sig;
|
2011-06-09 14:38:58 +02:00
|
|
|
{ n with n_name = ln; n_params = []; n_param_constraints = []; }
|
2010-08-26 13:55:29 +02:00
|
|
|
|
2011-01-07 17:16:50 +01:00
|
|
|
let node_dec n =
|
|
|
|
List.map (node_dec_instance n) (get_node_instances n.n_name)
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
let program p =
|
2011-04-19 18:45:56 +02:00
|
|
|
let program_desc pd acc = match pd with
|
|
|
|
| Pnode n ->
|
|
|
|
let nds = node_dec n in
|
|
|
|
List.fold_left (fun pds n -> Pnode n :: pds) acc nds
|
|
|
|
| _ -> pd :: acc
|
|
|
|
in
|
2011-04-19 10:38:48 +02:00
|
|
|
{ p with p_desc = List.fold_right program_desc p.p_desc [] }
|
2010-08-26 13:55:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
open Param_instances
|
|
|
|
|
|
|
|
type info =
|
2011-02-07 14:24:17 +01:00
|
|
|
{ mutable opened : program ModulEnv.t;
|
2010-09-09 00:35:06 +02:00
|
|
|
mutable called_nodes : ((qualname * static_exp list) list) QualEnv.t; }
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
let info =
|
|
|
|
{ (** opened programs*)
|
2011-02-07 14:24:17 +01:00
|
|
|
opened = ModulEnv.empty;
|
2010-08-26 13:55:29 +02:00
|
|
|
(** Maps a node to the list of (node name, params) it calls *)
|
2010-09-09 00:35:06 +02:00
|
|
|
called_nodes = QualEnv.empty }
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** Loads the modname.epo file. *)
|
2011-02-07 14:24:17 +01:00
|
|
|
let load_object_file modul =
|
|
|
|
Modules.open_module modul;
|
|
|
|
let modname = match modul with
|
|
|
|
| Names.Pervasives -> "Pervasives"
|
|
|
|
| Names.Module n -> n
|
2011-05-23 09:24:57 +02:00
|
|
|
| Names.LocalModule -> Misc.internal_error "modules"
|
|
|
|
| Names.QualModule _ -> Misc.unsupported "modules"
|
2011-02-07 14:24:17 +01:00
|
|
|
in
|
2010-08-26 13:55:29 +02:00
|
|
|
let name = String.uncapitalize modname in
|
|
|
|
try
|
2010-09-15 09:38:52 +02:00
|
|
|
let filename = Compiler_utils.findfile (name ^ ".epo") in
|
2010-08-26 13:55:29 +02:00
|
|
|
let ic = open_in_bin filename in
|
|
|
|
try
|
2011-01-07 17:16:50 +01:00
|
|
|
let (p : program) = input_value ic in
|
2010-08-26 13:55:29 +02:00
|
|
|
if p.p_format_version <> minils_format_version then (
|
|
|
|
Format.eprintf "The file %s was compiled with \
|
2010-09-01 13:31:28 +02:00
|
|
|
an older version of the compiler.@\n\
|
|
|
|
Please recompile %s.ept first.@." filename name;
|
2010-09-15 09:38:52 +02:00
|
|
|
raise Errors.Error
|
2010-08-26 13:55:29 +02:00
|
|
|
);
|
|
|
|
close_in ic;
|
2011-02-07 14:24:17 +01:00
|
|
|
info.opened <- ModulEnv.add p.p_modname p info.opened
|
2010-08-26 13:55:29 +02:00
|
|
|
with
|
|
|
|
| End_of_file | Failure _ ->
|
|
|
|
close_in ic;
|
2010-09-01 13:31:28 +02:00
|
|
|
Format.eprintf "Corrupted object file %s.@\n\
|
|
|
|
Please recompile %s.ept first.@." filename name;
|
2010-09-15 09:38:52 +02:00
|
|
|
raise Errors.Error
|
2010-08-26 13:55:29 +02:00
|
|
|
with
|
2010-09-15 09:38:52 +02:00
|
|
|
| Compiler_utils.Cannot_find_file(filename) ->
|
2010-09-01 13:31:28 +02:00
|
|
|
Format.eprintf "Cannot find the object file '%s'.@."
|
2010-08-26 13:55:29 +02:00
|
|
|
filename;
|
2010-09-15 09:38:52 +02:00
|
|
|
raise Errors.Error
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** @return the node with name [ln], loading the corresponding
|
|
|
|
object file if necessary. *)
|
2010-09-14 09:39:02 +02:00
|
|
|
let node_by_longname node =
|
2011-02-07 14:24:17 +01:00
|
|
|
if not (ModulEnv.mem node.qual info.opened)
|
2010-09-14 09:39:02 +02:00
|
|
|
then load_object_file node.qual;
|
2010-09-09 00:35:06 +02:00
|
|
|
try
|
2011-02-07 14:24:17 +01:00
|
|
|
let p = ModulEnv.find node.qual info.opened in
|
2011-04-18 19:20:03 +02:00
|
|
|
let n = List.find (function Pnode n -> n.n_name = node | _ -> false) p.p_desc in
|
2011-04-19 18:45:56 +02:00
|
|
|
(match n with
|
|
|
|
| Pnode n -> n
|
2011-06-09 14:12:32 +02:00
|
|
|
| _ -> Misc.internal_error "callgraph")
|
2010-09-09 00:35:06 +02:00
|
|
|
with
|
|
|
|
Not_found -> Error.message no_location (Error.Enode_unbound node)
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** @return the list of nodes called by the node named [ln], with the
|
|
|
|
corresponding params (static parameters appear as free variables). *)
|
|
|
|
let collect_node_calls ln =
|
2012-02-21 16:07:29 +01:00
|
|
|
(** only add nodes when not external and with params *)
|
2010-08-26 13:55:29 +02:00
|
|
|
let add_called_node ln params acc =
|
|
|
|
match params with
|
|
|
|
| [] -> acc
|
|
|
|
| _ ->
|
2012-02-21 16:07:29 +01:00
|
|
|
if (Modules.find_value ln).node_external
|
|
|
|
then acc
|
|
|
|
else (ln, params)::acc
|
2010-08-26 13:55:29 +02:00
|
|
|
in
|
2010-09-14 09:39:02 +02:00
|
|
|
let edesc _ acc ed = match ed with
|
2010-08-26 13:55:29 +02:00
|
|
|
| Eapp ({ a_op = (Enode ln | Efun ln); a_params = params }, _, _) ->
|
|
|
|
ed, add_called_node ln params acc
|
|
|
|
| Eiterator(_, { a_op = (Enode ln | Efun ln); a_params = params },
|
2011-03-21 17:22:03 +01:00
|
|
|
_, _, _, _) ->
|
2010-08-26 13:55:29 +02:00
|
|
|
ed, add_called_node ln params acc
|
2010-09-15 09:38:52 +02:00
|
|
|
| _ -> raise Errors.Fallback
|
2010-08-26 13:55:29 +02:00
|
|
|
in
|
|
|
|
let funs = { Mls_mapfold.defaults with edesc = edesc } in
|
|
|
|
let n = node_by_longname ln in
|
|
|
|
let _, acc = Mls_mapfold.node_dec funs [] n in
|
2012-02-21 16:07:29 +01:00
|
|
|
acc
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** @return the list of nodes called by the node named [ln]. This list is
|
|
|
|
computed lazily the first time it is needed. *)
|
|
|
|
let called_nodes ln =
|
2010-09-09 00:35:06 +02:00
|
|
|
if not (QualEnv.mem ln info.called_nodes) then (
|
2010-08-26 13:55:29 +02:00
|
|
|
let called = collect_node_calls ln in
|
2010-09-09 00:35:06 +02:00
|
|
|
info.called_nodes <- QualEnv.add ln called info.called_nodes;
|
2010-08-26 13:55:29 +02:00
|
|
|
called
|
|
|
|
) else
|
2010-09-09 00:35:06 +02:00
|
|
|
QualEnv.find ln info.called_nodes
|
2010-08-26 13:55:29 +02:00
|
|
|
|
|
|
|
(** Generates the list of instances of nodes needed to call
|
|
|
|
[ln] with static parameters [params]. *)
|
|
|
|
let rec call_node (ln, params) =
|
|
|
|
(* First, add the instance for this node *)
|
|
|
|
let n = node_by_longname ln in
|
2010-09-10 14:29:13 +02:00
|
|
|
let m = build QualEnv.empty n.n_params params in
|
2010-08-26 13:55:29 +02:00
|
|
|
(* List.iter check_no_static_var params; *)
|
|
|
|
add_node_instance ln params;
|
2010-06-18 11:20:35 +02:00
|
|
|
|
2010-08-26 13:55:29 +02:00
|
|
|
(* Recursively generate instances for called nodes. *)
|
|
|
|
let call_list = called_nodes ln in
|
|
|
|
let call_list =
|
|
|
|
List.map (fun (ln, p) -> ln, instantiate m p) call_list in
|
|
|
|
List.iter call_node call_list
|
|
|
|
|
|
|
|
let program p =
|
|
|
|
(* Find the nodes without static parameters *)
|
2011-04-18 19:20:03 +02:00
|
|
|
let main_nodes = List.filter (function Pnode n -> is_empty n.n_params | _ -> false) p.p_desc in
|
|
|
|
let main_nodes = List.map (function Pnode n -> n.n_name, []
|
2011-05-23 09:24:57 +02:00
|
|
|
| _ -> Misc.internal_error "callgraph") main_nodes in
|
2011-02-07 14:24:17 +01:00
|
|
|
info.opened <- ModulEnv.add p.p_modname p ModulEnv.empty;
|
2010-12-14 18:29:55 +01:00
|
|
|
(* Creates the list of instances starting from these nodes *)
|
|
|
|
List.iter call_node main_nodes;
|
2011-02-07 14:24:17 +01:00
|
|
|
let p_list = ModulEnv.fold (fun _ p l -> p::l) info.opened [] in
|
2010-12-14 18:29:55 +01:00
|
|
|
(* Generate all the needed instances *)
|
|
|
|
List.map Param_instances.Instantiate.program p_list
|