File: pa_bitstring.ml

package info (click to toggle)
ocaml-bitstring 2.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,516 kB
  • ctags: 546
  • sloc: ml: 3,470; sh: 343; makefile: 335; ansic: 114
file content (1196 lines) | stat: -rw-r--r-- 40,276 bytes parent folder | download
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
(* Bitstring syntax extension.
 * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
 *
 * This library 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; either
 * version 2 of the License, or (at your option) any later version,
 * with the OCaml linking exception described in COPYING.LIB.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * $Id: pa_bitstring.ml 189 2012-01-17 13:02:18Z richard.wm.jones@gmail.com $
 *)

open Printf

open Camlp4.PreCast
open Syntax
open Ast

open Bitstring
module P = Bitstring_persistent

(* If this is true then we emit some debugging code which can
 * be useful to tell what is happening during matches.  You
 * also need to do 'Bitstring.debug := true' in your main program.
 *
 * If this is false then no extra debugging code is emitted.
 *)
let debug = false

(* Hashtable storing named persistent patterns. *)
let pattern_hash : (string, P.pattern) Hashtbl.t = Hashtbl.create 13

let locfail _loc msg = Loc.raise _loc (Failure msg)

(* Work out if an expression is an integer constant.
 *
 * Returns [Some i] if so (where i is the integer value), else [None].
 *
 * Fairly simplistic algorithm: we can only detect simple constant
 * expressions such as [k], [k+c], [k-c] etc.
 *)
let rec expr_is_constant = function
  | <:expr< $int:i$ >> ->	       (* Literal integer constant. *)
    Some (int_of_string i)
  | <:expr< $lid:op$ $a$ $b$ >> ->
    (match expr_is_constant a, expr_is_constant b with
     | Some a, Some b ->	       (* Integer binary operations. *)
         let ops = ["+", (+); "-", (-); "*", ( * ); "/", (/);
		    (* NB: explicit fun .. -> is necessary here to work
		     * around a camlp4 bug in OCaml 3.10.0.
		     *)
                    "land", (fun a b -> a land b);
		    "lor", (fun a b -> a lor b);
		    "lxor", (fun a b -> a lxor b);
                    "lsl", (fun a b -> a lsl b);
		    "lsr", (fun a b -> a lsr b);
		    "asr", (fun a b -> a asr b);
		    "mod", (fun a b -> a mod b)] in
         (try Some ((List.assoc op ops) a b) with Not_found -> None)
     | _ -> None)
  | _ -> None

(* Generate a fresh, unique symbol each time called. *)
let gensym =
  let i = ref 1000 in
  fun name ->
    incr i; let i = !i in
    sprintf "__pabitstring_%s_%d" name i

(* Used to keep track of which qualifiers we've seen in parse_field. *)
type whatset_t = {
  endian_set : bool; signed_set : bool; type_set : bool;
  offset_set : bool; check_set : bool; bind_set : bool;
  save_offset_to_set : bool;
}
let noneset = {
  endian_set = false; signed_set = false; type_set = false;
  offset_set = false; check_set = false; bind_set = false;
  save_offset_to_set = false
}

(* Deal with the qualifiers which appear for a field of both types. *)
let parse_field _loc field qs =
  let fail = locfail _loc in

  let whatset, field =
    match qs with
    | None -> noneset, field
    | Some qs ->
        let check already_set msg = if already_set then fail msg in
        let apply_qualifier (whatset, field) =
	  function
          | "endian", Some expr ->
              check whatset.endian_set "an endian flag has been set already";
              let field = P.set_endian_expr field expr in
	      { whatset with endian_set = true }, field
          | "endian", None ->
	      fail "qualifier 'endian' should be followed by an expression"
          | "offset", Some expr ->
              check whatset.offset_set "an offset has been set already";
              let field = P.set_offset field expr in
	      { whatset with offset_set = true }, field
          | "offset", None ->
	      fail "qualifier 'offset' should be followed by an expression"
	  | "check", Some expr ->
	      check whatset.check_set "a check-qualifier has been set already";
	      let field = P.set_check field expr in
	      { whatset with check_set = true }, field
	  | "check", None ->
	      fail "qualifier 'check' should be followed by an expression"
	  | "bind", Some expr ->
	      check whatset.bind_set "a bind expression has been set already";
	      let field = P.set_bind field expr in
	      { whatset with bind_set = true }, field
	  | "bind", None ->
	      fail "qualifier 'bind' should be followed by an expression"
	  | "save_offset_to", Some expr (* XXX should be a pattern *) ->
	      check whatset.save_offset_to_set
		"a save_offset_to-qualifier has been set already";
	      let id =
		match expr with
		| <:expr< $lid:id$ >> -> id
		| _ ->
		    failwith "pa_bitstring: internal error: save_offset_to only supports simple identifiers at the moment.  In future we should support full patterns." in
	      let field = P.set_save_offset_to_lident field id in
	      { whatset with save_offset_to_set = true }, field
	  | "save_offset_to", None ->
	      fail "qualifier 'save_offset_to' should be followed by a binding expression"
          | s, Some _ ->
	      fail (s ^ ": unknown qualifier, or qualifier should not be followed by an expression")
          | qual, None ->
              let endian_quals = ["bigendian", BigEndian;
                                  "littleendian", LittleEndian;
                                  "nativeendian", NativeEndian] in
              let sign_quals = ["signed", true; "unsigned", false] in
              let type_quals = ["int", P.set_type_int;
				"string", P.set_type_string;
				"bitstring", P.set_type_bitstring] in
              if List.mem_assoc qual endian_quals then (
		check whatset.endian_set "an endian flag has been set already";
		let field = P.set_endian field (List.assoc qual endian_quals) in
		{ whatset with endian_set = true }, field
              ) else if List.mem_assoc qual sign_quals then (
		check whatset.signed_set "a signed flag has been set already";
		let field = P.set_signed field (List.assoc qual sign_quals) in
		{ whatset with signed_set = true }, field
              ) else if List.mem_assoc qual type_quals then (
		check whatset.type_set "a type flag has been set already";
		let field = (List.assoc qual type_quals) field in
		{ whatset with type_set = true }, field
              ) else
		fail (qual ^ ": unknown qualifier, or qualifier should be followed by an expression") in
        List.fold_left apply_qualifier (noneset, field) qs in

  (* If type is set to string or bitstring then endianness and
   * signedness qualifiers are meaningless and must not be set.
   *)
  let () =
    let t = P.get_type field in
    if (t = P.Bitstring || t = P.String) &&
      (whatset.endian_set || whatset.signed_set) then
	fail "string types and endian or signed qualifiers cannot be mixed" in

  (* Default endianness, signedness, type if not set already. *)
  let field =
    if whatset.endian_set then field else P.set_endian field BigEndian in
  let field =
    if whatset.signed_set then field else P.set_signed field false in
  let field =
    if whatset.type_set then field else P.set_type_int field in

  field

type functype = ExtractFunc | ConstructFunc

(* Choose the right constructor function. *)
let build_bitstring_call _loc functype length endian signed =
  match functype, length, endian, signed with
    (* XXX The meaning of signed/unsigned breaks down at
     * 31, 32, 63 and 64 bits.
     *)
  | (ExtractFunc, Some 1, _, _) -> <:expr< Bitstring.extract_bit >>
  | (ConstructFunc, Some 1, _, _) -> <:expr< Bitstring.construct_bit >>
  | (functype, Some (2|3|4|5|6|7|8), _, signed) ->
      let funcname = match functype with
	| ExtractFunc -> "extract"
	| ConstructFunc -> "construct" in
      let sign = if signed then "signed" else "unsigned" in
      let call = sprintf "%s_char_%s" funcname sign in
      <:expr< Bitstring.$lid:call$ >>
  | (functype, len, endian, signed) ->
      let funcname = match functype with
	| ExtractFunc -> "extract"
	| ConstructFunc -> "construct" in
      let t = match len with
	| Some i when i <= 31 -> "int"
	| Some 32 -> "int32"
	| _ -> "int64" in
      let sign = if signed then "signed" else "unsigned" in
      match endian with
      | P.ConstantEndian constant ->
          let endianness = match constant with
          | BigEndian -> "be"
          | LittleEndian -> "le"
          | NativeEndian -> "ne" in
          let call = sprintf "%s_%s_%s_%s" funcname t endianness sign in
          <:expr< Bitstring.$lid:call$ >>
      | P.EndianExpr expr ->
          let call = sprintf "%s_%s_%s_%s" funcname t "ee" sign in
          <:expr< Bitstring.$lid:call$ $expr$ >>

(* Generate the code for a constructor, ie. 'BITSTRING ...'. *)
let output_constructor _loc fields =
  (* This function makes code to raise a Bitstring.Construct_failure exception
   * containing a message and the current _loc context.
   * (Thanks to Bluestorm for suggesting this).
   *)
  let construct_failure _loc msg =
    <:expr<
      Bitstring.Construct_failure
        ($`str:msg$,
	 $`str:Loc.file_name _loc$,
	 $`int:Loc.start_line _loc$,
	 $`int:Loc.start_off _loc - Loc.start_bol _loc$)
    >>
  in
  let raise_construct_failure _loc msg =
    <:expr< raise $construct_failure _loc msg$ >>
  in

  (* Bitstrings are created like the 'Buffer' module (in fact, using
   * the Buffer module), by appending snippets to a growing buffer.
   * This is reasonably efficient and avoids a lot of garbage.
   *)
  let buffer = gensym "buffer" in

  (* General exception which is raised inside the constructor functions
   * when an int expression is out of range at runtime.
   *)
  let exn = gensym "exn" in
  let exn_used = ref false in

  (* Convert each field to a simple bitstring-generating expression. *)
  let fields = List.map (
    fun field ->
      let fexpr = P.get_expr field in
      let flen = P.get_length field in
      let endian = P.get_endian field in
      let signed = P.get_signed field in
      let t = P.get_type field in
      let _loc = P.get_location field in

      let fail = locfail _loc in

      (* offset(), check(), bind(), save_offset_to() not supported in
       * constructors.
       *
       * Implementation of forward-only offsets is fairly
       * straightforward: we would need to just calculate the length of
       * padding here and add it to what has been constructed.  For
       * general offsets, including going backwards, that would require
       * a rethink in how we construct bitstrings.
       *)
      if P.get_offset field <> None then
	fail "offset expressions are not supported in BITSTRING constructors";
      if P.get_check field <> None then
	fail "check expressions are not supported in BITSTRING constructors";
      if P.get_bind field <> None then
	fail "bind expressions are not supported in BITSTRING constructors";
      if P.get_save_offset_to field <> None then
	fail "save_offset_to is not supported in BITSTRING constructors";

      (* Is flen an integer constant?  If so, what is it?  This
       * is very simple-minded and only detects simple constants.
       *)
      let flen_is_const = expr_is_constant flen in

      let int_construct_const (i, endian, signed) =
        build_bitstring_call _loc ConstructFunc (Some i) endian signed in
      let int_construct (endian, signed) =
	build_bitstring_call _loc ConstructFunc None endian signed in

      let expr =
	match t, flen_is_const with
	(* Common case: int field, constant flen.
	 *
	 * Range checks are done inside the construction function
	 * because that's a lot simpler w.r.t. types.  It might
	 * be better to move them here. XXX
	 *)
	| P.Int, Some i when i > 0 && i <= 64 ->
	    let construct_fn = int_construct_const (i,endian,signed) in
	    exn_used := true;

	    <:expr<
	      $construct_fn$ $lid:buffer$ $fexpr$ $`int:i$ $lid:exn$
	    >>

	| P.Int, Some _ ->
	    fail "length of int field must be [1..64]"

	(* Int field, non-constant length.  We need to perform a runtime
	 * test to ensure the length is [1..64].
	 *
	 * Range checks are done inside the construction function
	 * because that's a lot simpler w.r.t. types.  It might
	 * be better to move them here. XXX
	 *)
	| P.Int, None ->
	    let construct_fn = int_construct (endian,signed) in
	    exn_used := true;

	    <:expr<
	      if $flen$ >= 1 && $flen$ <= 64 then
		$construct_fn$ $lid:buffer$ $fexpr$ $flen$ $lid:exn$
	      else
		$raise_construct_failure _loc "length of int field must be [1..64]"$
	    >>

        (* String, constant length > 0, must be a multiple of 8. *)
	| P.String, Some i when i > 0 && i land 7 = 0 ->
	    let bs = gensym "bs" in
	    let j = i lsr 3 in
	    <:expr<
	      let $lid:bs$ = $fexpr$ in
	      if String.length $lid:bs$ = $`int:j$ then
		Bitstring.construct_string $lid:buffer$ $lid:bs$
	      else
		$raise_construct_failure _loc "length of string does not match declaration"$
	    >>

	(* String, constant length -1, means variable length string
	 * with no checks.
	 *)
	| P.String, Some (-1) ->
	    <:expr< Bitstring.construct_string $lid:buffer$ $fexpr$ >>

	(* String, constant length = 0 is probably an error, and so is
	 * any other value.
	 *)
	| P.String, Some _ ->
	    fail "length of string must be > 0 and a multiple of 8, or the special value -1"

	(* String, non-constant length.
	 * We check at runtime that the length is > 0, a multiple of 8,
	 * and matches the declared length.
	 *)
	| P.String, None ->
	    let bslen = gensym "bslen" in
	    let bs = gensym "bs" in
	    <:expr<
	      let $lid:bslen$ = $flen$ in
	      if $lid:bslen$ > 0 then (
		if $lid:bslen$ land 7 = 0 then (
		  let $lid:bs$ = $fexpr$ in
		  if String.length $lid:bs$ = ($lid:bslen$ lsr 3) then
		    Bitstring.construct_string $lid:buffer$ $lid:bs$
		  else
		    $raise_construct_failure _loc "length of string does not match declaration"$
		) else
		  $raise_construct_failure _loc "length of string must be a multiple of 8"$
	      ) else
		$raise_construct_failure _loc "length of string must be > 0"$
	    >>

        (* Bitstring, constant length >= 0. *)
	| P.Bitstring, Some i when i >= 0 ->
	    let bs = gensym "bs" in
	    <:expr<
	      let $lid:bs$ = $fexpr$ in
	      if Bitstring.bitstring_length $lid:bs$ = $`int:i$ then
		Bitstring.construct_bitstring $lid:buffer$ $lid:bs$
	      else
		$raise_construct_failure _loc "length of bitstring does not match declaration"$
	    >>

	(* Bitstring, constant length -1, means variable length bitstring
	 * with no checks.
	 *)
	| P.Bitstring, Some (-1) ->
	    <:expr< Bitstring.construct_bitstring $lid:buffer$ $fexpr$ >>

	(* Bitstring, constant length < -1 is an error. *)
	| P.Bitstring, Some _ ->
	    fail "length of bitstring must be >= 0 or the special value -1"

	(* Bitstring, non-constant length.
	 * We check at runtime that the length is >= 0 and matches
	 * the declared length.
	 *)
	| P.Bitstring, None ->
	    let bslen = gensym "bslen" in
	    let bs = gensym "bs" in
	    <:expr<
	      let $lid:bslen$ = $flen$ in
	      if $lid:bslen$ >= 0 then (
		let $lid:bs$ = $fexpr$ in
		if Bitstring.bitstring_length $lid:bs$ = $lid:bslen$ then
		  Bitstring.construct_bitstring $lid:buffer$ $lid:bs$
		else
		  $raise_construct_failure _loc "length of bitstring does not match declaration"$
	      ) else
		$raise_construct_failure _loc "length of bitstring must be > 0"$
	    >> in
      expr
  ) fields in

  (* Create the final bitstring.  Start by creating an empty buffer
   * and then evaluate each expression above in turn which will
   * append some more to the bitstring buffer.  Finally extract
   * the bitstring.
   *
   * XXX We almost have enough information to be able to guess
   * a good initial size for the buffer.
   *)
  let fields =
    match fields with
    | [] -> <:expr< [] >>
    | h::t -> List.fold_left (fun h t -> <:expr< $h$; $t$ >>) h t in

  let expr =
    <:expr<
      let $lid:buffer$ = Bitstring.Buffer.create () in
      $fields$;
      Bitstring.Buffer.contents $lid:buffer$
    >> in

  if !exn_used then
    <:expr<
      let $lid:exn$ = $construct_failure _loc "value out of range"$ in
      $expr$
    >>
  else
    expr

(* Generate the code for a bitmatch statement.  '_loc' is the
 * location, 'bs' is the bitstring parameter, 'cases' are
 * the list of cases to test against.
 *)
let output_bitmatch _loc bs cases =
  (* These symbols are used through the generated code to record our
   * current position within the bitstring:
   *
   *   data - original bitstring data (string, never changes)
   *   off  - current offset within data (int, increments as we move through
   *            the bitstring)
   *   len  - current remaining length within data (int, decrements as
   *            we move through the bitstring)
   *
   * Also:
   *
   *   original_off - saved offset at the start of the match (never changes)
   *   original_len - saved length at the start of the match (never changes)
   *   off_aligned  - true if the original offset is byte-aligned (allows
   *            us to make some common optimizations)
   *)
  let data = gensym "data"
  and off = gensym "off"
  and len = gensym "len"
  and original_off = gensym "original_off"
  and original_len = gensym "original_len"
  and off_aligned = gensym "off_aligned"

  (* This is where the result will be stored (a reference). *)
  and result = gensym "result" in

  (* This generates the field extraction code for each
   * field in a single case.  There must be enough remaining data
   * in the bitstring to satisfy the field.
   *
   * As we go through the fields, symbols 'data', 'off' and 'len'
   * track our position and remaining length in the bitstring.
   *
   * The whole thing is a lot of nested 'if'/'match' statements.
   * Code is generated from the inner-most (last) field outwards.
   *)
  let rec output_field_extraction inner = function
    | [] -> inner
    | field :: fields ->
	let fpatt = P.get_patt field in
	let flen = P.get_length field in
	let endian = P.get_endian field in
	let signed = P.get_signed field in
	let t = P.get_type field in
	let _loc = P.get_location field in

	let fail = locfail _loc in

	(* Is flen (field len) an integer constant?  If so, what is it?
	 * This will be [Some i] if it's a constant or [None] if it's
	 * non-constant or we couldn't determine.
	 *)
	let flen_is_const = expr_is_constant flen in

	(* Surround the inner expression by check and bind clauses, so:
	 *   if $check$ then
	 *     let $bind...$ in
	 *       $inner$
	 * where the check and bind are switched on only if they are
	 * present in the field.  (In the common case when neither
	 * clause is present, expr = inner).  Note the order of the
	 * check & bind is visible to the user and defined in the
	 * documentation, so it must not change.
	 *)
	let expr = inner in
	let expr =
	  match P.get_bind field with
	  | None -> expr
	  | Some bind_expr ->
	      <:expr< let $fpatt$ = $bind_expr$ in $expr$ >> in
	let expr =
	  match P.get_check field with
	  | None -> expr
	  | Some check_expr ->
	      <:expr< if $check_expr$ then $expr$ >> in

	(* Compute the offset of this field within the match, if it
	 * can be known at compile time.
	 *
	 * Actually, we'll compute two things: the 'natural_field_offset'
	 * is the offset assuming this field had no offset() qualifier
	 * (in other words, its position, immediately following the
	 * preceding field).  'field_offset' is the real field offset
	 * taking into account any offset() qualifier.
	 *
	 * This will be [Some i] if our current offset is known
	 * at compile time, or [None] if we can't determine it.
	 *)
	let natural_field_offset, field_offset =
	  let has_constant_offset field =
	    match P.get_offset field with
	    | None -> false
	    | Some expr ->
		match expr_is_constant expr with
		| None -> false
		| Some i -> true
	  in
	  let get_constant_offset field =
	    match P.get_offset field with
	    | None -> assert false
	    | Some expr ->
		match expr_is_constant expr with
		| None -> assert false
		| Some i -> i
	  in

	  let has_constant_len field =
	    match expr_is_constant (P.get_length field) with
	    | None -> false
	    | Some i when i > 0 -> true
	    | Some _ -> false
	  in
	  let get_constant_len field =
	    match expr_is_constant (P.get_length field) with
	    | None -> assert false
	    | Some i when i > 0 -> i
	    | Some _ -> assert false
	  in

	  (* NB: We are looping over the PRECEDING fields in reverse order. *)
	  let rec loop = function
	    (* first field has constant offset 0 *)
	    | [] -> Some 0
	    (* preceding field with constant offset & length *)
	    | f :: _
		when has_constant_offset f && has_constant_len f ->
		Some (get_constant_offset f + get_constant_len f)
	    (* preceding field with no offset & constant length *)
	    | f :: fs
		when P.get_offset f = None && has_constant_len f ->
		(match loop fs with
		 | None -> None
		 | Some offset -> Some (offset + get_constant_len f))
	    (* else, can't work out the offset *)
	    | _ -> None
	  in

	  let natural_field_offset = loop fields in

	  let field_offset =
	    match P.get_offset field with
	    | None -> natural_field_offset
	    | Some expr -> (* has an offset() clause *)
		match expr_is_constant expr with
		| None -> None
		| i -> i in

	  natural_field_offset, field_offset in

	(* Also compute if the field_offset is known to be byte-aligned at
	 * compile time, which is usually both the common and best possible
	 * case for generating optimized code.
	 *
	 * This is None if not aligned / don't know.
	 * Or Some byte_offset if we can work it out.
	 *)
	let field_offset_aligned =
	  match field_offset with
	  | None -> None		(* unknown, assume no *)
	  | Some off when off land 7 = 0 -> Some (off lsr 3)
	  | Some _ -> None in		(* definitely no *)

	(* Now build the code which matches a single field. *)
	let int_extract_const i endian signed =
          build_bitstring_call _loc ExtractFunc (Some i) endian signed in
	let int_extract endian signed =
	  build_bitstring_call _loc ExtractFunc None endian signed in

	let expr =
	  match t, flen_is_const, field_offset_aligned, endian, signed with
	    (* Very common cases: int field, constant 8/16/32/64 bit
	     * length, aligned to the match at a known offset.  We
	     * still have to check if the bitstring is aligned (can only
	     * be known at runtime) but we may be able to directly access
	     * the bytes in the string.
	     *)
	  | P.Int, Some 8, Some field_byte_offset, _, _ ->
	      let extract_fn = int_extract_const 8 endian signed in

              (* The fast-path code when everything is aligned. *)
              let fastpath =
		<:expr<
                  let o =
		    ($lid:original_off$ lsr 3) + $`int:field_byte_offset$ in
                  Char.code (String.unsafe_get $lid:data$ o)              
                >> in

              <:expr<
		if $lid:len$ >= 8 then (
                  let v =
                    if $lid:off_aligned$ then
                      $fastpath$
                    else
                      $extract_fn$ $lid:data$ $lid:off$ $lid:len$ 8 in
                  let $lid:off$ = $lid:off$ + 8
                  and $lid:len$ = $lid:len$ - 8 in
                  match v with $fpatt$ when true -> $expr$ | _ -> ()
		)                                                               
	      >>

	  | P.Int, Some ((16|32|64) as i),
	    Some field_byte_offset, (P.ConstantEndian _ as endian), signed ->
	      let extract_fn = int_extract_const i endian signed in

	      (* The fast-path code when everything is aligned. *)
	      let fastpath =
		let fastpath_call =
		  let endian = match endian with
		    | P.ConstantEndian BigEndian -> "be"
		    | P.ConstantEndian LittleEndian -> "le"
		    | P.ConstantEndian NativeEndian -> "ne"
		    | P.EndianExpr _ -> assert false in
		  let signed = if signed then "signed" else "unsigned" in
		  let name =
		    sprintf "extract_fastpath_int%d_%s_%s" i endian signed in
		  match i with
		  | 16 ->
		      <:expr< Bitstring.$lid:name$ $lid:data$ o >>
		  | 32 ->
		      <:expr<
			(* must allocate a new zero each time *)
			let zero = Int32.of_int 0 in
			Bitstring.$lid:name$ $lid:data$ o zero
		      >>
		  | 64 ->
		      <:expr<
			(* must allocate a new zero each time *)
			let zero = Int64.of_int 0 in
			Bitstring.$lid:name$ $lid:data$ o zero
		      >>
		  | _ -> assert false in
		<:expr<
		  (* Starting offset within the string. *)
		  let o =
		    ($lid:original_off$ lsr 3) + $`int:field_byte_offset$ in
		  $fastpath_call$
	        >> in

	      let slowpath =
		<:expr<
		  $extract_fn$ $lid:data$ $lid:off$ $lid:len$ $`int:i$
		>> in

	      <:expr<
		if $lid:len$ >= $`int:i$ then (
		  let v =
		    if $lid:off_aligned$ then $fastpath$ else $slowpath$ in
		  let $lid:off$ = $lid:off$ + $`int:i$
		  and $lid:len$ = $lid:len$ - $`int:i$ in
		  match v with $fpatt$ when true -> $expr$ | _ -> ()
		)
	      >>

	  (* Common case: int field, constant flen *)
	  | P.Int, Some i, _, _, _ when i > 0 && i <= 64 ->
	      let extract_fn = int_extract_const i endian signed in
	      let v = gensym "val" in
	      <:expr<
		if $lid:len$ >= $`int:i$ then (
		  let $lid:v$ =
		    $extract_fn$ $lid:data$ $lid:off$ $lid:len$ $`int:i$ in
		  let $lid:off$ = $lid:off$ + $`int:i$
		  and $lid:len$ = $lid:len$ - $`int:i$ in
		  match $lid:v$ with $fpatt$ when true -> $expr$ | _ -> ()
		)
	      >>

	  | P.Int, Some _, _, _, _ ->
	      fail "length of int field must be [1..64]"

	  (* Int field, non-const flen.  We have to test the range of
	   * the field at runtime.  If outside the range it's a no-match
	   * (not an error).
	   *)
	  | P.Int, None, _, _, _ ->
	      let extract_fn = int_extract endian signed in
	      let v = gensym "val" in
	      <:expr<
		if $flen$ >= 1 && $flen$ <= 64 && $flen$ <= $lid:len$ then (
		  let $lid:v$ =
		    $extract_fn$ $lid:data$ $lid:off$ $lid:len$ $flen$ in
		  let $lid:off$ = $lid:off$ + $flen$
		  and $lid:len$ = $lid:len$ - $flen$ in
		  match $lid:v$ with $fpatt$ when true -> $expr$ | _ -> ()
		)
	      >>

          (* String, constant flen > 0.
	   * The field is at a known byte-aligned offset so we may
	   * be able to optimize the substring extraction.
	   *)
	  | P.String, Some i, Some field_byte_offset, _, _
	      when i > 0 && i land 7 = 0 ->
	      let fastpath =
		<:expr<
		  (* Starting offset within the string. *)
		  let o =
		    ($lid:original_off$ lsr 3) + $`int:field_byte_offset$ in
		  String.sub $lid:data$ o $`int:(i lsr 3)$
		>> in

	      let slowpath =
		<:expr<
		  Bitstring.string_of_bitstring
		    ($lid:data$, $lid:off$, $`int:i$)
		>> in

	      let cond =
		<:expr<
		  if $lid:off_aligned$ then $fastpath$ else $slowpath$
		>> in

	      <:expr<
		if $lid:len$ >= $`int:i$ then (
		  let str = $cond$ in
		  let $lid:off$ = $lid:off$ + $`int:i$
		  and $lid:len$ = $lid:len$ - $`int:i$ in
		  match str with
		  | $fpatt$ when true -> $expr$
		  | _ -> ()
		)
	      >>

          (* String, constant flen > 0. *)
	  | P.String, Some i, None, _, _ when i > 0 && i land 7 = 0 ->
	      <:expr<
		if $lid:len$ >= $`int:i$ then (
		  let str =
		    Bitstring.string_of_bitstring
		      ($lid:data$, $lid:off$, $`int:i$) in
		  let $lid:off$ = $lid:off$ + $`int:i$
		  and $lid:len$ = $lid:len$ - $`int:i$ in
		  match str with
		  | $fpatt$ when true -> $expr$
		  | _ -> ()
		)
	      >>

          (* String, constant flen = -1, means consume all the
	   * rest of the input.
	   * XXX It should be possible to optimize this for known byte
	   * offset, but the optimization is tricky because the end/length
	   * of the string may not be byte-aligned.
	   *)
	  | P.String, Some i, _, _, _ when i = -1 ->
	      let str = gensym "str" in

	      <:expr<
		let $lid:str$ =
		  Bitstring.string_of_bitstring
		    ($lid:data$, $lid:off$, $lid:len$) in
		let $lid:off$ = $lid:off$ + $lid:len$ in
		let $lid:len$ = 0 in
		match $lid:str$ with
		| $fpatt$ when true -> $expr$
		| _ -> ()
	      >>

	  | P.String, Some _, _, _, _ ->
	      fail "length of string must be > 0 and a multiple of 8, or the special value -1"

	  (* String field, non-const flen.  We check the flen is > 0
	   * and a multiple of 8 (-1 is not allowed here), at runtime.
	   *)
	  | P.String, None, _, _, _ ->
	      let bs = gensym "bs" in
	      <:expr<
		if $flen$ >= 0 && $flen$ <= $lid:len$
		  && $flen$ land 7 = 0 then (
		    let $lid:bs$ = ($lid:data$, $lid:off$, $flen$) in
		    let $lid:off$ = $lid:off$ + $flen$
		    and $lid:len$ = $lid:len$ - $flen$ in
		    match Bitstring.string_of_bitstring $lid:bs$ with
		    | $fpatt$ when true -> $expr$
		    | _ -> ()
		  )
	      >>

          (* Bitstring, constant flen >= 0.
	   * At the moment all we can do is assign the bitstring to an
	   * identifier.
	   *)
	  | P.Bitstring, Some i, _, _, _ when i >= 0 ->
	      let ident =
		match fpatt with
		| <:patt< $lid:ident$ >> -> ident
		| <:patt< _ >> -> "_"
		| _ ->
		    fail "cannot compare a bitstring to a constant" in
	      <:expr<
		if $lid:len$ >= $`int:i$ then (
		  let $lid:ident$ = ($lid:data$, $lid:off$, $`int:i$) in
		  let $lid:off$ = $lid:off$ + $`int:i$
		  and $lid:len$ = $lid:len$ - $`int:i$ in
		  $expr$
		)
	      >>

          (* Bitstring, constant flen = -1, means consume all the
	   * rest of the input.
	   *)
	  | P.Bitstring, Some i, _, _, _ when i = -1 ->
	      let ident =
		match fpatt with
		| <:patt< $lid:ident$ >> -> ident
		| <:patt< _ >> -> "_"
		| _ ->
		    fail "cannot compare a bitstring to a constant" in
	      <:expr<
		let $lid:ident$ = ($lid:data$, $lid:off$, $lid:len$) in
		let $lid:off$ = $lid:off$ + $lid:len$ in
		let $lid:len$ = 0 in
		  $expr$
	      >>

	  | P.Bitstring, Some _, _, _, _ ->
	      fail "length of bitstring must be >= 0 or the special value -1"

	  (* Bitstring field, non-const flen.  We check the flen is >= 0
	   * (-1 is not allowed here) at runtime.
	   *)
	  | P.Bitstring, None, _, _, _ ->
	      let ident =
		match fpatt with
		| <:patt< $lid:ident$ >> -> ident
		| <:patt< _ >> -> "_"
		| _ ->
		    fail "cannot compare a bitstring to a constant" in
	      <:expr<
		if $flen$ >= 0 && $flen$ <= $lid:len$ then (
		  let $lid:ident$ = ($lid:data$, $lid:off$, $flen$) in
		  let $lid:off$ = $lid:off$ + $flen$
		  and $lid:len$ = $lid:len$ - $flen$ in
		  $expr$
		)
	      >>
	in

	(* Computed offset: only offsets forward are supported.
	 *
	 * We try hard to optimize this based on what we know.  Are
	 * we at a predictable offset now?  (Look at the outer 'fields'
	 * list and see if they all have constant field length starting
	 * at some constant offset).  Is this offset constant?
	 *
	 * Based on this we can do a lot of the computation at
	 * compile time, or defer it to runtime only if necessary.
	 *
	 * In all cases, the off and len fields get updated.
	 *)
	let expr =
	  match P.get_offset field with
	  | None -> expr (* common case: there was no offset expression *)
	  | Some offset_expr ->
	      (* This will be [Some i] if offset is a constant expression
	       * or [None] if it's a non-constant.
	       *)
	      let requested_offset = expr_is_constant offset_expr in

              (* Look at the field offset (if known) and requested offset
	       * cases and determine what code to generate.
	       *)
	      match natural_field_offset, requested_offset with
		(* This is the good case: both the field offset and
		 * the requested offset are constant, so we can remove
		 * almost all the runtime checks.
		 *)
	      | Some natural_field_offset, Some requested_offset ->
		  let move = requested_offset - natural_field_offset in
		  if move < 0 then
		    fail (sprintf "requested offset is less than the field offset (%d < %d)" requested_offset natural_field_offset);
		  (* Add some code to move the offset and length by a
		   * constant amount, and a runtime test that len >= 0
		   * (XXX possibly the runtime test is unnecessary?)
		   *)
		  <:expr<
		    let $lid:off$ = $lid:off$ + $`int:move$ in
		    let $lid:len$ = $lid:len$ - $`int:move$ in
		    if $lid:len$ >= 0 then $expr$
		  >>
	      (* In any other case, we need to use runtime checks.
	       *
	       * XXX It's not clear if a backwards move detected at runtime
	       * is merely a match failure, or a runtime error.  At the
	       * moment it's just a match failure since bitmatch generally
	       * doesn't raise runtime errors.
	       *)
	      | _ ->
		  let move = gensym "move" in
		  <:expr<
		    let $lid:move$ =
		      $offset_expr$ - ($lid:off$ - $lid:original_off$) in
		    if $lid:move$ >= 0 then (
		      let $lid:off$ = $lid:off$ + $lid:move$ in
		      let $lid:len$ = $lid:len$ - $lid:move$ in
		      if $lid:len$ >= 0 then $expr$
		    )
		  >> in (* end of computed offset code *)

	(* save_offset_to(patt) saves the current offset into a variable. *)
	let expr =
	  match P.get_save_offset_to field with
	  | None -> expr (* no save_offset_to *)
	  | Some patt ->
	      <:expr<
		let $patt$ = $lid:off$ - $lid:original_off$ in
		$expr$
	      >> in

	(* Emit extra debugging code. *)
	let expr =
	  if not debug then expr else (
	    let field = P.string_of_pattern_field field in

	    <:expr<
	      if !Bitstring.debug then (
		Printf.eprintf "PA_BITSTRING: TEST:\n";
		Printf.eprintf "  %s\n" $str:field$;
		Printf.eprintf "  off %d len %d\n%!" $lid:off$ $lid:len$;
		(*Bitstring.hexdump_bitstring stderr
		  ($lid:data$,$lid:off$,$lid:len$);*)
	      );
	      $expr$
	    >>
	  ) in

	output_field_extraction expr fields
  in

  (* Convert each case in the match. *)
  let cases = List.map (
    fun (fields, bind, whenclause, code) ->
      let inner = <:expr< $lid:result$ := Some ($code$); raise Exit >> in
      let inner =
	match whenclause with
	| Some whenclause ->
	    <:expr< if $whenclause$ then $inner$ >>
	| None -> inner in
      let inner =
	match bind with
	| Some name ->
	    <:expr<
	      let $lid:name$ = ($lid:data$,
                                $lid:original_off$, $lid:original_len$) in
	      $inner$
	      >>
	| None -> inner in
      output_field_extraction inner (List.rev fields)
  ) cases in

  (* Join them into a single expression.
   *
   * Don't do it with a normal fold_right because that leaves
   * 'raise Exit; ()' at the end which causes a compiler warning.
   * Hence a bit of complexity here.
   *
   * Note that the number of cases is always >= 1 so List.hd is safe.
   *)
  let cases = List.rev cases in
  let cases =
    List.fold_left (fun base case -> <:expr< $case$ ; $base$ >>)
      (List.hd cases) (List.tl cases) in

  (* The final code just wraps the list of cases in a
   * try/with construct so that each case is tried in
   * turn until one case matches (that case sets 'result'
   * and raises 'Exit' to leave the whole statement).
   * If result isn't set by the end then we will raise
   * Match_failure with the location of the bitmatch
   * statement in the original code.
   *)
  let loc_fname = Loc.file_name _loc in
  let loc_line = string_of_int (Loc.start_line _loc) in
  let loc_char = string_of_int (Loc.start_off _loc - Loc.start_bol _loc) in

  <:expr<
    (* Note we save the original offset/length at the start of the match
     * in 'original_off'/'original_len' symbols.  'data' never changes.
     * This code also ensures that if original_off/original_len/off_aligned
     * aren't actually used, we don't get a warning.
     *)
    let ($lid:data$, $lid:original_off$, $lid:original_len$) = $bs$ in
    let $lid:off$ = $lid:original_off$ and $lid:len$ = $lid:original_len$ in
    let $lid:off_aligned$ = $lid:off$ land 7 = 0 in
    ignore $lid:off_aligned$;
    let $lid:result$ = ref None in
    (try
      $cases$
    with Exit -> ());
    match ! $lid:result$ with
    | Some x -> x
    | None -> raise (Match_failure ($str:loc_fname$,
				    $int:loc_line$, $int:loc_char$))
  >>

(* Add a named pattern. *)
let add_named_pattern _loc name pattern =
  Hashtbl.add pattern_hash name pattern

(* Expand a named pattern from the pattern_hash. *)
let expand_named_pattern _loc name =
  try Hashtbl.find pattern_hash name
  with Not_found ->
    locfail _loc (sprintf "named pattern not found: %s" name)

(* Add named patterns from a file.  See the documentation on the
 * directory search path in bitstring_persistent.mli
 *)
let load_patterns_from_file _loc filename =
  let chan =
    if Filename.is_relative filename && Filename.is_implicit filename then (
      (* Try current directory. *)
      try open_in filename
      with _ ->
	(* Try OCaml library directory. *)
	try open_in (Filename.concat Bitstring_config.ocamllibdir filename)
	with exn -> Loc.raise _loc exn
    ) else (
      try open_in filename
      with exn -> Loc.raise _loc exn
    ) in
  let names = ref [] in
  (try
     let rec loop () =
       let name = P.named_from_channel chan in
       names := name :: !names
     in
     loop ()
   with End_of_file -> ()
  );
  close_in chan;
  let names = List.rev !names in
  List.iter (
    function
    | name, P.Pattern patt ->
	if patt = [] then
	  locfail _loc (sprintf "pattern %s: no fields" name);
	add_named_pattern _loc name patt
    | _, P.Constructor _ -> () (* just ignore these for now *)
  ) names

EXTEND Gram
  GLOBAL: expr str_item;

  (* Qualifiers are a list of identifiers ("string", "bigendian", etc.)
   * followed by an optional expression (used in certain cases).  Note
   * that we are careful not to declare any explicit reserved words.
   *)
  qualifiers: [
    [ LIST0
	[ q = LIDENT;
	  e = OPT [ "("; e = expr; ")" -> e ] -> (q, e) ]
	SEP "," ]
  ];

  (* Field used in the bitmatch operator (a pattern).  This can actually
   * return multiple fields, in the case where the 'field' is a named
   * persitent pattern.
   *)
  patt_field: [
    [ fpatt = patt; ":"; len = expr LEVEL "top";
      qs = OPT [ ":"; qs = qualifiers -> qs ] ->
	let field = P.create_pattern_field _loc in
	let field = P.set_patt field fpatt in
	let field = P.set_length field len in
	[parse_field _loc field qs]	(* Normal, single field. *)
    | ":"; name = LIDENT ->
	expand_named_pattern _loc name (* Named -> list of fields. *)
    ]
  ];

  (* Case inside bitmatch operator. *)
  patt_fields: [
    [ "{";
      fields = LIST0 patt_field SEP ";";
      "}" ->
	List.concat fields
    | "{";
      "_";
      "}" ->
	[]
    ]
  ];

  patt_case: [
    [ fields = patt_fields;
      bind = OPT [ "as"; name = LIDENT -> name ];
      whenclause = OPT [ "when"; e = expr -> e ]; "->";
      code = expr ->
	(fields, bind, whenclause, code)
    ]
  ];

  (* Field used in the BITSTRING constructor (an expression). *)
  constr_field: [
    [ fexpr = expr LEVEL "top"; ":"; len = expr LEVEL "top";
      qs = OPT [ ":"; qs = qualifiers -> qs ] ->
	let field = P.create_constructor_field _loc in
	let field = P.set_expr field fexpr in
	let field = P.set_length field len in
	parse_field _loc field qs
    ]
  ];

  constr_fields: [
    [ "{";
      fields = LIST0 constr_field SEP ";";
      "}" ->
	fields
    ]
  ];

  (* 'bitmatch' expressions. *)
  expr: LEVEL ";" [
    [ "bitmatch";
      bs = expr; "with"; OPT "|";
      cases = LIST1 patt_case SEP "|" ->
	output_bitmatch _loc bs cases
    ]

  (* Constructor. *)
  | [ "BITSTRING";
      fields = constr_fields ->
	output_constructor _loc fields
    ]
  ];

  (* Named persistent patterns.
   *
   * NB: Currently only allowed at the top level.  We can probably lift
   * this restriction later if necessary.  We only deal with patterns
   * at the moment, not constructors, but the infrastructure to do
   * constructors is in place.
   *)
  str_item: LEVEL "top" [
    [ "let"; "bitmatch";
      name = LIDENT; "="; fields = patt_fields ->
	add_named_pattern _loc name fields;
        (* The statement disappears, but we still need a str_item so ... *)
        <:str_item< >>
    | "open"; "bitmatch"; filename = STRING ->
	load_patterns_from_file _loc filename;
	<:str_item< >>
    ]
  ];

END