/
pa_bitstring.ml
1188 lines (1087 loc) · 39.1 KB
/
pa_bitstring.ml
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
(* 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$
*)
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, _, signed ->
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
Bitstring.char_code (String.unsafe_get $lid:data$ o)
>> in
<:expr<
if $lid:len$ >= 8 then (
let v =
if not $`bool:signed$ && $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< Bitstring.$lid:name$ $lid:data$ o >>
| 64 ->
<:expr< Bitstring.$lid:name$ $lid:data$ o >>
| _ -> 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