run_dispatch.ml 10.2 KB
Newer Older
1 2
(* Running dispatchers *)

3 4 5
(* Possible simple optimizations:
     - in make_result_prod, see if buffer can be simply overwritten
       (precompute this ...)
6
     - optimize for Xml elements (don't build the Pair (attr,content))
7 8
*)

9 10 11 12
(*
let (<) : int -> int -> bool = (<);;
*)

13
open Value
14
open Ident
15
open Patterns.Compile
16
open Encodings
17

18 19 20 21 22 23 24 25

let buffer = ref (Array.create 127 Absent)
let cursor = ref 0

let blit a1 ofs1 a2 ofs2 len =
  for i = 0 to len - 1 do
    Array.unsafe_set a2 (ofs2 + i) (Array.unsafe_get a1 (ofs1 + i))
  done
26
(* important to do this in the increasing order ... *)
27 28 29 30 31 32 33 34


let ensure_room n =
  let l = Array.length !buffer in
  if !cursor + n > l then
    let buffer' = Array.create (l * 2 + n) Absent in
    blit !buffer 0 buffer' 0 !cursor;
    buffer := buffer'
35 36 37 38 39 40 41

let push v =
  ensure_room 1;
  !buffer.(!cursor) <- v;
  incr cursor


42
let make_result_prod v1 v2 v (code,r,pop) = 
43
  let n = Array.length r in
44
  if n > 0 then (
45 46
  ensure_room n;
  let buf = !buffer in
47
  let c = !cursor in
48 49 50 51
  for a = 0 to n - 1 do
    let x = match Array.unsafe_get r a with
      | Catch -> v
      | Const c -> const c
52 53 54 55
      | Nil -> nil
      | Left -> v1
      | Right -> v2
      | Stack i -> buf.(c - i)
56
      | Recompose (i,j) -> 
57
	  Pair (
58 59
	    (match i with (-1) -> v1 | (-2) -> nil | _ -> buf.(c - i)),
	    (match j with (-1) -> v2 | (-2) -> nil | _ -> buf.(c - j))
60
	  )
61
    in
62
    buf.(c + a) <- x
63
  done;
64 65 66
  if pop != 0 then blit buf c buf (c - pop) n);
  cursor := !cursor - pop + n;  (* clean space for GC ? *)
  code
67

68
let make_result_basic v (code,r,_) = 
69
  let n = Array.length r in
70
  if n > 0 then (
71 72 73 74 75 76 77 78
  ensure_room n;
  let buf = !buffer in
  for a = 0 to n - 1 do
    let x = match Array.unsafe_get r a with
      | Catch -> v
      | Const c -> const c 
      | _ -> assert false
    in
79 80
    buf.(!cursor) <- x;
    incr cursor
81 82
  done);
  code
83

84

85
let make_result_char ch (code,r,_) = 
86
  let n = Array.length r in
87
  if n > 0 then (
88 89 90 91 92 93 94 95 96 97
  ensure_room n;
  let buf = !buffer in
  for a = 0 to n - 1 do
    let x = match Array.unsafe_get r a with
      | Catch -> Char ch
      | Const c -> const c
      | _ -> assert false
    in
    buf.(!cursor + a) <- x
  done;
98 99
  cursor := !cursor + n);
  code
100

101
let tail_string_latin1 i j s q =
102
  if i + 1 == j then q else String_latin1 (i + 1,j,s,q)
103

104
let make_result_string_latin1 i j s q (code,r,pop) = 
105
  let n = Array.length r in
106
  if n > 0 then (
107
  ensure_room n;
108
  let c = !cursor in
109 110 111
  let buf = !buffer in
  for a = 0 to n - 1 do
    let x = match Array.unsafe_get r a with
112
      | Catch -> String_latin1 (i,j,s,q)
113
      | Const c -> const c
114 115 116 117
      | Nil -> nil
      | Left -> Char (Chars.V.mk_char s.[i])
      | Right -> tail_string_latin1 i j s q
      | Stack n -> buf.(c - n)
118
      | Recompose (n,m) -> 
119 120 121 122
	  Pair (
	    (match n with 
	       | (-1) -> Char (Chars.V.mk_char s.[i]) 
	       | (-2) -> nil 
123
	       | _ -> buf.(c - n)),
124 125 126
	    (match m with 
	       | (-1) -> tail_string_latin1 i j s q 
	       | (-2) -> nil 
127
	       | _ -> buf.(c - m))
128
	  )
129
    in
130
    buf.(c + a) <- x
131
  done;
132 133 134
  if pop != 0 then blit buf c buf (c - pop) n);
  cursor := !cursor - pop + n;
  code
135

136 137 138 139
let tail_string_utf8 i j s q =
  let i = Utf8.advance s i in
  if Utf8.equal_index i j then q else String_utf8 (i,j,s,q)

140
let make_result_string_utf8 i j s q (code,r,pop) = 
141
  let n = Array.length r in
142
  if n > 0 then (
143
  ensure_room n;
144
  let c = !cursor in
145 146 147 148 149
  let buf = !buffer in
  for a = 0 to n - 1 do
    let x = match Array.unsafe_get r a with
      | Catch -> String_utf8 (i,j,s,q)
      | Const c -> const c
150 151 152 153
      | Nil -> nil
      | Left -> Char (Chars.V.mk_int (Utf8.get s i))
      | Right -> tail_string_utf8 i j s q 
      | Stack n -> buf.(c - n)
154
      | Recompose (n,m) -> 
155 156 157 158
	  Pair (
	    (match n with 
	       | (-1) -> Char (Chars.V.mk_int (Utf8.get s i)) 
	       | (-2) -> nil 
159
	       | _ -> buf.(c - n)),
160 161 162
	    (match m with 
	       | (-1) -> tail_string_utf8 i j s q 
	       | (-2) -> nil 
163
	       | _ -> buf.(c - m))
164
	  )
165
    in
166
    buf.(c + a) <- x
167
  done;
168 169 170 171
  if pop != 0 then blit buf c buf (c - pop) n;
  );
  cursor := !cursor - pop + n;
  code
172

173 174 175
let rec run_disp_basic v f =  function
  | [(_,r)] -> make_result_basic v r
  | (t,r)::rem -> if f t then make_result_basic v r else run_disp_basic v f rem
176 177 178
  | _ ->  
      Format.fprintf Format.std_formatter "ERR: %a@." Value.print v;
      assert false
179

180
let rec run_dispatcher d v = 
181 182 183
(*  Format.fprintf Format.std_formatter "Matching (%a) with:@." Value.print v;
  Patterns.Compile.print_dispatcher Format.std_formatter d;  *)

184 185 186
  match actions d with
    | AIgnore r -> make_result_basic v r
    | AKind k -> run_disp_kind k v
187

188 189
and run_disp_kind actions v =
  match v with
190
  | Pair (v1,v2) -> run_disp_prod v v1 v2 actions.prod
191 192
  | Xml (v1,v2,v3) 
  | XmlNs (v1,v2,v3,_) -> run_disp_prod v v1 (Pair (v2,v3)) actions.xml
193
  | Record r -> run_disp_record 0 v r actions.record
194
  | String_latin1 (i,j,s,q) -> 
195 196
(*      run_disp_kind actions (Value.normalize v)  *)
       run_disp_string_latin1 i j s q actions 
197
  | String_utf8 (i,j,s,q) -> 
198 199
(*      run_disp_kind actions (Value.normalize v)  *)
 	run_disp_string_utf8 i j s q actions  
200 201
  | Atom a -> make_result_basic v (Atoms.get_map a actions.atoms) 
  | Char c -> make_result_basic v (Chars.get_map c actions.chars) 
202
  | Integer i ->
203
      run_disp_basic v (fun t -> Types.Int.has_int t i) actions.basic
204 205 206 207
  | Abstraction (None,_) ->
      run_disp_basic v (fun t -> failwith "Run-time inspection of external abstraction")
        actions.basic
  | Abstraction (Some iface,_)
208
  | Abstraction2 (_,iface,_) ->
209
      run_disp_basic v (fun t -> Types.Arrow.check_iface iface t) 
210
        actions.basic
211 212 213
  | Abstract (abs,_) -> 
      run_disp_basic v (fun t -> Types.Abstract.contains abs (Types.get_abstract t))
	actions.basic
214 215
  | Absent ->
      run_disp_basic v (fun t -> Types.Record.has_absent t) actions.basic
216
  | Concat (_,_) as v -> run_disp_kind actions (Value.normalize v)
217

218
and run_disp_prod v v1 v2 = function
219 220
  | Impossible -> assert false
  | TailCall d1 -> run_dispatcher d1 v1
221
  | Ignore d2 -> run_disp_prod2 v1 v v2 d2
222
  | Dispatch (d1,b1) ->
223
      let code1 = run_dispatcher d1 v1 in
224
      run_disp_prod2 v1 v v2 b1.(code1)
225

226
and run_disp_prod2 v1 v v2 = function
227
  | Impossible -> assert false
228
  | Ignore r -> make_result_prod v1 v2 v r
229 230
  | TailCall d2 -> run_dispatcher d2 v2
  | Dispatch (d2,b2) ->
231
      let code2 = run_dispatcher d2 v2 in
232
      make_result_prod v1 v2 v b2.(code2)
233

234
and run_disp_record n v fields = function
235
  | None -> assert false
236
  | Some (RecLabel (l,d)) ->
237 238 239
      (* TODO: get rid of this exception... *)
      (try run_disp_record1 v (succ n) (Imap.find fields l) fields d
       with Not_found -> run_disp_record1 v n Absent fields d)
240
  | Some (RecNolabel (some,none)) ->
241
      let r = if (n < Imap.cardinal fields) then some else none in
242 243 244 245
      match r with
	| Some r -> make_result_basic v r
	| None -> assert false
      
246
and run_disp_record1 v n v1 rem = function
247 248
  | Impossible -> assert false
  | TailCall d1 -> run_dispatcher d1 v1
249
  | Ignore d2 ->  run_disp_record2 v n v1 rem d2
250
  | Dispatch (d1,b1) ->
251
      let code1 = run_dispatcher d1 v1 in
252
      run_disp_record2 v n v1 rem b1.(code1)
253

254
and run_disp_record2 v n v1 rem = function
255
  | Impossible -> assert false
256
  | Ignore r -> make_result_prod v1 Absent v r
257
  | TailCall d2 -> run_disp_record_loop v n rem d2
258
  | Dispatch (d2,b2) ->
259
      let code2 = run_disp_record_loop v n rem d2 in
260
      make_result_prod v1 Absent v b2.(code2)
261

262
and run_disp_record_loop v n rem d =
263
  match actions d with
264
    | AIgnore r -> make_result_basic v r
265
    | AKind k -> run_disp_record n v rem k.record
266
  
267

268
and run_disp_string_latin1 i j s q actions = 
269
  if i == j then run_disp_kind actions q 
270 271
  else match actions.prod with
    | Impossible -> assert false
272
    | TailCall d1 -> run_disp_string_latin1_char d1 (Chars.V.mk_char s.[i])
273
    | Ignore d2 -> run_disp_string_latin1_2 i j s q d2
274
    | Dispatch (d1,b1) ->
275
	let code1 = run_disp_string_latin1_char d1 (Chars.V.mk_char s.[i]) in
276
	run_disp_string_latin1_2 i j s q b1.(code1)
277 278 279 280
and run_disp_string_latin1_char d ch =
  match actions d with
    | AIgnore r -> make_result_char ch r
    | AKind k -> make_result_char ch (Chars.get_map ch k.chars) 
281
and run_disp_string_latin1_2 i j s q = function
282 283
  | Impossible -> assert false
  | Ignore r -> 
284
      make_result_string_latin1 i j s q r
285 286 287
  | TailCall d2 -> run_disp_string_latin1_loop i j s q d2
  | Dispatch (d2,b2) ->
      let code2 = run_disp_string_latin1_loop i j s q d2 in
288
      make_result_string_latin1 i j s q b2.(code2)
289
and run_disp_string_latin1_loop i j s q d =
290 291
  let i = succ i in
  if i == j then run_dispatcher d q else
292
  match actions d with
293 294
    | AIgnore r -> make_result_basic (Value.String_latin1 (i,j,s,q)) r
    | AKind k -> run_disp_string_latin1 i j s q k
295 296

and run_disp_string_utf8 i j s q actions = 
297 298 299
  if Utf8.equal_index i j then run_disp_kind actions q
  else
  match actions.prod with
300
    | Impossible -> assert false
301
    | TailCall d1 -> run_disp_string_utf8_char d1 (Chars.V.mk_int (Utf8.get s i))
302
    | Ignore d2 -> run_disp_string_utf8_2 i j s q d2
303
    | Dispatch (d1,b1) ->
304
	let code1 = run_disp_string_utf8_char d1 (Chars.V.mk_int (Utf8.get s i)) in
305
	run_disp_string_utf8_2 i j s q b1.(code1)
306
and run_disp_string_utf8_char d ch =
307 308
  match actions d with
    | AIgnore r -> make_result_char ch r
309
    | AKind k -> make_result_char ch (Chars.get_map ch k.chars) 
310
and run_disp_string_utf8_2 i j s q = function
311 312
  | Impossible -> assert false
  | Ignore r -> 
313
      make_result_string_utf8 i j s q r
314
  | TailCall d2 -> run_disp_string_utf8_loop i j s q d2
315
  | Dispatch (d2,b2) ->
316
      let code2 = run_disp_string_utf8_loop i j s q d2 in
317
      make_result_string_utf8 i j s q b2.(code2)
318
and run_disp_string_utf8_loop i j s q d =
319 320
  let i = Utf8.advance s i in
  if Utf8.equal_index i j then run_dispatcher d q else
321
  match actions d with
322 323
    | AIgnore r -> make_result_basic (Value.String_utf8 (i,j,s,q)) r
    | AKind k -> run_disp_string_utf8 i j s q k
324 325 326

let run_dispatcher d v =
  let code = run_dispatcher d v in
327 328
  cursor := 0;
  (code,!buffer) 
329

330 331 332 333 334 335 336
(*
let rec check_overwrite_aux r i =
  if i < 0 then true
  else match r.(i) with
    | Right j | Recompose (_,j) -> 
	if (j < 0) || (j >=i ) then check_overwrite_aux r (i - 1) else false
    | _ -> check_overwrite_aux r (i - 1)
337

338 339 340 341 342 343

let check_overwrite r2 r =
  (Array.length r2 = Array.length r) && (check_overwrite_aux r (Array.length r - 1))


*)