Skip to content

Commit b908b19

Browse files
committed
WIP
1 parent ca5829f commit b908b19

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

compiler/lib/shape.ml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
(* Js_of_ocaml compiler
2+
* http://www.ocsigen.org/js_of_ocaml/
3+
* Copyright (C) 2024 Hugo Heuzard
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, with linking exception;
8+
* either version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
*)
19+
20+
open! Stdlib
21+
22+
type t =
23+
| Bot of string
24+
| Block of t list
25+
| Function of
26+
{ arity : int
27+
; pure : bool
28+
; res : t
29+
}
30+
31+
let rec to_string (shape : t) =
32+
match shape with
33+
| Bot _s -> "N"
34+
| Block l -> "[" ^ String.concat ~sep:"," (List.map ~f:to_string l) ^ "]"
35+
| Function { arity; _ } -> Printf.sprintf "F(%d)" arity
36+
37+
type key =
38+
| Name of string
39+
| Var of Code.Var.t
40+
41+
module Hashtbl = Hashtbl.Make (struct
42+
type t = key
43+
44+
let equal a b = Poly.(a = b)
45+
46+
let hash = function
47+
| Name s -> Hashtbl.hash s
48+
| Var x -> Code.Var.idx x
49+
end)
50+
51+
let state : t Hashtbl.t = Hashtbl.create 17
52+
53+
let set_shape ~name shape = Hashtbl.add state (Name name) shape
54+
55+
let get_shape ~name = Hashtbl.find_opt state (Name name)
56+
57+
let assign x shape = Hashtbl.add state (Var x) shape
58+
59+
let propagate x offset target =
60+
match Hashtbl.find_opt state (Var x) with
61+
| None -> ()
62+
| Some (Bot _ | Function _) -> ()
63+
| Some (Block l) -> Hashtbl.replace state (Var target) (List.nth l offset)
64+
65+
let get x = Hashtbl.find_opt state (Var x)
66+
67+
let reset () =
68+
Hashtbl.to_seq_keys state
69+
|> Seq.filter (function
70+
| Name _ -> false
71+
| Var _ -> true)
72+
|> List.of_seq
73+
|> List.iter ~f:(Hashtbl.remove state)

compiler/lib/shape.mli

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
(* Js_of_ocaml compiler
2+
* http://www.ocsigen.org/js_of_ocaml/
3+
* Copyright (C) 2024 Hugo Heuzard
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, with linking exception;
8+
* either version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
*)
19+
20+
type t =
21+
| Bot of string
22+
| Block of t list
23+
| Function of
24+
{ arity : int
25+
; pure : bool
26+
; res : t
27+
}
28+
29+
val to_string : t -> string
30+
31+
val set_shape : name:string -> t -> unit
32+
33+
val get_shape : name:string -> t option
34+
35+
val propagate : Code.Var.t -> int -> Code.Var.t -> unit
36+
37+
val assign : Code.Var.t -> t -> unit
38+
39+
val get : Code.Var.t -> t option
40+
41+
val reset : unit -> unit

0 commit comments

Comments
 (0)