run_dispatch.ml 10.4 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 false v (Imap.elements 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 other v fields = function
235
  | None -> assert false
236
  | Some (RecLabel (l,d)) ->
237
238
      let rec aux other = function
	| (l1,_) :: rem when l1 < l -> aux true rem
239
	| (l1,vl) :: rem when l1 == l ->
240
	    run_disp_record1 v other vl rem d
241
	| rem -> 
242
	    run_disp_record1 v other Absent rem d
243
      in
244
      aux other fields
245
  | Some (RecNolabel (some,none)) ->
246
      let other = other || (fields != []) in
247
248
249
250
251
      let r = if other then some else none in
      match r with
	| Some r -> make_result_basic v r
	| None -> assert false
      
252
and run_disp_record1 v other v1 rem = function
253
254
  | Impossible -> assert false
  | TailCall d1 -> run_dispatcher d1 v1
255
  | Ignore d2 ->  run_disp_record2 v other v1 rem d2
256
  | Dispatch (d1,b1) ->
257
      let code1 = run_dispatcher d1 v1 in
258
      run_disp_record2 v other v1 rem b1.(code1)
259

260
and run_disp_record2 v other v1 rem = function
261
  | Impossible -> assert false
262
  | Ignore r -> make_result_prod v1 Absent v r
263
  | TailCall d2 -> run_disp_record_loop v other rem d2
264
  | Dispatch (d2,b2) ->
265
      let code2 = run_disp_record_loop v other rem d2 in
266
      make_result_prod v1 Absent v b2.(code2)
267

268
and run_disp_record_loop v other rem d =
269
  match actions d with
270
271
    | AIgnore r -> make_result_basic v r
    | AKind k -> run_disp_record other v rem k.record
272
  
273

274
and run_disp_string_latin1 i j s q actions = 
275
  if i == j then run_disp_kind actions q 
276
277
  else match actions.prod with
    | Impossible -> assert false
278
    | TailCall d1 -> run_disp_string_latin1_char d1 (Chars.V.mk_char s.[i])
279
    | Ignore d2 -> run_disp_string_latin1_2 i j s q d2
280
    | Dispatch (d1,b1) ->
281
	let code1 = run_disp_string_latin1_char d1 (Chars.V.mk_char s.[i]) in
282
	run_disp_string_latin1_2 i j s q b1.(code1)
283
284
285
286
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) 
287
and run_disp_string_latin1_2 i j s q = function
288
289
  | Impossible -> assert false
  | Ignore r -> 
290
      make_result_string_latin1 i j s q r
291
292
293
  | 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
294
      make_result_string_latin1 i j s q b2.(code2)
295
and run_disp_string_latin1_loop i j s q d =
296
297
  let i = succ i in
  if i == j then run_dispatcher d q else
298
  match actions d with
299
300
    | AIgnore r -> make_result_basic (Value.String_latin1 (i,j,s,q)) r
    | AKind k -> run_disp_string_latin1 i j s q k
301
302

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

let run_dispatcher d v =
  let code = run_dispatcher d v in
333
334
  cursor := 0;
  (code,!buffer) 
335

336
337
338
339
340
341
342
(*
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)
343

344
345
346
347
348
349

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


*)