+ let () =
+ let t = P.get_type field in
+ if (t = P.Bitstring || t = P.String) && (endian_set || 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 endian_set then field else P.set_endian field BigEndian in
+ let field = if signed_set then field else P.set_signed field false in
+ let field = if type_set then field else P.set_type_int field in
+
+ field
+
+(* Generate the code for a constructor, ie. 'BITSTRING ...'. *)
+let output_constructor _loc fields =
+ let fail = locfail _loc in
+
+ 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
+
+ (* 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 offset = P.get_offset field in
+
+ (* offset() 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 offset <> None then
+ fail "offset expressions are 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
+
+ (* Choose the right constructor function. *)
+ let int_construct_const = function
+ (* XXX The meaning of signed/unsigned breaks down at
+ * 31, 32, 63 and 64 bits.
+ *)
+ | (1, _, _) ->
+ <:expr<Bitmatch.construct_bit>>
+ | ((2|3|4|5|6|7|8), _, false) ->
+ <:expr<Bitmatch.construct_char_unsigned>>
+ | ((2|3|4|5|6|7|8), _, true) ->
+ <:expr<Bitmatch.construct_char_signed>>
+ | (i, P.ConstantEndian BigEndian, false) when i <= 31 ->
+ <:expr<Bitmatch.construct_int_be_unsigned>>
+ | (i, P.ConstantEndian BigEndian, true) when i <= 31 ->
+ <:expr<Bitmatch.construct_int_be_signed>>
+ | (i, P.ConstantEndian LittleEndian, false) when i <= 31 ->
+ <:expr<Bitmatch.construct_int_le_unsigned>>
+ | (i, P.ConstantEndian LittleEndian, true) when i <= 31 ->
+ <:expr<Bitmatch.construct_int_le_signed>>
+ | (i, P.ConstantEndian NativeEndian, false) when i <= 31 ->
+ <:expr<Bitmatch.construct_int_ne_unsigned>>
+ | (i, P.ConstantEndian NativeEndian, true) when i <= 31 ->
+ <:expr<Bitmatch.construct_int_ne_signed>>
+ | (i, P.EndianExpr expr, false) when i <= 31 ->
+ <:expr<Bitmatch.construct_int_ee_unsigned $expr$>>
+ | (i, P.EndianExpr expr, true) when i <= 31 ->
+ <:expr<Bitmatch.construct_int_ee_signed $expr$>>
+ | (32, P.ConstantEndian BigEndian, false) ->
+ <:expr<Bitmatch.construct_int32_be_unsigned>>
+ | (32, P.ConstantEndian BigEndian, true) ->
+ <:expr<Bitmatch.construct_int32_be_signed>>
+ | (32, P.ConstantEndian LittleEndian, false) ->
+ <:expr<Bitmatch.construct_int32_le_unsigned>>
+ | (32, P.ConstantEndian LittleEndian, true) ->
+ <:expr<Bitmatch.construct_int32_le_signed>>
+ | (32, P.ConstantEndian NativeEndian, false) ->
+ <:expr<Bitmatch.construct_int32_ne_unsigned>>
+ | (32, P.ConstantEndian NativeEndian, true) ->
+ <:expr<Bitmatch.construct_int32_ne_signed>>
+ | (32, P.EndianExpr expr, false) ->
+ <:expr<Bitmatch.construct_int32_ee_unsigned $expr$>>
+ | (32, P.EndianExpr expr, true) ->
+ <:expr<Bitmatch.construct_int32_ee_signed $expr$>>
+ | (_, P.ConstantEndian BigEndian, false) ->
+ <:expr<Bitmatch.construct_int64_be_unsigned>>
+ | (_, P.ConstantEndian BigEndian, true) ->
+ <:expr<Bitmatch.construct_int64_be_signed>>
+ | (_, P.ConstantEndian LittleEndian, false) ->
+ <:expr<Bitmatch.construct_int64_le_unsigned>>
+ | (_, P.ConstantEndian LittleEndian, true) ->
+ <:expr<Bitmatch.construct_int64_le_signed>>
+ | (_, P.ConstantEndian NativeEndian, false) ->
+ <:expr<Bitmatch.construct_int64_ne_unsigned>>
+ | (_, P.ConstantEndian NativeEndian, true) ->
+ <:expr<Bitmatch.construct_int64_ne_signed>>
+ | (_, P.EndianExpr expr, false) ->
+ <:expr<Bitmatch.construct_int64_ee_unsigned $expr$>>
+ | (_, P.EndianExpr expr, true) ->
+ <:expr<Bitmatch.construct_int64_ee_signed $expr$>>
+ in
+ let int_construct = function
+ | (P.ConstantEndian BigEndian, false) ->
+ <:expr<Bitmatch.construct_int64_be_unsigned>>
+ | (P.ConstantEndian BigEndian, true) ->
+ <:expr<Bitmatch.construct_int64_be_signed>>
+ | (P.ConstantEndian LittleEndian, false) ->
+ <:expr<Bitmatch.construct_int64_le_unsigned>>
+ | (P.ConstantEndian LittleEndian, true) ->
+ <:expr<Bitmatch.construct_int64_le_signed>>
+ | (P.ConstantEndian NativeEndian, false) ->
+ <:expr<Bitmatch.construct_int64_ne_unsigned>>
+ | (P.ConstantEndian NativeEndian, true) ->
+ <:expr<Bitmatch.construct_int64_ne_signed>>
+ | (P.EndianExpr expr, false) ->
+ <:expr<Bitmatch.construct_int64_ee_unsigned $expr$>>
+ | (P.EndianExpr expr, true) ->
+ <:expr<Bitmatch.construct_int64_ee_signed $expr$>>
+ 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 (Bitmatch.Construct_failure
+ ("length of int field must be [1..64]",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$))
+ >>
+
+ (* 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
+ Bitmatch.construct_string $lid:buffer$ $lid:bs$
+ else
+ raise (Bitmatch.Construct_failure
+ ("length of string does not match declaration",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$))
+ >>
+
+ (* String, constant length -1, means variable length string
+ * with no checks.
+ *)
+ | P.String, Some (-1) ->
+ <:expr< Bitmatch.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
+ Bitmatch.construct_string $lid:buffer$ $lid:bs$
+ else
+ raise (Bitmatch.Construct_failure
+ ("length of string does not match declaration",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$))
+ ) else
+ raise (Bitmatch.Construct_failure
+ ("length of string must be a multiple of 8",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$))
+ ) else
+ raise (Bitmatch.Construct_failure
+ ("length of string must be > 0",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$))
+ >>
+
+ (* Bitstring, constant length >= 0. *)
+ | P.Bitstring, Some i when i >= 0 ->
+ let bs = gensym "bs" in
+ <:expr<
+ let $lid:bs$ = $fexpr$ in
+ if Bitmatch.bitstring_length $lid:bs$ = $`int:i$ then
+ Bitmatch.construct_bitstring $lid:buffer$ $lid:bs$
+ else
+ raise (Bitmatch.Construct_failure
+ ("length of bitstring does not match declaration",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$))
+ >>
+
+ (* Bitstring, constant length -1, means variable length bitstring
+ * with no checks.
+ *)
+ | P.Bitstring, Some (-1) ->
+ <:expr< Bitmatch.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 Bitmatch.bitstring_length $lid:bs$ = $lid:bslen$ then
+ Bitmatch.construct_bitstring $lid:buffer$ $lid:bs$
+ else
+ raise (Bitmatch.Construct_failure
+ ("length of bitstring does not match declaration",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$))
+ ) else
+ raise (Bitmatch.Construct_failure
+ ("length of bitstring must be > 0",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$))
+ >> 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$ = Bitmatch.Buffer.create () in
+ $fields$;
+ Bitmatch.Buffer.contents $lid:buffer$
+ >> in
+
+ if !exn_used then
+ <:expr<
+ let $lid:exn$ =
+ Bitmatch.Construct_failure ("value out of range",
+ $str:loc_fname$,
+ $int:loc_line$, $int:loc_char$) in
+ $expr$
+ >>
+ else
+ expr