Source file ppx_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
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
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
open Migrate_parsetree
open Ast_405
open Ast_convenience_405
open Ast_mapper
open Asttypes
open Parsetree
open Lexing
open Printf
let ocaml_version = Versions.ocaml_405
module Entity = struct
type t = {
txt : string;
exp : Parsetree.expression;
pat : Parsetree.pattern
}
let mksym =
let i = ref 1000 in
fun name ->
incr i; let i = !i in
sprintf "__ppxbitstring_%s_%d" name i
;;
let make ~loc v =
let txt = mksym v in
{ txt; exp = evar ~loc txt; pat = pvar ~loc txt }
end
module Context = struct
type t = {
dat : Entity.t;
off : Entity.t;
len : Entity.t
}
let make ~loc =
let dat = Entity.make ~loc "dat"
and off = Entity.make ~loc "off"
and len = Entity.make ~loc "len"
in
{ dat; off; len }
let next ~loc t =
let off = Entity.make ~loc "off"
and len = Entity.make ~loc "len"
in
{ t with off; len }
end
module Type = struct
type t =
| Int
| String
| Bitstring
end
module Sign = struct
type t =
| Signed
| Unsigned
let to_string = function
| Signed -> "signed"
| Unsigned -> "unsigned"
end
module Endian = struct
type t =
| Little
| Big
| Native
| Referred of Parsetree.expression
let to_string = function
| Little -> "le"
| Big -> "be"
| Native -> "ne"
| Referred _ -> "ee"
end
module Qualifiers = struct
type t = {
value_type : Type.t option;
sign : Sign.t option;
endian : Endian.t option;
check : Parsetree.expression option;
bind : Parsetree.expression option;
map : Parsetree.expression option;
save_offset_to : Parsetree.expression option;
offset : Parsetree.expression option;
}
let empty = {
value_type = None;
sign = None;
endian = None;
check = None;
bind = None;
map = None;
save_offset_to = None;
offset = None;
}
let default = {
value_type = Some Type.Int;
sign = Some Sign.Unsigned;
endian = Some Endian.Big;
check = None;
bind = None;
map = None;
save_offset_to = None;
offset = None;
}
let set_value_type_default q =
match q.value_type with
| None -> { q with value_type = Some Type.Int }
| _ -> q
;;
let set_sign_default q =
match q.sign with
| None -> { q with sign = Some Sign.Unsigned }
| _ -> q
;;
let set_endian_default q =
match q.endian with
| None -> { q with endian = Some Endian.Big }
| _ -> q
;;
let set_defaults v =
v
|> set_value_type_default
|> set_sign_default
|> set_endian_default
;;
end
module MatchField = struct
type bitlen =
(Parsetree.expression * int option)
;;
type tuple = {
pat : Parsetree.pattern;
len : bitlen;
qls : Qualifiers.t;
opt : bool
}
type t =
| Any of Parsetree.pattern
| Tuple of tuple
;;
end
let location_exn ~loc msg =
Location.Error (Location.error ~loc msg)
|> raise
;;
let split_string ~on s =
Str.split (Str.regexp on) s
;;
let option_bind opt f =
match opt with
| None -> None
| Some v -> f v
;;
let rec process_expr_loc ~loc expr =
match expr with
| { pexp_desc = Pexp_ident(ident); _ } ->
let lident = Location.mkloc ident.txt loc in
{ expr with pexp_desc = Pexp_ident(lident); pexp_loc = loc }
| { pexp_desc = Pexp_tuple(ops); _ } ->
let fld = List.fold_left
(fun acc exp -> acc @ [ process_expr_loc ~loc exp ])
[]
ops
in { expr with pexp_desc = Pexp_tuple(fld); pexp_loc = loc }
| { pexp_desc = Pexp_construct(ident, ops); _ } ->
let lident = Location.mkloc ident.txt loc in
let lops = begin match ops with
| Some o -> Some (process_expr_loc ~loc o)
| None -> None
end in
{ expr with pexp_desc = Pexp_construct(lident, lops); pexp_loc = loc }
| { pexp_desc = Pexp_apply(ident, ops); _ } ->
let lident = process_expr_loc ~loc ident in
let fld = List.fold_left
(fun acc (lbl, exp) -> acc @ [ (lbl, (process_expr_loc ~loc exp)) ])
[]
ops
in { expr with pexp_desc = Pexp_apply(lident, fld); pexp_loc = loc }
| { pexp_desc = Pexp_fun(ident, ops,
{ ppat_desc = Ppat_var(pid); ppat_loc; ppat_attributes },
exp); _ } ->
let lpid = Location.mkloc pid.txt loc in
let lpat = { ppat_desc = Ppat_var lpid; ppat_loc = loc; ppat_attributes } in
let lops = begin match ops with
| Some o -> Some (process_expr_loc ~loc o)
| None -> None
end in
let lexp = process_expr_loc ~loc exp in
{ expr with pexp_desc = Pexp_fun(ident, lops, lpat, lexp); pexp_loc = loc }
| _ ->
{ expr with pexp_loc = loc }
;;
let parse_expr expr =
try
Parse.expression Versions.ocaml_405 (Lexing.from_string expr.txt)
|> process_expr_loc ~loc:expr.loc
with
_ -> location_exn ~loc:expr.loc ("Parse expression error: '" ^ expr.txt ^ "'")
;;
let process_pat_loc ~loc pat =
match pat with
| { ppat_desc = Ppat_var(ident); ppat_loc; ppat_attributes; _ } ->
let lident = Location.mkloc ident.txt loc in
{ ppat_desc = Ppat_var(lident); ppat_loc = loc; ppat_attributes }
| _ ->
{ pat with ppat_loc = loc }
;;
let parse_pattern pat =
try
Parse.pattern Versions.ocaml_405 (Lexing.from_string pat.txt)
|> process_pat_loc ~loc:pat.loc
with
_ -> location_exn ~loc:pat.loc ("Parse pattern error: '" ^ pat.txt ^ "'")
;;
let find_loc_boundaries ~loc last rem =
let open Location in
let { loc_start; loc_end; loc_ghost } = loc in
let xtr_lines = List.length rem in
let xtr_char = List.fold_left (+) xtr_lines rem in
let ne = { loc_start with
pos_lnum = loc_start.pos_lnum + xtr_lines;
pos_bol = loc_start.pos_bol + xtr_char;
pos_cnum = loc_start.pos_cnum + xtr_char + last
}
and ns = if xtr_lines = 0
then { loc_start with
pos_cnum = loc_start.pos_cnum + xtr_char + last + 1
}
else { loc_start with
pos_lnum = loc_start.pos_lnum + xtr_lines;
pos_bol = loc_start.pos_bol + xtr_char;
pos_cnum = loc_start.pos_cnum + xtr_char
} in
let tloc = { loc_start; loc_end = ne; loc_ghost } in
let nloc = { loc_start = ns; loc_end; loc_ghost } in
(tloc, nloc)
;;
let rec split_loc_rec ~loc = function
| [] -> []
| hd :: tl ->
let line_list = split_string ~on:"\n" hd
|> List.rev
|> List.map String.length in
begin
match line_list with
| [] -> []
| last::rem ->
let (tloc, nloc) = find_loc_boundaries ~loc last rem in
[ tloc ] @ (split_loc_rec ~loc:nloc tl)
end
;;
let split_loc ~loc lst =
split_loc_rec ~loc lst
|> List.map2 (fun e loc -> Location.mkloc (String.trim e) loc) lst
;;
let check_map_functor sub =
match sub with
| [%expr (fun [%p? _] -> [%e? _])] -> Some (sub)
| _ -> None
;;
let process_qual state qual =
let open Qualifiers in
let loc = qual.pexp_loc in
match qual with
| [%expr int] ->
begin match state.value_type with
| Some v -> location_exn ~loc "Value type redefined"
| None -> { state with value_type = Some Type.Int }
end
| [%expr string] ->
begin match state.value_type with
| Some v -> location_exn ~loc "Value type redefined"
| None -> { state with value_type = Some Type.String }
end
| [%expr bitstring] ->
begin match state.value_type with
| Some v -> location_exn ~loc "Value type redefined"
| None -> { state with value_type = Some Type.Bitstring }
end
| [%expr signed] ->
begin match state.sign with
| Some v -> location_exn ~loc "Signedness redefined"
| None -> { state with sign = Some Sign.Signed }
end
| [%expr unsigned] ->
begin match state.sign with
| Some v -> location_exn ~loc "Signedness redefined"
| None -> { state with sign = Some Sign.Unsigned }
end
| [%expr littleendian] ->
begin match state.endian with
| Some v -> location_exn ~loc "Endianness redefined"
| None -> { state with endian = Some Endian.Little }
end
| [%expr bigendian] ->
begin match state.endian with
| Some v -> location_exn ~loc "Endianness redefined"
| None -> { state with endian = Some Endian.Big }
end
| [%expr nativeendian] ->
begin match state.endian with
| Some v -> location_exn ~loc "Endianness redefined"
| None -> { state with endian = Some Endian.Native }
end
| [%expr endian [%e? sub]] ->
begin match state.endian with
| Some v -> location_exn ~loc "Endianness redefined"
| None -> { state with endian = Some (Endian.Referred sub) }
end
| [%expr bind [%e? sub]] ->
begin match state.bind, state.map with
| Some b, None -> location_exn ~loc "Bind expression redefined"
| None, Some m -> location_exn ~loc "Map expression already defined"
| Some b, Some m -> location_exn ~loc "Inconsistent internal state"
| None, None -> { state with bind = Some sub }
end
| [%expr map [%e? sub]] ->
begin match state.bind, state.map with
| Some b, None -> location_exn ~loc "Bind expression already defined"
| None, Some m -> location_exn ~loc "Map expression redefined"
| Some b, Some m -> location_exn ~loc "Inconsistent internal state"
| None, None -> begin
match check_map_functor sub with
| Some sub -> { state with map = Some sub }
| None -> location_exn ~loc "Invalid map functor"
end
end
| [%expr check [%e? sub]] ->
begin match state.check with
| Some v -> location_exn ~loc "Check expression redefined"
| None -> { state with check = Some sub }
end
| [%expr save_offset_to [%e? sub]] ->
begin match state.save_offset_to with
| Some v -> location_exn ~loc "Save offset expression redefined"
| None -> { state with save_offset_to = Some sub }
end
| [%expr offset [%e? sub]] ->
begin match state.offset with
| Some v -> location_exn ~loc "Offset expression redefined"
| None -> { state with offset = Some sub }
end
| _ ->
location_exn ~loc "Invalid qualifier"
;;
let parse_quals quals =
let expr = parse_expr quals in
let rec process_quals state = function
| [] -> state
| hd :: tl -> process_quals (process_qual state hd) tl
in match expr with
| { pexp_desc = Pexp_ident (_); _ } ->
process_qual Qualifiers.empty expr
| { pexp_desc = Pexp_apply (_, _); _ } ->
process_qual Qualifiers.empty expr
| { pexp_desc = Pexp_tuple (e); _ } ->
process_quals Qualifiers.empty e
| expr ->
location_exn ~loc:expr.pexp_loc "Invalid qualifiers list"
;;
let rec evaluate_expr = function
| [%expr [%e? lhs] + [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l + r)
| _ -> None
end
| [%expr [%e? lhs] - [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l - r)
| _ -> None
end
| [%expr [%e? lhs] * [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l * r)
| _ -> None
end
| [%expr [%e? lhs] / [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l / r)
| _ -> None
end
| [%expr [%e? lhs] land [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l land r)
| _ -> None
end
| [%expr [%e? lhs] lor [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l lor r)
| _ -> None
end
| [%expr [%e? lhs] lxor [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l lxor r)
| _ -> None
end
| [%expr [%e? lhs] lsr [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l lsr r)
| _ -> None
end
| [%expr [%e? lhs] asr [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l asr r)
| _ -> None
end
| [%expr [%e? lhs] mod [%e? rhs]] ->
begin match evaluate_expr lhs, evaluate_expr rhs with
| Some l, Some r -> Some (l mod r)
| _ -> None
end
| { pexp_desc = Pexp_constant (const); _ } ->
begin match const with
| Pconst_integer(i, _) -> Some (int_of_string i)
| _ -> None
end
| _ -> None
;;
let parse_match_fields str =
let open MatchField in
split_string ~on:":" str.txt
|> split_loc ~loc:str.loc
|> function
| [ { txt = "_" ; loc } as pat ] ->
MatchField.Any (parse_pattern pat)
| [ spat; slen ] ->
let qls = Qualifiers.default
and eln = parse_expr slen
and pat = parse_pattern spat
and opt = false in
let len = (eln, evaluate_expr eln) in
MatchField.Tuple { pat; len; qls; opt }
| [ spat; slen; sqls ] ->
let qls = Qualifiers.set_defaults (parse_quals sqls)
and eln = parse_expr slen
and pat = parse_pattern spat
and opt = false in
let len = (eln, evaluate_expr eln) in
MatchField.Tuple { pat; len; qls; opt }
| [ stmt ] ->
let pat_str = stmt.txt in
location_exn ~loc:stmt.loc ("Invalid statement: '" ^ pat_str ^ "'")
| _ ->
location_exn ~loc:str.loc "Invalid number of fields in statement"
;;
let stitch_ambiguous_operators lst =
let fn e = function
| [] -> [ e ]
| hd :: tl when hd = "" || e == "" -> e :: hd :: tl
| hd :: tl when Str.first_chars hd 1 = ">" -> (e ^ ":" ^ hd) :: tl
| l -> e :: l
in
List.fold_right fn lst []
let parse_const_fields str =
let open Qualifiers in
split_string ~on:":" str.txt
|> stitch_ambiguous_operators
|> split_loc ~loc:str.loc
|> function
| [ vl; len ] ->
(parse_expr vl, Some (parse_expr len), Some Qualifiers.default)
| [ vl; len; quals ] ->
let q = Qualifiers.set_defaults (parse_quals quals) in
begin match q.bind, q.map, q.check, q.save_offset_to with
| Some _, _, _, _ ->
location_exn ~loc:str.loc "Bind meaningless in constructor"
| _, Some _, _, _ ->
location_exn ~loc:str.loc "Map meaningless in constructor"
| _, _, Some _, _ ->
location_exn ~loc:str.loc "Check meaningless in constructor"
| _, _, _, Some _ ->
location_exn ~loc:str.loc "Saving offset meaningless in constructor"
| None, None, None, None ->
(parse_expr vl, Some (parse_expr len), Some (q))
end
| [ stmt ] ->
let pat_str = stmt.txt in
location_exn ~loc:stmt.loc ("Invalid statement: '" ^ pat_str ^ "'")
| _ ->
location_exn ~loc:str.loc "Invalid number of fields in statement"
;;
let check_field_len ~loc fld =
let (l, v) = fld.MatchField.len
in
match v, fld.MatchField.qls.Qualifiers.value_type with
| Some (n), Some (Type.String) ->
if n < -1 || (n > 0 && (n mod 8) <> 0) then
location_exn ~loc "Length of string must be > 0 and multiple of 8, or the special value -1"
else Some n
| Some (n), Some (Type.Bitstring) ->
if n < -1 then location_exn ~loc "Length of bitstring must be >= 0 or the special value -1"
else Some n
| Some (n), Some (Type.Int) ->
if n < 1 || n > 64 then location_exn ~loc "Length of int field must be [1..64]"
else Some n
| None, Some (_) -> None
| _, None -> location_exn ~loc "No type to check"
;;
let get_inttype ~loc ~fastpath = function
| v when v > 8 && v <= 16 -> if fastpath then "int16" else "int"
| v when v > 16 && v <= 31 -> if fastpath then "int32" else "int"
| v when v = 32 -> "int32"
| v when v > 32 && v <= 64 -> "int64"
| _ -> location_exn ~loc "Invalid integer size"
let gen_int_extractor_static ~loc nxt size sign endian =
let edat = nxt.Context.dat.Entity.exp
and eoff = nxt.Context.off.Entity.exp
in
let sn = Sign.to_string sign
and ft = get_inttype ~loc ~fastpath:true size
and en = Endian.to_string endian in
let fp = sprintf "Bitstring.extract_fastpath_%s_%s_%s" ft en sn
in
[%expr
[%e evar ~loc fp] [%e edat] ([%e eoff] lsr 3)]
[@metaloc loc]
;;
let gen_int_extractor_dynamic ~loc nxt size sign endian =
let edat = nxt.Context.dat.Entity.exp
and eoff = nxt.Context.off.Entity.exp
and elen = nxt.Context.len.Entity.exp
in
let sn = Sign.to_string sign
and it = get_inttype ~loc ~fastpath:false size
and en = Endian.to_string endian in
let ex = sprintf "Bitstring.extract_%s_%s_%s" it en sn
in
[%expr [%e evar ~loc ex] [%e edat] [%e eoff] [%e elen] [%e int ~loc size]]
[@metaloc loc]
;;
let gen_int_extractor ~loc nxt fld =
let open Qualifiers in
let (l, v) = fld.MatchField.len
in
let edat = nxt.Context.dat.Entity.exp
and eoff = nxt.Context.off.Entity.exp
and elen = nxt.Context.len.Entity.exp
in
match v, fld.MatchField.qls.sign, fld.MatchField.qls.endian with
| Some (size), Some (_), Some (_) when size = 1 ->
[%expr
Bitstring.extract_bit [%e edat] [%e eoff] [%e elen] [%e l]]
[@metaloc loc]
| Some (size), Some (sign), Some (_) when size >= 2 && size <= 8 ->
let ex = sprintf "Bitstring.extract_char_%s" (Sign.to_string sign)
in
[%expr
[%e evar ~loc ex] [%e edat] [%e eoff] [%e elen] [%e int ~loc size]]
[@metaloc loc]
| Some (size), Some (sign), Some (Endian.Referred r) ->
let ss = Sign.to_string sign
and it = get_inttype ~loc ~fastpath:false size in
let ex = sprintf "Bitstring.extract_%s_ee_%s" it ss
in
[%expr
[%e evar ~loc ex] ([%e r]) [%e edat] [%e eoff] [%e elen] [%e int ~loc size]]
[@metaloc loc]
| Some (size), Some (sign), Some (endian) ->
if fld.MatchField.opt then
gen_int_extractor_static ~loc nxt size sign endian
else
gen_int_extractor_dynamic ~loc nxt size sign endian
| None, Some (sign), Some (Endian.Referred r) ->
let ss = Sign.to_string sign in
let ex = sprintf "Bitstring.extract_int64_ee_%s" ss in
[%expr
[%e evar ~loc ex] ([%e r]) [%e edat] [%e eoff] [%e elen] ([%e l])]
[@metaloc loc]
| None, Some (sign), Some (endian) ->
let es = Endian.to_string endian and ss = Sign.to_string sign in
let ex = sprintf "Bitstring.extract_int64_%s_%s" es ss in
[%expr
[%e evar ~loc ex] [%e edat] [%e eoff] [%e elen] ([%e l])]
[@metaloc loc]
| _, _, _ ->
location_exn ~loc "Invalid type"
;;
let gen_extractor ~loc nxt fld =
let open Qualifiers in
let (l, v) = fld.MatchField.len
in
let edat = nxt.Context.dat.Entity.exp
and eoff = nxt.Context.off.Entity.exp
and elen = nxt.Context.len.Entity.exp
in
match fld.MatchField.qls.value_type with
| Some (Type.Bitstring) -> begin
match v with
| Some (-1) ->
[%expr ([%e edat], [%e eoff], [%e elen])] [@metaloc loc]
| Some (_) | None ->
[%expr ([%e edat], [%e eoff], [%e l])] [@metaloc loc]
end
| Some (Type.String) ->
[%expr
(Bitstring.string_of_bitstring ([%e edat], [%e eoff], [%e l]))]
[@metaloc loc]
| Some (Type.Int) ->
gen_int_extractor ~loc nxt fld
| _ ->
location_exn ~loc "Invalid type"
;;
let gen_value ~loc fld res beh =
let open Qualifiers in
match fld.MatchField.qls.bind, fld.MatchField.qls.map with
| Some b, None ->
[%expr let [%p fld.pat] = [%e b] in [%e beh]][@metaloc loc]
| None, Some m ->
[%expr let [%p fld.pat] = [%e m] [%e res] in [%e beh]][@metaloc loc]
| _, _ -> beh
;;
let rec gen_next ~loc cur nxt fld beh fields =
let open Entity in
let open Context in
let (l, v) = fld.MatchField.len in
match v with
| Some (-1) ->
[%expr
let [%p nxt.off.pat] = [%e nxt.off.exp] + [%e nxt.len.exp]
and [%p nxt.len.pat] = 0 in
[%e (gen_fields ~loc cur nxt beh fields)]]
[@metaloc loc]
| Some (_) | None ->
[%expr
let [%p nxt.off.pat] = [%e nxt.off.exp] + [%e l]
and [%p nxt.len.pat] = [%e nxt.len.exp] - [%e l] in
[%e (gen_fields ~loc cur nxt beh fields)]]
[@metaloc loc]
and gen_next_all ~loc cur nxt beh fields =
let open Entity in
let open Context in
[%expr
let [%p nxt.off.pat] = [%e nxt.off.exp] + [%e nxt.len.exp]
and [%p nxt.len.pat] = 0 in
[%e (gen_fields ~loc cur nxt beh fields)]]
[@metaloc loc]
and gen_match_check ~loc = function
| Some chk -> chk
| None -> constr ~loc "true" []
and gen_match ~loc cur nxt fld beh fields =
let open Entity in
let open Context in
let open Qualifiers in
let value = Entity.make ~loc "val"
and (l, _) = fld.MatchField.len
in
let mcheck = gen_match_check ~loc fld.MatchField.qls.check
and mfields = gen_fields ~loc cur nxt beh fields
and mres = gen_extractor ~loc nxt fld
in
let mwrap = gen_value ~loc fld value.exp mfields
in
let mcase = [%expr
begin match [%e value.exp] with
| [%p fld.MatchField.pat] when [%e mcheck] -> [%e mwrap]
| _ -> ()
end][@metaloc loc]
in
[%expr
let [%p value.pat] = [%e mres]
and [%p nxt.off.pat] = [%e nxt.off.exp] + [%e l]
and [%p nxt.len.pat] = [%e nxt.len.exp] - [%e l] in [%e mcase]]
[@metaloc loc]
and gen_offset ~loc cur nxt fld beh =
let open Context in
let open Entity in
let open Qualifiers in
match fld.MatchField.qls.offset with
| Some ({ pexp_loc; _ } as off) ->
[%expr
let [%p nxt.off.pat] = [%e cur.off.exp] + [%e off] in [%e beh]]
[@metaloc pexp_loc]
| None -> beh
and gen_offset_saver ~loc cur nxt fld beh =
let open Context in
let open Entity in
let open Qualifiers in
match fld.MatchField.qls.save_offset_to with
| Some { pexp_desc = Pexp_ident ({ txt; loc = eloc }); _ } ->
let ptxt = pvar ~loc:eloc (Longident.last txt) in
[%expr
let [%p ptxt] = [%e nxt.off.exp] - [%e cur.off.exp] in [%e beh]]
[@metaloc eloc]
| Some _ | None -> beh
and gen_unbound_string ~loc cur nxt fld beh fields =
let p = fld.MatchField.pat
in
match p with
| { ppat_desc = Ppat_var(_); _ } ->
[%expr
let [%p p] = [%e (gen_extractor ~loc nxt fld)] in
[%e (gen_next_all ~loc cur nxt beh fields)]]
[@metaloc loc]
| [%pat? _ ] ->
[%expr
[%e (gen_next_all ~loc cur nxt beh fields)]]
[@metaloc loc]
| _ ->
location_exn ~loc "Unbound string or bitstring can only be assigned to a variable or skipped"
and gen_bound_bitstring ~loc cur nxt fld beh fields =
let open Entity in
let open Context in
let p = fld.MatchField.pat
and (l, _) = fld.MatchField.len
in
match p with
| { ppat_desc = Ppat_var(_); _ } ->
[%expr
if Stdlib.(>=) [%e nxt.len.exp] [%e l] then
let [%p p] = [%e (gen_extractor ~loc nxt fld)] in
[%e (gen_next ~loc cur nxt fld beh fields)]
else ()]
[@metaloc loc]
| [%pat? _ ] ->
[%expr
if Stdlib.(>=) [%e nxt.len.exp] [%e l] then
[%e (gen_next ~loc cur nxt fld beh fields)]
else ()]
[@metaloc loc]
| _ ->
location_exn ~loc "Bound bitstring can only be assigned to variables or skipped"
and gen_bound_string ~loc cur nxt fld beh fields =
let open Entity in
let open Context in
let (l, _) = fld.MatchField.len
in
[%expr
if Stdlib.(>=) [%e nxt.len.exp] [%e l] then
[%e (gen_match ~loc cur nxt fld beh fields)]
else ()]
[@metaloc loc]
and gen_bound_int_with_size ~loc cur nxt fld beh fields =
let open Entity in
let open Context in
let (l, _) = fld.MatchField.len
in
[%expr
if Stdlib.(>=) [%e nxt.len.exp] [%e l] then
[%e (gen_match ~loc cur nxt fld beh fields)]
else ()]
[@metaloc loc]
and gen_bound_int ~loc cur nxt fld beh fields =
let open Entity in
let open Context in
let (l, _) = fld.MatchField.len
in
[%expr
if Stdlib.(>=) [%e l] 1 &&
Stdlib.(<=) [%e l] 64 &&
Stdlib.(>=) [%e nxt.len.exp] [%e l] then
[%e (gen_match ~loc cur nxt fld beh fields)]
else ()]
[@metaloc loc]
and gen_fields_with_quals_by_type ~loc cur nxt fld beh fields =
let open Qualifiers in
match check_field_len ~loc fld, fld.MatchField.qls.value_type with
| Some (-1), Some (Type.Bitstring | Type.String) ->
gen_unbound_string ~loc cur nxt fld beh fields
| (Some (_) | None), Some (Type.Bitstring) ->
gen_bound_bitstring ~loc cur nxt fld beh fields
| (Some (_) | None), Some (Type.String) ->
gen_bound_string ~loc cur nxt fld beh fields
| Some (s), Some (Type.Int) ->
if s >= 1 && s <= 64 then
gen_bound_int_with_size ~loc cur nxt fld beh fields
else
location_exn ~loc "Invalid bit length for type Integer"
| None, Some (Type.Int) ->
gen_bound_int ~loc cur nxt fld beh fields
| _, _ ->
location_exn ~loc "No type to generate"
and gen_fields_with_quals ~loc cur nxt fld beh fields =
gen_fields_with_quals_by_type ~loc cur nxt fld beh fields
|> gen_offset_saver ~loc cur nxt fld
|> gen_offset ~loc cur nxt fld
and gen_fields ~loc cur nxt beh fields =
let (exp, alias) = beh
in
match fields with
| [] ->
begin match alias with
| None -> exp
| Some a -> [%expr
let [%p pvar ~loc a] = ([%e cur.dat.exp], [%e cur.off.exp], ([%e cur.len.exp] - [%e nxt.len.exp]))
in
[%e exp]
][@metaloc loc]
end
| MatchField.Any (_) :: tl ->
begin match alias with
| None -> exp
| Some a -> [%expr
let [%p pvar ~loc a] = ([%e cur.dat.exp], [%e cur.off.exp], [%e cur.len.exp])
in
[%e exp]
][@metaloc loc]
end
| MatchField.Tuple (fld) :: tl -> gen_fields_with_quals ~loc cur nxt fld beh tl
;;
let is_field_size_open_ended = function
| (_, Some (-1)) -> true
| _ -> false
let check_for_open_endedness fields =
let check init fld =
let p = fld.MatchField.pat
and l = fld.MatchField.len in
let oe = is_field_size_open_ended l in
if init || (oe && init) then
location_exn ~loc:p.ppat_loc "Pattern is already open-ended"
else oe
in
let inspect init = function
| MatchField.Any (_) -> init && false
| MatchField.Tuple fld -> check init fld
in
let rec scan init = function
| [] -> ()
| hd :: tl -> scan (inspect init hd) tl
in
scan false fields; fields
;;
let mark_optimized_fastpath fields =
let open Qualifiers in
let open MatchField
in
let check_field off tuple =
match tuple with
| { pat; len = (l, Some (v)); qls = { value_type = Some (Type.Int); _ }; _ } ->
if (off land 7) = 0 && (v = 16 || v = 32 || v = 64) then
(Some (off + v), MatchField.Tuple { tuple with opt = true })
else
(None, MatchField.Tuple tuple)
| _ ->
(None, MatchField.Tuple tuple)
in
let check_offset_and_field offset fld =
match offset, fld with
| Some (off), MatchField.Tuple (tuple) -> check_field off tuple
| _, _ -> (None, fld)
in
let rec scan offset result = function
| [] -> result
| hd :: tl ->
let (noff, nfld) = check_offset_and_field offset hd in
scan noff (result @ [ nfld ]) tl
in
scan (Some 0) [] fields
;;
let gen_case_constant ~loc cur nxt res case value alias =
let open Entity in
let beh = [%expr
[%e res.exp] := Some ([%e case.pc_rhs]);
raise Exit][@metaloc loc]
in
let beh =
match case.pc_guard with
| None -> beh
| Some cond -> [%expr if [%e cond] then [%e beh] else ()][@metaloc loc]
in
split_string ~on:";" value
|> split_loc ~loc
|> List.map parse_match_fields
|> check_for_open_endedness
|> mark_optimized_fastpath
|> gen_fields ~loc cur nxt (beh, alias)
let gen_case cur nxt res case =
let loc = case.pc_lhs.ppat_loc in
match case.pc_lhs.ppat_desc with
| Ppat_constant (Pconst_string (value, _)) ->
gen_case_constant ~loc cur nxt res case value None
| Ppat_alias ({ ppat_desc = Ppat_constant (Pconst_string (value, _)); _ }, { txt = a; _ }) ->
gen_case_constant ~loc cur nxt res case value (Some a)
| _ ->
location_exn ~loc "Wrong pattern type"
;;
let rec gen_cases_sequence ~loc = function
| [] -> location_exn ~loc "Empty case list"
| [hd] -> hd
| hd :: tl -> [%expr [%e hd]; [%e gen_cases_sequence ~loc tl]][@metaloc loc]
;;
let gen_cases ~loc ident cases =
let open Entity in
let open Context in
let cur = Context.make ~loc
and res = Entity.make ~loc "res"
in
let nxt = Context.next ~loc cur
and tupl = [%pat? ([%p cur.dat.pat], [%p cur.off.pat], [%p cur.len.pat])][@metaloc loc]
and fnam = str ~loc loc.Location.loc_start.pos_fname
and lpos = int ~loc loc.Location.loc_start.pos_lnum
and cpos = int ~loc (loc.Location.loc_start.pos_cnum - loc.Location.loc_start.pos_bol)
in
List.fold_left
(fun acc case -> acc @ [ gen_case cur nxt res case ])
[]
cases
|> gen_cases_sequence ~loc
|> fun seq ->
[%expr
let [%p tupl] = [%e ident] in
let [%p nxt.off.pat] = [%e cur.off.exp]
and [%p nxt.len.pat] = [%e cur.len.exp]
and [%p res.pat] = ref None
in
(try [%e seq]; with | Exit -> ());
match ![%e res.exp] with
| Some x -> x
| None -> raise (Match_failure ([%e fnam], [%e lpos], [%e cpos]))]
[@metaloc loc]
;;
let gen_function ~loc cases =
let open Entity in
let cas = Entity.make ~loc "case" in
[%expr
(fun [%p cas.pat] -> [%e (gen_cases ~loc cas.exp cases)])]
[@metaloc loc]
let gen_constructor_exn ~loc =
let open Location in
[%expr Bitstring.Construct_failure (
[%e str ~loc "Bad field value"],
[%e str ~loc loc.loc_start.pos_fname],
[%e int ~loc loc.loc_start.pos_lnum],
[%e int ~loc loc.loc_start.pos_cnum])]
[@metaloc loc]
;;
let gen_constructor_bitstring ~loc sym (l, _, _ ) =
[%expr
Bitstring.construct_bitstring [%e sym.Entity.exp] [%e l]]
[@metaloc loc]
;;
let gen_constructor_string ~loc sym (l, _, _) =
[%expr
Bitstring.construct_string [%e sym.Entity.exp] [%e l]]
[@metaloc loc]
;;
let get_1_bit_constr_value ~loc (l, _, _) =
match (evaluate_expr l) with
| Some (1) -> [%expr true][@metaloc loc]
| Some (0) -> [%expr false][@metaloc loc]
| Some (_) | None -> l
;;
let gen_constructor_int ~loc sym fld =
let open Qualifiers in
let (l, s, q) = fld in
let eexc = gen_constructor_exn ~loc
and esym = sym.Entity.exp in
let (fnc, vl, sz) = match (evaluate_expr s), q.sign, q.endian with
| Some (size), Some (_), Some (_) when size = 1 ->
(evar ~loc "Bitstring.construct_bit", get_1_bit_constr_value ~loc fld, [%expr 1])
| Some (size), Some (sign), Some (_) when size >= 2 && size <= 8 ->
let sn = Sign.to_string sign in
let ex = sprintf "Bitstring.construct_char_%s" sn in
(evar ~loc ex, l, int ~loc size)
| Some (size), Some (sign), Some (Endian.Referred r) ->
let ss = Sign.to_string sign
and it = get_inttype ~loc ~fastpath:false size in
let ex = sprintf "Bitstring.construct_%s_ee_%s" it ss in
([%expr [%e evar ~loc ex] [%e r]], l, s)
| Some (size), Some (sign), Some (endian) ->
let tp = get_inttype ~loc ~fastpath:false size
and en = Endian.to_string endian
and sn = Sign.to_string sign in
let ex = sprintf "Bitstring.construct_%s_%s_%s" tp en sn in
(evar ~loc ex, l, int ~loc size)
| None, Some (sign), Some (Endian.Referred r) ->
let ss = Sign.to_string sign in
let ex = sprintf "Bitstring.construct_int64_ee_%s" ss in
([%expr [%e evar ~loc ex] [%e r]], l, s)
| None, Some (sign), Some (endian) ->
let en = Endian.to_string endian
and sn = Sign.to_string sign in
let ex = sprintf "Bitstring.construct_int64_%s_%s" en sn in
(evar ~loc ex, l, s)
| _, _, _ ->
location_exn ~loc "Invalid type"
in
[%expr
[%e fnc] [%e esym] [%e vl] [%e sz] [%e eexc]]
[@metaloc loc]
;;
let gen_constructor_complete ~loc sym fld =
let (_, _, q) = fld in
match q.Qualifiers.value_type with
| Some (Type.Bitstring) -> gen_constructor_bitstring ~loc sym fld
| Some (Type.String) -> gen_constructor_string ~loc sym fld
| Some (Type.Int) -> gen_constructor_int ~loc sym fld
| _ -> location_exn ~loc "Invalid type"
;;
let gen_constructor ~loc sym = function
| (f, Some (s), Some (q)) -> gen_constructor_complete ~loc sym (f, s, q)
| _ -> location_exn ~loc "Invalid field format"
;;
let gen_assignment_size_of_sized_field ~loc (f, s, q) =
match (evaluate_expr s), option_bind q (fun q -> q.Qualifiers.value_type) with
| Some (-1), Some (Type.String) -> [%expr (String.length [%e f] * 8)]
| Some (v), Some (Type.String) when v > 0 && (v mod 8) = 0 -> s
| Some (_), Some (Type.String) ->
location_exn ~loc "Length of string must be > 0 and multiple of 8, or the special value -1"
| Some (-1), Some (Type.Bitstring) -> [%expr (Bitstring.bitstring_length [%e f])]
| Some (v), Some (Type.Bitstring) when v > 0 -> s
| Some (_), Some (Type.Bitstring) ->
location_exn ~loc "Length of bitstring must be >= 0 or the special value -1"
| Some (v), _ when v > 0 -> s
| Some (v), _ ->
location_exn ~loc "Negative or null field size in constructor"
| None, _ -> s
;;
let gen_assignment_size_of_field ~loc = function
| (_, None, _) -> [%expr 0]
| (f, Some (s), q) -> gen_assignment_size_of_sized_field ~loc (f, s, q)
;;
let rec gen_assignment_size ~loc = function
| [] -> [%expr 0]
| field :: tl ->
let this = gen_assignment_size_of_field ~loc field in
let next = gen_assignment_size ~loc tl in
[%expr [%e this] + ([%e next])][@metaloc loc]
;;
let gen_assignment_behavior ~loc sym fields =
let size = gen_assignment_size ~loc fields in
let res = sym.Entity.exp in
let rep = [%expr Bitstring.Buffer.contents [%e res]][@metaloc loc] in
let len = match (evaluate_expr size) with
| Some (v) -> int v
| None -> size
in
let post =
[%expr
let _res = [%e rep] in
if Stdlib.(=) (Bitstring.bitstring_length _res) [%e len]
then _res else raise Exit]
[@metaloc loc]
in
let seq = List.fold_right
(fun fld acc -> [%expr [%e (gen_constructor ~loc sym fld)]; [%e acc]])
fields
post
in
[%expr
let [%p sym.Entity.pat] = Bitstring.Buffer.create () in
[%e seq]]
[@metaloc loc]
;;
let parse_assignment_behavior ~loc sym value =
split_string ~on:";" value
|> split_loc ~loc
|> List.map (fun flds -> parse_const_fields flds)
|> gen_assignment_behavior ~loc sym
;;
let gen_constructor_expr ~loc value =
let open Entity in
let sym = Entity.make ~loc "constructor" in
let beh = parse_assignment_behavior ~loc sym value in
[%expr let [%p sym.pat] = fun () -> [%e beh] in [%e sym.exp] ()]
;;
let transform_single_let ~loc ast expr =
match ast.pvb_pat.ppat_desc, ast.pvb_expr.pexp_desc with
| Parsetree.Ppat_var (s), Pexp_constant (Pconst_string (value, _)) ->
let pat = pvar ~loc s.txt in
let constructor_expr = gen_constructor_expr ~loc value in
[%expr let [%p pat] = [%e constructor_expr] in [%e expr]]
| _ -> location_exn ~loc "Invalid pattern type"
;;
let extension expr =
let loc = expr.pexp_loc in
match expr.pexp_desc with
| Pexp_constant (Pconst_string (value, (_ : string option))) ->
gen_constructor_expr ~loc value
| Pexp_let (Nonrecursive, bindings, expr) ->
List.fold_right
(fun binding expr -> transform_single_let ~loc binding expr)
bindings
expr
| Pexp_match (ident, cases) ->
gen_cases ~loc ident cases
| Pexp_function (cases) ->
gen_function ~loc cases
| _ ->
location_exn ~loc
"'bitstring' can only be used with 'let', 'match', and as '[%bitstring]'"
let expression mapper = function
| [%expr [%bitstring [%e? e0]]] -> mapper.expr mapper (extension e0)
| expr -> Ast_mapper.default_mapper.expr mapper expr
let structure_item_mapper mapper = function
| [%stri [%%bitstring let [%p? var] = [%e? e0]]] ->
[%stri let [%p mapper.pat mapper var] = [%e mapper.expr mapper (extension e0)]]
| stri -> Ast_mapper.default_mapper.structure_item mapper stri
let rewriter config cookies = {
Ast_mapper.default_mapper with
expr = expression;
structure_item = structure_item_mapper;
}
let () =
Driver.register ~name:"ppx_bitstring" ~args:[] Versions.ocaml_405 rewriter
;;