heptagon/compiler/obc/obc.ml

156 lines
3.9 KiB
OCaml
Raw Normal View History

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
open Ident
2010-07-07 11:17:18 +02:00
open Types
open Signature
2010-07-08 17:17:00 +02:00
open Location
2010-06-15 10:49:03 +02:00
type class_name = name
type instance_name = longname
type obj_name = name
type op_name = longname
type type_dec =
{ t_name : name;
t_desc : tdesc;
2010-07-08 17:17:00 +02:00
t_loc : location }
2010-06-15 10:49:03 +02:00
and tdesc =
| Type_abs
| Type_enum of name list
| Type_struct of structure
2010-06-15 10:49:03 +02:00
2010-07-08 17:17:00 +02:00
type const_dec = {
c_name : name;
c_value : static_exp;
c_type : ty;
2010-07-08 17:17:00 +02:00
c_loc : location }
type lhs = { l_desc : lhs_desc; l_ty : ty; l_loc : location }
2010-07-07 11:17:18 +02:00
and lhs_desc =
| Lvar of var_ident
| Lmem of var_ident
2010-07-07 11:17:18 +02:00
| Lfield of lhs * field_name
| Larray of lhs * exp
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 =
| Elhs of lhs
| Econst of static_exp
| Eop of op_name * exp list
| Estruct of type_name * (field_name * exp) list
| Earray of exp list
type obj_call =
2010-07-07 11:17:18 +02:00
| Oobj of obj_name
| Oarray of obj_name * lhs
type method_name =
| Mreset
| Mstep
| Mmethod of name
2010-06-15 10:49:03 +02:00
type act =
2010-07-07 11:17:18 +02:00
| Aassgn of lhs * exp
| Acall of lhs list * obj_call * method_name * exp list
| Acase of exp * (constructor_name * block) list
| Afor of var_ident * static_exp * static_exp * block
2010-07-07 11:17:18 +02:00
and block = act list
2010-06-15 10:49:03 +02:00
type var_dec =
{ v_ident : var_ident;
v_type : ty; (* TODO should be here, v_controllable : bool*)
2010-07-08 17:17:00 +02:00
v_loc : location }
2010-06-15 10:49:03 +02:00
type obj_dec =
2010-07-07 09:44:23 +02:00
{ o_name : obj_name;
o_class : instance_name;
2010-07-08 17:17:00 +02:00
o_size : static_exp option;
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;
m_locals : var_dec list;
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;
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 =
{ p_modname : name;
p_opened : name 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
2010-07-08 17:17:00 +02:00
let mk_var_dec ?(loc=no_location) name ty =
{ v_ident = name; v_type = ty; v_loc = loc }
2010-06-18 10:30:23 +02:00
2010-07-08 17:17:00 +02:00
let mk_exp ?(ty=invalid_type) ?(loc=no_location) desc =
{ e_desc = desc; e_ty = ty; e_loc = loc }
2010-07-07 11:17:18 +02:00
2010-07-08 17:17:00 +02:00
let mk_lhs ?(ty=invalid_type) ?(loc=no_location) desc =
{ l_desc = desc; l_ty = ty; l_loc = loc }
2010-07-07 11:17:18 +02:00
let mk_lhs_exp ?(ty=invalid_type) desc =
let lhs = mk_lhs ~ty:ty desc in
mk_exp ~ty:ty (Elhs lhs)
let mk_evar id =
mk_exp (Elhs (mk_lhs (Lvar id)))
2010-07-08 17:17:00 +02:00
let rec var_name x =
match x.l_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
(** 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
| 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
| [] -> Format.printf "Not found var %s\n" (name n); raise Not_found
| vd::l ->
if vd.v_ident = n then vd else vd_find n l
2010-06-15 10:49:03 +02:00
let lhs_of_exp e = match e.e_desc with
2010-07-07 11:17:18 +02:00
| Elhs 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
let obj_call_name o =
match o with
| Oobj obj
| Oarray (obj, _) -> obj