serial.ml 4.87 KB
Newer Older
1
(*
2 3 4 5 6 7 8 9
let cu = Types.CompUnit.mk (Encodings.Utf8.mk "OCAML")
let () = Types.CompUnit.enter cu; Types.caml_mode := true
let init = ref []
let serialize = ref []

module Mk(X : Custom.T) = struct
  module A = Custom.Array(X)
  
10 11 12 13 14 15
  type pchunk = { mutable nb : int; mutable lst : X.t list }
  let put c x = 
    let i = c.nb in
    c.nb <- succ i;
    c.lst <- x::c.lst;
    i
16
    
17 18 19
  let init () = { nb = 0; lst = [] }
  let serialize s c = Serialize.Put.array X.serialize s 
		      (Array.of_list (List.rev c.lst))
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

  type 'a entry = Serialized of X.t | Computed of 'a
  type 'a chunk = 'a entry array

  let deserialize s = 
    Serialize.Get.array (fun s -> Serialized (X.deserialize s)) s

  let get f a i =
    match a.(i) with
      | Serialized x ->
	  let x = f x in
	  a.(i) <- Computed x;
	  x
      | Computed x ->
	  x
end

module PM = Mk(Custom.Pair(Types)(Custom.List(Patterns.Node)))
module CONST = Mk(Types.Const)
39
module LAB = Mk(Ident.LabelPool)
40
module T = Mk(Types)
41
module T2 = Mk(Custom.Pair(Types)(Types))
42
module LABA = Mk(Custom.Array(Ident.LabelPool))
43
module TAG = Mk(Atoms.V)
44
module TAGA = Mk(Custom.Array(Custom.Pair(Atoms.V)(Custom.Int)))
45 46

module P = struct
47 48 49 50 51
  type chunk =
      { pm : PM.pchunk;
	cst : CONST.pchunk;
	lab : LAB.pchunk;
	typ : T.pchunk;
52
 	laba : LABA.pchunk;
53
	tag : TAG.pchunk;
54
	taga : TAGA.pchunk;
55
	typ2 : T2.pchunk;
56
     }
57

58 59 60 61
  let init () = 
    { pm = PM.init ();
      cst = CONST.init ();
      lab = LAB.init ();
62 63
      typ = T.init ();
      laba = LABA.init ();
64
      tag = TAG.init ();
65
      taga = TAGA.init ();
66
      typ2 = T2.init ();
67
    }
68 69 70 71 72

  let serialize s c =
    PM.serialize s c.pm;
    CONST.serialize s c.cst;
    LAB.serialize s c.lab;
73
    T.serialize s c.typ;
74
    LABA.serialize s c.laba;
75 76
    TAG.serialize s c.tag;
    TAGA.serialize s c.taga;
77
    T2.serialize s c.typ2;
78
    ()
79 80 81 82 83

  let pm c = PM.put c.pm
  let const c = CONST.put c.cst
  let label c = LAB.put c.lab
  let typ c = T.put c.typ
84
  let label_array c = LABA.put c.laba
85
  let tag c = TAG.put c.tag
86
  let tag_array c = TAGA.put c.taga
87
  let typ2 c t1 t2 = T2.put c.typ2 (t1,t2)
88 89 90

  let mk c =
    let s = Serialize.Put.run serialize c in
91 92
    ignore (Types.CompUnit.close_serialize ());
    s
93 94 95 96

end

module G = struct
97 98 99 100 101
  type chunk =
      { pm :
	  (Patterns.Compile.dispatcher * int Patterns.Compile.rhs array)
	  PM.chunk;
	cst : Value.t CONST.chunk;
102
	lab : Ident.label LAB.chunk;
103
	typ : Types.t T.chunk;
104
	laba : Ident.label array LABA.chunk;
105
	tag : Value.t TAG.chunk;
106
	taga : int Atoms.map TAGA.chunk;
107
	typ2 : (Value.t -> unit) T2.chunk;
108 109 110 111 112
      }

  let deserialize s =
    let pm = PM.deserialize s in
    let cst = CONST.deserialize s in
113
    let lab = LAB.deserialize s in
114
    let typ = T.deserialize s in
115
    let laba = LABA.deserialize s in
116
    let tag = TAG.deserialize s in
117
    let taga = TAGA.deserialize s in
118
    let typ2 = T2.deserialize s in
119
    { pm = pm; cst = cst; lab = lab; typ = typ; laba = laba; tag = tag;
120
      taga = taga; typ2 = typ2 }
121 122

  let mk s = 
123 124 125 126 127 128 129
    Types.clear_deserialize_table ();
    Serialize.Get.run deserialize s

  let mk_pm (t,brs) =
    let brs = Array.to_list (Array.mapi (fun i x -> (x,i)) (Array.of_list brs))
    in
    Patterns.Compile.make_branches t brs
130

131 132
  let pm chunk i v =
    let (d,rhs) = PM.get mk_pm chunk.pm i in
133 134
    let (code,bindings) = Run_dispatch.run_dispatcher d v in
    match rhs.(code) with
135
      | Patterns.Compile.Fail -> (-1,[||])
136 137 138 139 140
      | Patterns.Compile.Match (bind,i) ->
	  i,
	  Array.map
	    (fun (_,i) -> if (i == -1) then v else bindings.(i))
	    (Array.of_list bind)
141 142 143 144


  let const chunk i =
    CONST.get Value.const chunk.cst i
145
	  
146 147
  let remove_label chunk i v =
    Value.remove_field (LAB.get (fun x -> x) chunk.lab i) v
148 149
  let get_field chunk i v =
    Value.get_field v (LAB.get (fun x -> x) chunk.lab i)
150 151 152

  let typ chunk i =
    T.get (fun x -> x) chunk.typ i
153

154 155 156
  let check chunk i v =
    T2.get (fun (t0,t) -> Explain.check t0 t) chunk.typ2 i v;
    v
157

158 159
  let record chunk i vs =
    Value.mk_record (LABA.get (fun x -> x) chunk.laba i) vs
160

161 162 163
  let constr_const chunk i =
    TAG.get (fun x -> Value.Atom x) chunk.tag i

164
  let constr chunk i vs =
165
    Value.ocaml2cduce_constr (constr_const chunk i) vs
166

167 168 169 170 171 172 173 174
  let taga chunk i = 
    TAGA.get
      (fun x ->
	 let x = Array.map (fun (t,i) ->
			      Atoms.atom t, i) x in
	 Atoms.mk_map (Array.to_list x))
      chunk.taga i

175
  let dconstr chunk i v =
176 177 178
    Value.cduce2ocaml_constr (taga chunk i) v
  let dvariant chunk i v =
    Value.cduce2ocaml_variant (taga chunk i) v
179
end
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
module P = struct
  type chunk = {
    mutable nb : int;
    mutable objs : Obj.t list
  }

  let init () = { nb = 0; objs = [] }

  let mk c =
    let o = Array.of_list (List.rev c.objs) in
    Marshal.to_string (Value.extract_all (), o) []

  let put c x =
    let i = c.nb in
    c.nb <- succ i;
    c.objs <- Obj.repr x :: c.objs;
    i
end

module G = struct
  let mk s =
    let (pools,objs) = Marshal.from_string s 0 in
    Value.intract_all pools;
    objs
end