2010-06-15 10:49:03 +02:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon *)
|
|
|
|
(* *)
|
|
|
|
(* Author : Marc Pouzet *)
|
|
|
|
(* Organization : Demons, LRI, University of Paris-Sud, Orsay *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
(* Object code internal representation *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Names
|
2010-07-23 19:45:19 +02:00
|
|
|
open Idents
|
2010-07-07 11:17:18 +02:00
|
|
|
open Types
|
2010-07-13 14:03:39 +02:00
|
|
|
open Signature
|
2010-07-08 17:17:00 +02:00
|
|
|
open Location
|
2010-07-07 15:11:32 +02:00
|
|
|
|
2010-09-13 09:03:15 +02:00
|
|
|
type class_name = qualname
|
2010-09-09 00:35:06 +02:00
|
|
|
type op_name = qualname
|
2011-01-20 23:05:18 +01:00
|
|
|
type obj_ident = var_ident
|
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
type type_dec =
|
2011-01-20 23:05:18 +01:00
|
|
|
{ t_name : type_name;
|
2010-07-07 15:11:32 +02:00
|
|
|
t_desc : tdesc;
|
2010-07-08 17:17:00 +02:00
|
|
|
t_loc : location }
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
and tdesc =
|
2010-06-26 16:53:25 +02:00
|
|
|
| Type_abs
|
2010-07-26 17:41:52 +02:00
|
|
|
| Type_alias of ty
|
2010-09-13 13:44:26 +02:00
|
|
|
| Type_enum of constructor_name list
|
2010-07-13 14:03:39 +02:00
|
|
|
| Type_struct of structure
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
type const_dec = {
|
2010-09-13 09:03:15 +02:00
|
|
|
c_name : qualname;
|
2010-07-08 17:17:00 +02:00
|
|
|
c_value : static_exp;
|
2010-07-13 14:03:39 +02:00
|
|
|
c_type : ty;
|
2010-07-08 17:17:00 +02:00
|
|
|
c_loc : location }
|
|
|
|
|
2010-11-05 15:36:11 +01:00
|
|
|
type pattern = { pat_desc : pat_desc; pat_ty : ty; pat_loc : location }
|
2010-07-07 11:17:18 +02:00
|
|
|
|
2010-11-05 15:36:11 +01:00
|
|
|
and pat_desc =
|
2010-07-07 15:11:32 +02:00
|
|
|
| Lvar of var_ident
|
|
|
|
| Lmem of var_ident
|
2010-11-05 15:36:11 +01:00
|
|
|
| Lfield of pattern * field_name
|
|
|
|
| Larray of pattern * exp
|
2010-07-07 11:17:18 +02:00
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
and exp = { e_desc : exp_desc; e_ty : ty; e_loc : location }
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-07-07 11:17:18 +02:00
|
|
|
and exp_desc =
|
2011-01-24 16:07:26 +01:00
|
|
|
| Epattern of pattern
|
2010-07-07 11:17:18 +02:00
|
|
|
| Econst of static_exp
|
|
|
|
| Eop of op_name * exp list
|
|
|
|
| Estruct of type_name * (field_name * exp) list
|
|
|
|
| Earray of exp list
|
2011-01-05 15:51:55 +01:00
|
|
|
| Ebang of exp
|
2010-06-16 11:32:13 +02:00
|
|
|
|
2010-11-05 15:36:11 +01:00
|
|
|
type obj_ref =
|
2011-01-20 23:05:18 +01:00
|
|
|
| Oobj of obj_ident
|
|
|
|
| Oarray of obj_ident * pattern
|
2010-07-07 11:17:18 +02:00
|
|
|
|
|
|
|
type method_name =
|
|
|
|
| Mreset
|
|
|
|
| Mstep
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
type act =
|
2010-11-05 15:36:11 +01:00
|
|
|
| Aassgn of pattern * exp
|
|
|
|
| Acall of pattern list * obj_ref * method_name * exp list
|
2011-01-05 15:51:55 +01:00
|
|
|
| Aasync_call of async_t * pattern list * obj_ref * method_name * exp list
|
2010-07-07 15:11:32 +02:00
|
|
|
| Acase of exp * (constructor_name * block) list
|
2011-01-24 16:07:26 +01:00
|
|
|
| Afor of var_dec * static_exp * static_exp * block
|
2010-07-07 11:17:18 +02:00
|
|
|
|
2010-07-22 09:36:22 +02:00
|
|
|
and block =
|
|
|
|
{ b_locals : var_dec list;
|
|
|
|
b_body : act list }
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-07-22 09:36:22 +02:00
|
|
|
and var_dec =
|
2010-07-07 15:11:32 +02:00
|
|
|
{ v_ident : var_ident;
|
2011-02-07 14:24:17 +01:00
|
|
|
v_type : ty;
|
2010-07-08 17:17:00 +02:00
|
|
|
v_loc : location }
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
type obj_dec =
|
2011-01-20 23:05:18 +01:00
|
|
|
{ o_ident : obj_ident;
|
2011-02-07 14:24:17 +01:00
|
|
|
o_async : async_t option;
|
2011-01-20 23:05:18 +01:00
|
|
|
o_class : class_name;
|
2010-07-22 09:44:57 +02:00
|
|
|
o_params : static_exp list;
|
2011-01-20 23:05:18 +01:00
|
|
|
o_size : static_exp option; (** size of the array if the declaration is an array of obj *)
|
2010-07-08 17:17:00 +02:00
|
|
|
o_loc : location }
|
2010-07-07 09:44:23 +02:00
|
|
|
|
|
|
|
type method_def =
|
2010-07-09 09:31:12 +02:00
|
|
|
{ m_name : method_name;
|
2010-07-08 17:17:00 +02:00
|
|
|
m_inputs : var_dec list;
|
|
|
|
m_outputs : var_dec list;
|
2010-07-21 15:53:50 +02:00
|
|
|
m_body : block; }
|
2010-07-07 09:44:23 +02:00
|
|
|
|
|
|
|
type class_def =
|
2010-07-08 17:17:00 +02:00
|
|
|
{ cd_name : class_name;
|
|
|
|
cd_mems : var_dec list;
|
|
|
|
cd_objs : obj_dec list;
|
2010-07-15 11:31:32 +02:00
|
|
|
cd_params : param list;
|
2010-07-08 17:17:00 +02:00
|
|
|
cd_methods: method_def list;
|
|
|
|
cd_loc : location }
|
2010-07-07 09:44:23 +02:00
|
|
|
|
|
|
|
type program =
|
2011-02-07 14:24:17 +01:00
|
|
|
{ p_modname : modul;
|
|
|
|
p_opened : modul list;
|
2010-07-07 09:44:23 +02:00
|
|
|
p_types : type_dec list;
|
2010-07-08 17:17:00 +02:00
|
|
|
p_consts : const_dec list;
|
2010-07-07 09:44:23 +02:00
|
|
|
p_defs : class_def list }
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let mk_var_dec ?(loc=no_location) ident ty =
|
|
|
|
{ v_ident = ident; v_type = ty; v_loc = loc }
|
2010-06-18 10:30:23 +02:00
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let mk_exp ?(loc=no_location) ty desc =
|
2010-07-08 17:17:00 +02:00
|
|
|
{ e_desc = desc; e_ty = ty; e_loc = loc }
|
2010-07-07 11:17:18 +02:00
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let mk_exp_int ?(loc=no_location) desc =
|
|
|
|
{ e_desc = desc; e_ty = Initial.tint; e_loc = loc }
|
|
|
|
|
|
|
|
let mk_exp_bool ?(loc=no_location) desc =
|
|
|
|
{ e_desc = desc; e_ty = Initial.tbool; e_loc = loc }
|
|
|
|
|
|
|
|
let mk_pattern ?(loc=no_location) ty desc =
|
2010-11-05 15:36:11 +01:00
|
|
|
{ pat_desc = desc; pat_ty = ty; pat_loc = loc }
|
2010-07-07 11:17:18 +02:00
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let mk_pattern_int ?(loc=no_location) desc =
|
|
|
|
{ pat_desc = desc; pat_ty = Initial.tint; pat_loc = loc }
|
|
|
|
|
|
|
|
let mk_pattern_exp ty desc =
|
|
|
|
let pat = mk_pattern ty desc in
|
|
|
|
mk_exp ty (Epattern pat)
|
|
|
|
|
|
|
|
let mk_evar ty id =
|
|
|
|
mk_exp ty (Epattern (mk_pattern ty (Lvar id)))
|
2010-07-13 14:03:39 +02:00
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let mk_evar_int id =
|
|
|
|
mk_exp Initial.tint (Epattern (mk_pattern Initial.tint (Lvar id)))
|
2010-07-13 14:03:39 +02:00
|
|
|
|
2010-07-22 09:36:22 +02:00
|
|
|
let mk_block ?(locals=[]) eq_list =
|
|
|
|
{ b_locals = locals;
|
|
|
|
b_body = eq_list }
|
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
let rec var_name x =
|
2010-11-05 15:36:11 +01:00
|
|
|
match x.pat_desc with
|
2010-07-07 11:17:18 +02:00
|
|
|
| Lvar x -> x
|
|
|
|
| Lmem x -> x
|
2010-07-08 17:17:00 +02:00
|
|
|
| Lfield(x,_) -> var_name x
|
|
|
|
| Larray(l, _) -> var_name l
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-06-26 16:53:25 +02:00
|
|
|
(** Returns whether an object of name n belongs to
|
2010-06-15 10:49:03 +02:00
|
|
|
a list of var_dec. *)
|
|
|
|
let rec vd_mem n = function
|
|
|
|
| [] -> false
|
2010-06-27 17:24:31 +02:00
|
|
|
| vd::l -> vd.v_ident = n or (vd_mem n l)
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
(** Returns the var_dec object corresponding to the name n
|
|
|
|
in a list of var_dec. *)
|
|
|
|
let rec vd_find n = function
|
2010-09-01 13:31:28 +02:00
|
|
|
| [] -> Format.eprintf "Not found var %s@." (name n); raise Not_found
|
2010-06-26 16:53:25 +02:00
|
|
|
| vd::l ->
|
2010-06-27 17:24:31 +02:00
|
|
|
if vd.v_ident = n then vd else vd_find n l
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-01-20 23:05:18 +01:00
|
|
|
(** Returns the type of a [var_dec list] *)
|
|
|
|
let vd_list_to_type vd_l = match vd_l with
|
|
|
|
| [] -> Types.Tunit
|
|
|
|
| [vd] -> vd.v_type
|
|
|
|
| _ -> Tprod (List.map (fun vd -> vd.v_type) vd_l)
|
|
|
|
|
|
|
|
let pattern_list_to_type p_l = match p_l with
|
|
|
|
| [] -> Types.Tunit
|
|
|
|
| [p] -> p.pat_ty
|
2011-01-24 16:07:26 +01:00
|
|
|
| _ -> Tprod (List.map (fun p -> p.pat_ty) p_l)
|
2011-01-20 23:05:18 +01:00
|
|
|
|
2011-01-24 16:07:26 +01:00
|
|
|
let pattern_of_exp e = match e.e_desc with
|
|
|
|
| Epattern l -> l
|
2010-06-15 10:49:03 +02:00
|
|
|
| _ -> assert false
|
2010-07-09 09:31:12 +02:00
|
|
|
|
|
|
|
let find_step_method cd =
|
|
|
|
List.find (fun m -> m.m_name = Mstep) cd.cd_methods
|
|
|
|
let find_reset_method cd =
|
|
|
|
List.find (fun m -> m.m_name = Mreset) cd.cd_methods
|
2010-07-13 14:03:39 +02:00
|
|
|
|
2010-11-05 15:36:11 +01:00
|
|
|
let obj_ref_name o =
|
2010-07-13 14:03:39 +02:00
|
|
|
match o with
|
|
|
|
| Oobj obj
|
|
|
|
| Oarray (obj, _) -> obj
|
|
|
|
|