package js_of_ocaml-compiler
Compiler from OCaml bytecode to JavaScript
Install
Dune Dependency
Authors
Maintainers
Sources
js_of_ocaml-5.7.0.tbz
sha256=cdf1dea0d9397a498b630abe8014908b6dd83b10ee0c7ff5777a6443a62983be
sha512=5cf7d883c97f4b81ab73687c8f29b624acaef28c002b5d739108954973671dd5fb4f869d8fedb485b4b17bc8aa10d8b55b30ae7ace21a9350c2a1ac59b28eb60
doc/src/js_of_ocaml-compiler/specialize_js.ml.html
Source file specialize_js.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
(* Js_of_ocaml compiler * http://www.ocsigen.org/js_of_ocaml/ * Copyright (C) 2010 Jérôme Vouillon * Laboratoire PPS - CNRS Université Paris Diderot * Copyright (C) 2013 Hugo Heuzard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, with linking exception; * either version 2.1 of the License, or (at your option) any later version. * * This program 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) open! Stdlib open Code open Flow let specialize_instr info i = match i with | Let (x, Prim (Extern "caml_format_int", [ y; z ])) -> ( match the_string_of info y with | Some "%d" -> ( match the_int info z with | Some i -> Let (x, Constant (String (Int32.to_string i))) | None -> Let (x, Prim (Extern "%caml_format_int_special", [ z ]))) | _ -> i) | Let (x, Prim (Extern "%caml_format_int_special", [ z ])) -> ( match the_int info z with | Some i -> Let (x, Constant (String (Int32.to_string i))) | None -> i) (* inline the String constant argument so that generate.ml can attempt to parse it *) | Let ( x , Prim ( Extern (("caml_js_var" | "caml_js_expr" | "caml_pure_js_expr") as prim) , [ (Pv _ as y) ] ) ) when Config.Flag.safe_string () -> ( match the_string_of info y with | Some s -> Let (x, Prim (Extern prim, [ Pc (String s) ])) | _ -> i) | Let (x, Prim (Extern ("caml_register_named_value" as prim), [ y; z ])) -> ( match the_string_of info y with | Some s when Primitive.need_named_value s -> Let (x, Prim (Extern prim, [ Pc (String s); z ])) | Some _ -> Let (x, Constant (Int 0l)) | None -> i) | Let (x, Prim (Extern "caml_js_call", [ f; o; a ])) -> ( match the_def_of info a with | Some (Block (_, a, _)) -> let a = Array.map a ~f:(fun x -> Pv x) in Let (x, Prim (Extern "%caml_js_opt_call", f :: o :: Array.to_list a)) | _ -> i) | Let (x, Prim (Extern "caml_js_fun_call", [ f; a ])) -> ( match the_def_of info a with | Some (Block (_, a, _)) -> let a = Array.map a ~f:(fun x -> Pv x) in Let (x, Prim (Extern "%caml_js_opt_fun_call", f :: Array.to_list a)) | _ -> i) | Let (x, Prim (Extern "caml_js_meth_call", [ o; m; a ])) -> ( match the_string_of info m with | Some m when Javascript.is_ident m -> ( match the_def_of info a with | Some (Block (_, a, _)) -> let a = Array.map a ~f:(fun x -> Pv x) in Let ( x , Prim ( Extern "%caml_js_opt_meth_call" , o :: Pc (NativeString (Native_string.of_string m)) :: Array.to_list a ) ) | _ -> i) | _ -> i) | Let (x, Prim (Extern "caml_js_new", [ c; a ])) -> ( match the_def_of info a with | Some (Block (_, a, _)) -> let a = Array.map a ~f:(fun x -> Pv x) in Let (x, Prim (Extern "%caml_js_opt_new", c :: Array.to_list a)) | _ -> i) | Let (x, Prim (Extern "caml_js_object", [ a ])) -> ( try let a = match the_def_of info a with | Some (Block (_, a, _)) -> a | _ -> raise Exit in let a = Array.map a ~f:(fun x -> match the_def_of info (Pv x) with | Some (Block (_, [| k; v |], _)) -> let k = match the_string_of info (Pv k) with | Some s when String.is_valid_utf_8 s -> Pc (NativeString (Native_string.of_string s)) | Some _ | None -> raise Exit in [ k; Pv v ] | Some (Constant (Tuple (0, [| String k; v |], (NotArray | Unknown)))) when String.is_valid_utf_8 k -> [ Pc (NativeString (Native_string.of_string k)); Pc v ] | Some _ | None -> raise Exit) in Let (x, Prim (Extern "%caml_js_opt_object", List.flatten (Array.to_list a))) with Exit -> i) | Let (x, Prim (Extern "caml_js_get", [ o; (Pv _ as f) ])) -> ( match the_native_string_of info f with | Some s -> Let (x, Prim (Extern "caml_js_get", [ o; Pc (NativeString s) ])) | _ -> i) | Let (x, Prim (Extern "caml_js_set", [ o; (Pv _ as f); v ])) -> ( match the_native_string_of info f with | Some s -> Let (x, Prim (Extern "caml_js_set", [ o; Pc (NativeString s); v ])) | _ -> i) | Let (x, Prim (Extern "caml_js_delete", [ o; (Pv _ as f) ])) -> ( match the_native_string_of info f with | Some s -> Let (x, Prim (Extern "caml_js_delete", [ o; Pc (NativeString s) ])) | _ -> i) | Let (x, Prim (Extern ("caml_jsstring_of_string" | "caml_js_from_string"), [ y ])) -> ( match the_string_of info y with | Some s when String.is_valid_utf_8 s -> Let (x, Constant (NativeString (Native_string.of_string s))) | Some _ | None -> i) | Let (x, Prim (Extern "caml_jsbytes_of_string", [ y ])) -> ( match the_string_of info y with | Some s -> Let (x, Constant (NativeString (Native_string.of_bytestring s))) | None -> i) | Let (x, Prim (Extern "%int_mul", [ y; z ])) -> ( match the_int info y, the_int info z with | Some j, _ when Int32.(abs j < 0x200000l) -> Let (x, Prim (Extern "%direct_int_mul", [ y; z ])) | _, Some j when Int32.(abs j < 0x200000l) -> Let (x, Prim (Extern "%direct_int_mul", [ y; z ])) | _ -> i) | Let (x, Prim (Extern "%int_div", [ y; z ])) -> ( match the_int info z with | Some j when Int32.(j <> 0l) -> Let (x, Prim (Extern "%direct_int_div", [ y; z ])) | _ -> i) | Let (x, Prim (Extern "%int_mod", [ y; z ])) -> ( match the_int info z with | Some j when Int32.(j <> 0l) -> Let (x, Prim (Extern "%direct_int_mod", [ y; z ])) | _ -> i) | _ -> i let specialize_instrs info l = let rec aux info checks l acc = match l with | [] -> List.rev acc | (i, loc) :: r -> ( (* We make bound checking explicit. Then, we can remove duplicated bound checks. Also, it appears to be more efficient to inline the array access. The bound checking function returns the array, which allows to produce more compact code. *) match i with | Let (x, Prim (Extern "caml_array_get", [ y; z ])) | Let (x, Prim (Extern "caml_array_get_float", [ y; z ])) | Let (x, Prim (Extern "caml_array_get_addr", [ y; z ])) -> let idx = match the_int info z with | Some idx -> `Cst idx | None -> `Var z in if List.mem (y, idx) ~set:checks then let acc = (Let (x, Prim (Extern "caml_array_unsafe_get", [ y; z ])), loc) :: acc in aux info checks r acc else let y' = Code.Var.fresh () in let acc = (Let (x, Prim (Extern "caml_array_unsafe_get", [ Pv y'; z ])), loc) :: (Let (y', Prim (Extern "caml_check_bound", [ y; z ])), noloc) :: acc in aux info ((y, idx) :: checks) r acc | Let (x, Prim (Extern "caml_array_set", [ y; z; t ])) | Let (x, Prim (Extern "caml_array_set_float", [ y; z; t ])) | Let (x, Prim (Extern "caml_array_set_addr", [ y; z; t ])) -> let idx = match the_int info z with | Some idx -> `Cst idx | None -> `Var z in if List.mem (y, idx) ~set:checks then let acc = (Let (x, Prim (Extern "caml_array_unsafe_set", [ y; z; t ])), loc) :: acc in aux info checks r acc else let y' = Code.Var.fresh () in let acc = (Let (x, Prim (Extern "caml_array_unsafe_set", [ Pv y'; z; t ])), loc) :: (Let (y', Prim (Extern "caml_check_bound", [ y; z ])), noloc) :: acc in aux info ((y, idx) :: checks) r acc | _ -> let i = specialize_instr info i in aux info checks r ((i, loc) :: acc)) in aux info [] l [] let specialize_all_instrs info p = let blocks = Addr.Map.map (fun block -> { block with Code.body = specialize_instrs info block.body }) p.blocks in { p with blocks } (****) let f info p = specialize_all_instrs info p let f_once p = let rec loop acc l = match l with | [] -> List.rev acc | (i, loc) :: r -> ( match i with | Let ( x , (Prim ( Extern ( "caml_array_set" | "caml_array_unsafe_set" | "caml_array_set_float" | "caml_array_set_addr" | "caml_array_unsafe_set_float" | "caml_floatarray_unsafe_set" ) , [ _; _; _ ] ) as p) ) -> let x' = Code.Var.fork x in let acc = (Let (x', p), loc) :: (Let (x, Constant (Int 0l)), loc) :: acc in loop acc r | _ -> loop ((i, loc) :: acc) r) in let blocks = Addr.Map.map (fun block -> { block with Code.body = loop [] block.body }) p.blocks in { p with blocks }
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>