1 (* Bitmatch persistent patterns.
2 * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 type patt = Camlp4.PreCast.Syntax.Ast.patt
28 type expr = Camlp4.PreCast.Syntax.Ast.expr
29 type loc_t = Camlp4.PreCast.Syntax.Ast.Loc.t
31 (* Field. In bitmatch (patterns) the type is [patt field]. In
32 * BITSTRING (constructor) the type is [expr field].
35 field : 'a; (* field ('a is either patt or expr) *)
36 flen : expr; (* length in bits, may be non-const *)
37 endian : endian_expr; (* endianness *)
38 signed : bool; (* true if signed, false if unsigned *)
39 t : field_type; (* type *)
40 _loc : Loc.t; (* location in source code *)
41 offset : expr option; (* offset expression *)
43 and field_type = Int | String | Bitstring (* field type *)
45 | ConstantEndian of Bitmatch.endian (* a constant little/big/nativeendian *)
46 | EndianExpr of expr (* an endian expression *)
48 type pattern = patt field list
50 type constructor = expr field list
52 type named = string * alt
55 | Constructor of constructor
57 (* Work out if an expression is an integer constant.
59 * Returns [Some i] if so (where i is the integer value), else [None].
61 * Fairly simplistic algorithm: we can only detect simple constant
62 * expressions such as [k], [k+c], [k-c] etc.
64 let rec expr_is_constant = function
65 | <:expr< $int:i$ >> -> (* Literal integer constant. *)
66 Some (int_of_string i)
67 | <:expr< $a$ + $b$ >> -> (* Addition of constants. *)
68 (match expr_is_constant a, expr_is_constant b with
69 | Some a, Some b -> Some (a+b)
71 | <:expr< $a$ - $b$ >> -> (* Subtraction. *)
72 (match expr_is_constant a, expr_is_constant b with
73 | Some a, Some b -> Some (a-b)
75 | <:expr< $a$ * $b$ >> -> (* Multiplication. *)
76 (match expr_is_constant a, expr_is_constant b with
77 | Some a, Some b -> Some (a*b)
79 | <:expr< $a$ / $b$ >> -> (* Division. *)
80 (match expr_is_constant a, expr_is_constant b with
81 | Some a, Some b -> Some (a/b)
83 | <:expr< $a$ lsl $b$ >> -> (* Shift left. *)
84 (match expr_is_constant a, expr_is_constant b with
85 | Some a, Some b -> Some (a lsl b)
87 | <:expr< $a$ lsr $b$ >> -> (* Shift right. *)
88 (match expr_is_constant a, expr_is_constant b with
89 | Some a, Some b -> Some (a lsr b)
91 | _ -> None (* Anything else is not constant. *)
93 let string_of_field_type = function
96 | Bitstring -> "bitstring"
98 let patt_printer = function
99 | <:patt< $lid:id$ >> -> id
100 | <:patt< _ >> -> "_"
103 let expr_printer = function
104 | <:expr< $lid:id$ >> -> id
105 | <:expr< $int:i$ >> -> i
106 | _ -> "[expression]"
108 let _string_of_field { flen = flen;
109 endian = endian; signed = signed; t = t;
113 match expr_is_constant flen with
114 | Some i -> string_of_int i
115 | None -> "[non-const-len]" in
118 | ConstantEndian endian -> Bitmatch.string_of_endian endian
119 | EndianExpr _ -> "endian([expr])" in
120 let signed = if signed then "signed" else "unsigned" in
121 let t = string_of_field_type t in
127 match expr_is_constant expr with
128 | Some i -> sprintf ", offset(%d)" i
129 | None -> sprintf ", offset([expr])" in
131 let loc_fname = Loc.file_name _loc in
132 let loc_line = Loc.start_line _loc in
133 let loc_char = Loc.start_off _loc - Loc.start_bol _loc in
135 sprintf "%s : %s, %s, %s%s (* %S:%d %d *)"
136 flen t endian signed offset loc_fname loc_line loc_char
138 let rec string_of_pattern_field ({ field = patt } as field) =
139 sprintf "%s : %s" (patt_printer patt) (_string_of_field field)
141 and string_of_constructor_field ({ field = expr } as field) =
142 sprintf "%s : %s" (expr_printer expr) (_string_of_field field)
144 let string_of_pattern pattern =
146 String.concat ";\n " (List.map string_of_pattern_field pattern) ^
149 let string_of_constructor constructor =
151 String.concat ";\n " (List.map string_of_constructor_field constructor) ^
154 let named_to_channel chan n = Marshal.to_channel chan n []
156 let named_to_string n = Marshal.to_string n []
158 let named_to_buffer str ofs len n = Marshal.to_buffer str ofs len n []
160 let named_from_channel = Marshal.from_channel
162 let named_from_string = Marshal.from_string
164 let create_pattern_field _loc =
166 field = <:patt< _ >>;
167 flen = <:expr< 32 >>;
168 endian = ConstantEndian Bitmatch.BigEndian;
175 let set_lident_patt field id =
176 let _loc = field._loc in
177 { field with field = <:patt< $lid:id$ >> }
178 let set_int_patt field i =
179 let _loc = field._loc in
180 { field with field = <:patt< $`int:i$ >> }
181 let set_string_patt field str =
182 let _loc = field._loc in
183 { field with field = <:patt< $str:str$ >> }
184 let set_unbound_patt field =
185 let _loc = field._loc in
186 { field with field = <:patt< _ >> }
187 let set_patt field patt = { field with field = patt }
188 let set_length_int field flen =
189 let _loc = field._loc in
190 { field with flen = <:expr< $`int:flen$ >> }
191 let set_length field flen = { field with flen = flen }
192 let set_endian field endian = { field with endian = ConstantEndian endian }
193 let set_endian_expr field expr = { field with endian = EndianExpr expr }
194 let set_signed field signed = { field with signed = signed }
195 let set_type_int field = { field with t = Int }
196 let set_type_string field = { field with t = String }
197 let set_type_bitstring field = { field with t = Bitstring }
198 let set_location field loc = { field with _loc = loc }
199 let set_offset_int field i =
200 let _loc = field._loc in
201 { field with offset = Some <:expr< $`int:i$ >> }
202 let set_offset field expr = { field with offset = Some expr }
203 let set_no_offset field = { field with offset = None }
205 let create_constructor_field _loc =
207 field = <:expr< 0 >>;
208 flen = <:expr< 32 >>;
209 endian = ConstantEndian Bitmatch.BigEndian;
216 let set_lident_expr field id =
217 let _loc = field._loc in
218 { field with field = <:expr< $lid:id$ >> }
219 let set_int_expr field i =
220 let _loc = field._loc in
221 { field with field = <:expr< $`int:i$ >> }
222 let set_string_expr field str =
223 let _loc = field._loc in
224 { field with field = <:expr< $str:str$ >> }
225 let set_expr field expr =
226 let _loc = field._loc in
227 { field with field = expr }
229 let get_patt field = field.field
230 let get_expr field = field.field
231 let get_length field = field.flen
232 let get_endian field = field.endian
233 let get_signed field = field.signed
234 let get_type field = field.t
235 let get_location field = field._loc
236 let get_offset field = field.offset