Refactor constructor and extractor function name generation (Bluestorm).
[ocaml-bitstring.git] / bitmatch_persistent.ml
1 (* Bitmatch persistent patterns.
2  * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
3  *
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.
8  *
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.
13  *
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
17  *
18  * $Id$
19  *)
20
21 open Printf
22
23 open Camlp4.PreCast
24 open Syntax
25 open Ast
26
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
30
31 (* Field.  In bitmatch (patterns) the type is [patt field].  In
32  * BITSTRING (constructor) the type is [expr field].
33  *)
34 type 'a 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 *)
42 }
43 and field_type = Int | String | Bitstring (* field type *)
44 and endian_expr =
45   | ConstantEndian of Bitmatch.endian   (* a constant little/big/nativeendian *)
46   | EndianExpr of expr                  (* an endian expression *)
47
48 type pattern = patt field list
49
50 type constructor = expr field list
51
52 type named = string * alt
53 and alt =
54   | Pattern of pattern
55   | Constructor of constructor
56
57 (* Work out if an expression is an integer constant.
58  *
59  * Returns [Some i] if so (where i is the integer value), else [None].
60  *
61  * Fairly simplistic algorithm: we can only detect simple constant
62  * expressions such as [k], [k+c], [k-c] etc.
63  *)
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)
70      | _ -> None)
71   | <:expr< $a$ - $b$ >> ->             (* Subtraction. *)
72     (match expr_is_constant a, expr_is_constant b with
73      | Some a, Some b -> Some (a-b)
74      | _ -> None)
75   | <:expr< $a$ * $b$ >> ->             (* Multiplication. *)
76     (match expr_is_constant a, expr_is_constant b with
77      | Some a, Some b -> Some (a*b)
78      | _ -> None)
79   | <:expr< $a$ / $b$ >> ->             (* Division. *)
80     (match expr_is_constant a, expr_is_constant b with
81      | Some a, Some b -> Some (a/b)
82      | _ -> None)
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)
86      | _ -> None)
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)
90      | _ -> None)
91   | _ -> None                           (* Anything else is not constant. *)
92
93 let string_of_field_type = function
94   | Int -> "int"
95   | String -> "string"
96   | Bitstring -> "bitstring"
97
98 let patt_printer = function
99   | <:patt< $lid:id$ >> -> id
100   | <:patt< _ >> -> "_"
101   | _ -> "[pattern]"
102
103 let expr_printer = function
104   | <:expr< $lid:id$ >> -> id
105   | <:expr< $int:i$ >> -> i
106   | _ -> "[expression]"
107
108 let _string_of_field { flen = flen;
109                        endian = endian; signed = signed; t = t;
110                        _loc = _loc;
111                        offset = offset } =
112   let flen =
113     match expr_is_constant flen with
114     | Some i -> string_of_int i
115     | None -> "[non-const-len]" in
116   let endian =
117     match endian with
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
122
123   let offset =
124     match offset with
125     | None -> ""
126     | Some expr ->
127         match expr_is_constant expr with
128         | Some i -> sprintf ", offset(%d)" i
129         | None -> sprintf ", offset([expr])" in
130
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
134
135   sprintf "%s : %s, %s, %s%s (* %S:%d %d *)"
136     flen t endian signed offset loc_fname loc_line loc_char
137
138 let rec string_of_pattern_field ({ field = patt } as field) =
139   sprintf "%s : %s" (patt_printer patt) (_string_of_field field)
140
141 and string_of_constructor_field ({ field = expr } as field) =
142   sprintf "%s : %s" (expr_printer expr) (_string_of_field field)
143
144 let string_of_pattern pattern =
145   "{ " ^
146     String.concat ";\n  " (List.map string_of_pattern_field pattern) ^
147     " }\n"
148
149 let string_of_constructor constructor =
150   "{ " ^
151     String.concat ";\n  " (List.map string_of_constructor_field constructor) ^
152     " }\n"
153
154 let named_to_channel chan n = Marshal.to_channel chan n []
155
156 let named_to_string n = Marshal.to_string n []
157
158 let named_to_buffer str ofs len n = Marshal.to_buffer str ofs len n []
159
160 let named_from_channel = Marshal.from_channel
161
162 let named_from_string = Marshal.from_string
163
164 let create_pattern_field _loc =
165   {
166     field = <:patt< _ >>;
167     flen = <:expr< 32 >>;
168     endian = ConstantEndian Bitmatch.BigEndian;
169     signed = false;
170     t = Int;
171     _loc = _loc;
172     offset = None;
173   }
174
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 }
204
205 let create_constructor_field _loc =
206   {
207     field = <:expr< 0 >>;
208     flen = <:expr< 32 >>;
209     endian = ConstantEndian Bitmatch.BigEndian;
210     signed = false;
211     t = Int;
212     _loc = _loc;
213     offset = None;
214   }
215
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 }
228
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