/tryocaml/js_of_ocaml-patched/compiler/subst.ml

http://github.com/cago/tryocaml · OCaml · 103 lines · 70 code · 12 blank · 21 comment · 0 complexity · 3847031f925d15002e6171906d050507 MD5 · raw file

  1. (* Js_of_ocaml compiler
  2. * http://www.ocsigen.org/js_of_ocaml/
  3. * Copyright (C) 2010 Jérôme Vouillon
  4. * Laboratoire PPS - CNRS Université Paris Diderot
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, with linking exception;
  9. * either version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *)
  20. open Code
  21. let expr s e =
  22. match e with
  23. Const _ | Constant _ ->
  24. e
  25. | Apply (f, l, n) ->
  26. Apply (s f, List.map (fun x -> s x) l, n)
  27. | Block (n, a) ->
  28. Block (n, Array.map (fun x -> s x) a)
  29. | Field (x, n) ->
  30. Field (s x, n)
  31. | Closure (l, pc) ->
  32. Closure (l, pc)
  33. | Prim (p, l) ->
  34. Prim (p, List.map (fun x -> match x with Pv x -> Pv (s x) | Pc _ -> x) l)
  35. let instr s i =
  36. match i with
  37. Let (x, e) ->
  38. Let (x, expr s e)
  39. | Set_field (x, n, y) ->
  40. Set_field (s x, n, s y)
  41. | Offset_ref (x, n) ->
  42. Offset_ref (s x, n)
  43. | Array_set (x, y, z) ->
  44. Array_set (s x, s y, s z)
  45. let instrs s l = List.map (fun i -> instr s i) l
  46. let subst_cont s (pc, arg) = (pc, List.map (fun x -> s x) arg)
  47. let last s l =
  48. match l with
  49. Stop ->
  50. l
  51. | Branch cont ->
  52. Branch (subst_cont s cont)
  53. | Pushtrap (cont1, x, cont2, pc) ->
  54. Pushtrap (subst_cont s cont1, x, subst_cont s cont2, pc)
  55. | Return x ->
  56. Return (s x)
  57. | Raise x ->
  58. Raise (s x)
  59. | Cond (c, x, cont1, cont2) ->
  60. Cond (c, s x, subst_cont s cont1, subst_cont s cont2)
  61. | Switch (x, a1, a2) ->
  62. Switch (s x,
  63. Array.map (fun cont -> subst_cont s cont) a1,
  64. Array.map (fun cont -> subst_cont s cont) a2)
  65. | Poptrap cont ->
  66. Poptrap (subst_cont s cont)
  67. let program s (pc, blocks, free_pc) =
  68. let blocks =
  69. AddrMap.map
  70. (fun block ->
  71. { params = block.params;
  72. handler = Util.opt_map
  73. (fun (x, cont) -> (x, subst_cont s cont)) block.handler;
  74. body = instrs s block.body;
  75. branch = last s block.branch }) blocks
  76. in
  77. (pc, blocks, free_pc)
  78. (****)
  79. let from_array s =
  80. fun x -> match s.(Var.idx x) with Some y -> y | None -> x
  81. (****)
  82. let rec build_mapping params args =
  83. match params, args with
  84. x :: params, y :: args ->
  85. VarMap.add x y (build_mapping params args)
  86. | [], _ ->
  87. VarMap.empty
  88. | _ ->
  89. assert false
  90. let from_map m =
  91. fun x -> try VarMap.find x m with Not_found -> x