|
| 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) |
0 commit comments