Integrated image/kdata into kimage structure. Removed dead-code.
[virt-mem.git] / extract / codegen / code_generation.ml
1 (* Memory info command for virtual domains.
2    (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *)
19
20 open Camlp4.PreCast
21 open Syntax
22 (*open Ast*)
23
24 open ExtList
25 open ExtString
26 open Printf
27
28 module PP = Pahole_parser
29 module MM = Minimizer
30
31 let rec uniq ?(cmp = Pervasives.compare) = function
32     [] -> []
33   | [x] -> [x]
34   | x :: y :: xs when cmp x y = 0 ->
35       uniq (x :: xs)
36   | x :: y :: xs ->
37       x :: uniq (y :: xs)
38
39 let sort_uniq ?cmp xs =
40   let xs = List.sort ?cmp xs in
41   let xs = uniq ?cmp xs in
42   xs
43
44 (* We don't care about locations when generating code, so it's
45  * useful to just have a single global _loc.
46  *)
47 let _loc = Loc.ghost
48
49 (* Some handy camlp4 construction functions which do some
50  * things that ought to be easy/obvious but aren't.
51  *
52  * 'concat_str_items' concatenates a list of str_item together into
53  * one big str_item.
54  *
55  * 'concat_record_fields' concatenates a list of records fields into
56  * a record.  The list must have at least one element.
57  *
58  * 'build_record' builds a record out of record fields.
59  * 
60  * 'build_tuple_from_exprs' builds an arbitrary length tuple from
61  * a list of expressions of length >= 2.
62  *
63  * Thanks to bluestorm on #ocaml for getting these working.
64  *)
65 let concat_str_items items =
66   match items with
67   | [] -> <:str_item< >>
68   | x :: xs ->
69       List.fold_left (fun xs x -> <:str_item< $xs$ $x$ >>) x xs
70
71 let concat_sig_items items =
72   match items with
73   | [] -> <:sig_item< >>
74   | x :: xs ->
75       List.fold_left (fun xs x -> <:sig_item< $xs$ $x$ >>) x xs
76
77 let concat_exprs exprs =
78   match exprs with
79   | [] -> assert false
80   | x :: xs ->
81       List.fold_left (fun xs x -> <:expr< $xs$ ; $x$ >>) x xs
82
83 let concat_record_fields fields =
84   match fields with
85   | [] -> assert false
86   | f :: fs ->
87       List.fold_left (fun fs f -> <:ctyp< $fs$ ; $f$ >>) f fs
88
89 let concat_record_bindings rbs =
90   match rbs with
91   | [] -> assert false
92   | rb :: rbs ->
93       List.fold_left (fun rbs rb -> <:rec_binding< $rbs$ ; $rb$ >>) rb rbs
94
95 let concat_bindings bs =
96   match bs with
97   | [] -> assert false
98   | b :: bs ->
99       List.fold_left (fun bs b -> <:binding< $bs$ and $b$ >>) b bs
100
101 let concat_sum_types ts =
102   match ts with
103   | [] -> assert false
104   | t :: ts ->
105       List.fold_left (fun ts t -> <:ctyp< $ts$ | $t$ >>) t ts
106
107 let build_record rbs =
108   Ast.ExRec (_loc, rbs, Ast.ExNil _loc)
109
110 let build_tuple_from_exprs exprs =
111   match exprs with
112   | [] | [_] -> assert false
113   | x :: xs ->
114       Ast.ExTup (_loc,
115                  List.fold_left (fun xs x -> Ast.ExCom (_loc, x, xs)) x xs)
116
117 let build_tuple_from_patts patts =
118   match patts with
119   | [] | [_] -> assert false
120   | x :: xs ->
121       Ast.PaTup (_loc,
122                  List.fold_left (fun xs x -> Ast.PaCom (_loc, x, xs)) x xs)
123
124 (* Helper functions to store things in a fixed-length tuple very efficiently.
125  * Note that the tuple length must be >= 2.
126  *)
127 type tuple = string list
128
129 let tuple_create fields : tuple = fields
130
131 (* Generates 'let _, _, resultpatt, _ = tupleexpr in body'. *)
132 let tuple_generate_extract fields field resultpatt tupleexpr body =
133   let patts = List.map (
134     fun name -> if name = field then resultpatt else <:patt< _ >>
135   ) fields in
136   let result = build_tuple_from_patts patts in
137   <:expr< let $result$ = $tupleexpr$ in $body$ >>
138
139 (* Generates '(fieldexpr1, fieldexpr2, ...)'. *)
140 let tuple_generate_construct fieldexprs =
141   build_tuple_from_exprs fieldexprs
142
143 type code = Ast.str_item * Ast.sig_item
144
145 let ocaml_type_of_field_type = function
146   | PP.FInteger, true -> <:ctyp< int64 >>
147   | PP.FInteger, false -> <:ctyp< int64 option >>
148   | PP.FString _, true -> <:ctyp< string >>
149   | PP.FString _, false -> <:ctyp< string option >>
150   | PP.FStructPointer _, true | PP.FVoidPointer, true
151   | PP.FAnonListHeadPointer, true | PP.FListHeadPointer _, true ->
152       <:ctyp< Virt_mem_mmap.addr >>
153   | PP.FStructPointer _, false | PP.FVoidPointer, false
154   | PP.FAnonListHeadPointer, false | PP.FListHeadPointer _, false ->
155       <:ctyp< Virt_mem_mmap.addr option >>
156
157 let generate_types xs =
158   let types = List.map (
159     fun (struct_name, all_fields) ->
160       let fields = List.map (
161         fun (name, (typ, always_available)) ->
162           match typ with
163           | PP.FListHeadPointer _ ->
164               (* A list head turns into three fields, the pointer,
165                * the offset within current struct, and the adjustment
166                * (offset within destination struct).
167                *)
168               let t = ocaml_type_of_field_type (typ, always_available) in
169               [ <:ctyp< $lid:struct_name^"_"^name$ : $t$ >>;
170                 <:ctyp< $lid:struct_name^"_"^name^"_offset"$ : int >>;
171                 <:ctyp< $lid:struct_name^"_"^name^"_adjustment"$ : int >> ]
172           | _ ->
173               let t = ocaml_type_of_field_type (typ, always_available) in
174               [ <:ctyp< $lid:struct_name^"_"^name$ : $t$ >> ]
175       ) all_fields in
176       let fields = List.concat fields in
177       let fields = concat_record_fields fields in
178
179       <:str_item<
180         type $lid:struct_name$ = { $fields$ }
181       >>,
182       <:sig_item<
183         type $lid:struct_name$ = { $fields$ }
184       >>
185   ) xs in
186
187   (* Generate a sum-type which can use to store any type of kernel
188    * structure, ie. Task_struct | Net_device | ...
189    *)
190   let types = types @ [
191     let constrs =
192       List.map (
193         fun (struct_name, _) ->
194           let struct_name_uc = String.capitalize struct_name in
195           <:ctyp< $uid:struct_name_uc$ of $lid:struct_name$ >>
196       ) xs in
197     let constrs = concat_sum_types constrs in
198     <:str_item<
199       type kernel_struct = $constrs$
200     >>,
201     <:sig_item<
202       type kernel_struct = $constrs$
203     >>
204   ] in
205
206   let strs, sigs = List.split types in
207   concat_str_items strs, concat_sig_items sigs
208
209 let generate_offsets xs =
210   (* Only need to generate the offset_of_* functions for fields
211    * which are cross-referenced from another field.  Which
212    * ones are those?
213    *)
214   let fields =
215     List.concat (
216       List.map (
217         fun (_, (_, all_fields)) ->
218           List.filter_map (
219             function
220             | (_,
221                (PP.FListHeadPointer
222                   ((Some (struct_name, field_name)) as f),
223                 _)) ->
224                 f
225             | _ ->
226                 None
227           ) all_fields
228       ) xs
229     ) in
230
231   let fields = sort_uniq fields in
232
233   let strs =
234     List.map (
235       fun (struct_name, field_name) ->
236         let kernels, _ =
237           try List.assoc struct_name xs
238           with Not_found ->
239             failwith (
240               sprintf "generate_offsets: structure %s not found. This is probably a list_head-related bug."
241                 struct_name
242             ) in
243         (* Find the offset of this field in each kernel version. *)
244         let offsets =
245           List.filter_map (
246             fun ({ PP.kernel_version = version },
247                  { PP.struct_fields = fields }) ->
248               try
249                 let field =
250                   List.find (fun { PP.field_name = name } -> field_name = name)
251                     fields in
252                 let offset = field.PP.field_offset in
253                 Some (version, offset)
254               with Not_found -> None
255           ) kernels in
256
257         if offsets = [] then
258           failwith (
259             sprintf "generate_offsets: field %s.%s not found in any kernel. This is probably a list_head-related bug."
260               struct_name field_name
261           );
262
263         (* Generate a map of kernel version to offset. *)
264         let map = List.fold_left (
265           fun map (version, offset) ->
266             <:expr< StringMap.add $str:version$ $`int:offset$ $map$ >>
267         ) <:expr< StringMap.empty >> offsets in
268
269         let code =
270           <:str_item<
271             let $lid:"offset_of_"^struct_name^"_"^field_name$ =
272               let map = $map$ in
273               fun kernel_version -> StringMap.find kernel_version map
274           >> in
275         code
276     ) fields in
277
278   let strs = concat_str_items strs in
279
280   strs, <:sig_item< >>
281
282 let generate_parsers xs =
283   let strs =
284     List.map (
285       fun (struct_name, (all_fields, palist)) ->
286         let palist =
287           List.map (
288             fun { MM.pa_name = pa_name } ->
289               <:str_item<
290                 let $lid:pa_name$ kernel_version bits = $str:pa_name$
291               >>
292           ) palist in
293         concat_str_items palist
294     ) xs in
295
296   let strs = concat_str_items strs in
297
298   (* The shared parser functions.
299    * 
300    * We could include bitmatch statements directly in here, but
301    * what happens is that the macros get expanded here, resulting
302    * in (even more) unreadable generated code.  So instead just
303    * do a textual substitution later by post-processing the
304    * generated files.  Not type-safe, but we can't have
305    * everything.
306    *)
307   let subs = Hashtbl.create 13 in
308   List.iter (
309     fun (struct_name, (all_fields, palist)) ->
310       List.iter (
311         fun ({ MM.pa_name = pa_name;
312                pa_endian = endian; pa_structure = structure }) ->
313           (* Generate the code to match this structure. *)
314           let endian =
315             match endian with
316             | Bitstring.LittleEndian -> "littleendian"
317             | Bitstring.BigEndian -> "bigendian"
318             | _ -> assert false in
319           let patterns =
320             String.concat ";\n      " (
321               List.map (
322                 function
323                 | { PP.field_name = field_name;
324                     field_type = PP.FInteger;
325                     field_offset = offset;
326                     field_size = size } ->
327                     (* 'zero+' is a hack to force the type to int64. *)
328                     sprintf "%s : zero+%d : offset(%d), %s"
329                       field_name (size*8) (offset*8) endian
330
331                 | { PP.field_name = field_name;
332                     field_type = (PP.FStructPointer _
333                                   | PP.FVoidPointer
334                                   | PP.FAnonListHeadPointer
335                                   | PP.FListHeadPointer _);
336                     field_offset = offset;
337                     field_size = size } ->
338                     sprintf "%s : zero+%d : offset(%d), %s"
339                       field_name (size*8) (offset*8) endian
340
341                 | { PP.field_name = field_name;
342                     field_type = PP.FString width;
343                     field_offset = offset;
344                     field_size = size } ->
345                     sprintf "%s : %d : offset(%d), string"
346                       field_name (width*8) (offset*8)
347               ) structure.PP.struct_fields
348             ) in
349
350           let assignments =
351             List.map (
352               fun (field_name, (field_type, always_available)) ->
353                 if always_available then (
354                   (* Go and look up the field offset in the correct kernel. *)
355                   let { PP.field_offset = offset } =
356                     List.find (fun { PP.field_name = name } ->
357                                  field_name = name)
358                       structure.PP.struct_fields in
359
360                   (* Generate assignment code.  List_heads are treated
361                    * specially because they have an implicit adjustment.
362                    *)
363                   match field_type with
364                   | PP.FListHeadPointer None ->
365                       sprintf "%s_%s = %s;
366           %s_%s_offset = %d;
367           %s_%s_adjustment = %d"
368                         struct_name field_name field_name
369                         struct_name field_name offset
370                         struct_name field_name offset
371
372                   | PP.FListHeadPointer (Some (other_struct_name,
373                                                other_field_name)) ->
374                       (* A reference to a field in another structure.  We don't
375                        * know the offset until runtime, so we have to call
376                        * offset_of_<struct>_<field> to find it.
377                        *)
378                       sprintf "%s_%s = %s;
379           %s_%s_offset = %d;
380           %s_%s_adjustment = offset_of_%s_%s kernel_version"
381                         struct_name field_name field_name
382                         struct_name field_name offset (* in this struct *)
383                         struct_name field_name        (* ... & in other struct*)
384                           other_struct_name other_field_name
385
386                   | _ ->
387                       sprintf "%s_%s = %s" struct_name field_name field_name
388                 ) else (
389                   (* Field is optional.  Is it available in this kernel
390                    * version?  If so, get its offset, else throw Not_found.
391                    *)
392                   try
393                     let { PP.field_offset = offset } =
394                       List.find (fun { PP.field_name = name } ->
395                                    field_name = name)
396                         structure.PP.struct_fields in
397
398                     (* Generate assignment code.  List_heads are treated
399                      * specially because they have an implicit adjustment.
400                      *)
401                     match field_type with
402                     | PP.FListHeadPointer None ->
403                         sprintf "%s_%s = Some %s;
404           %s_%s_offset = %d;
405           %s_%s_adjustment = %d"
406                           struct_name field_name field_name
407                           struct_name field_name offset
408                           struct_name field_name offset
409
410                     | PP.FListHeadPointer (Some (other_struct_name,
411                                                  other_field_name)) ->
412                         (* A reference to a field in another structure.  We
413                          * don't know the offset until runtime, so we have
414                          * to call offset_of_<struct>_<field> to find it.
415                          *)
416                         sprintf "%s_%s = Some %s;
417           %s_%s_offset = %d;
418           %s_%s_adjustment = offset_of_%s_%s kernel_version"
419                           struct_name field_name field_name
420                           struct_name field_name offset(*in this struct *)
421                           struct_name field_name       (*... & in other struct*)
422                             other_struct_name other_field_name
423
424                     | _ ->
425                         sprintf "%s_%s = Some %s"
426                           struct_name field_name field_name
427                   with
428                     Not_found ->
429                       (* Field is not available in this kernel version. *)
430                       match field_type with
431                       | PP.FListHeadPointer _ ->
432                           sprintf "%s_%s = None;
433           %s_%s_offset = -1;
434           %s_%s_adjustment = -1"
435                             struct_name field_name
436                             struct_name field_name
437                             struct_name field_name
438                       | _ ->
439                           sprintf "%s_%s = None" struct_name field_name
440                 )
441             ) all_fields in
442
443           let assignments = String.concat ";\n        " assignments in
444
445           let code =
446             sprintf "
447   bitmatch bits with
448   | { %s } ->
449       { %s }
450   | { _ } ->
451       raise (ParseError (%S, %S, match_err))"
452               patterns assignments
453               struct_name pa_name in
454
455           Hashtbl.add subs pa_name code
456       ) palist;
457   ) xs;
458
459   (strs, <:sig_item< >>), subs
460
461 let generate_version_maps xs =
462   (* size_of_<struct> kernel_version *)
463   let strs = List.map (
464     fun (struct_name, (kernels, _)) ->
465       let map =
466         List.fold_right (
467           fun ({ PP.kernel_version = version },
468                { PP.struct_total_size = size }) map ->
469             <:expr<
470               StringMap.add $str:version$ $`int:size$ $map$
471             >>
472         ) kernels <:expr< StringMap.empty >> in
473
474       <:str_item<
475         let $lid:"size_of_"^struct_name$ =
476           let map = $map$ in
477           fun kernel_version ->
478             try StringMap.find kernel_version map
479             with Not_found ->
480               unknown_kernel_version kernel_version $str:struct_name$
481       >>
482   ) xs in
483
484   (* parser_of_<struct> kernel_version *)
485   let strs = strs @ List.map (
486     fun (struct_name, (kernels, pahash)) ->
487       let map =
488         List.fold_right (
489           fun ({ PP.kernel_version = version }, _) map ->
490             let { MM.pa_name = pa_name } = Hashtbl.find pahash version in
491             <:expr<
492               StringMap.add $str:version$ $lid:pa_name$ $map$
493             >>
494         ) kernels <:expr< StringMap.empty >> in
495
496       <:str_item<
497         let $lid:"parser_of_"^struct_name$ =
498           let map = $map$ in
499           fun kernel_version ->
500             try StringMap.find kernel_version map
501             with Not_found ->
502               unknown_kernel_version kernel_version $str:struct_name$
503       >>
504   ) xs in
505
506   concat_str_items strs, <:sig_item< >>
507
508 let generate_followers names xs =
509   (* A follower function for every structure. *)
510   let bindings = List.map (
511     fun (struct_name, all_fields) ->
512       let followers = List.fold_right (
513         fun (name, (typ, always_available)) rest ->
514           let is_shape_field =
515             match typ with
516             | PP.FListHeadPointer None -> true
517             | PP.FListHeadPointer (Some (struct_name, _))
518             | PP.FStructPointer struct_name
519                 when List.mem struct_name names -> true
520             | _ -> false in
521           if not is_shape_field then rest
522           else (
523             let dest_struct_name =
524               match typ with
525               | PP.FListHeadPointer None -> struct_name
526               | PP.FListHeadPointer (Some (struct_name, _)) -> struct_name
527               | PP.FStructPointer struct_name -> struct_name
528               | _ -> assert false in
529
530             let body =
531               match typ with
532               | PP.FListHeadPointer _ ->
533                   <:expr<
534                     (* For list head pointers, add the address of the base
535                      * of this virtual structure to the map, then adjust
536                      * the pointer.
537                      *)
538                     let offset = data.$lid:struct_name^"_"^name^"_offset"$
539                     and adj = data.$lid:struct_name^"_"^name^"_adjustment"$ in
540                     let offset = Int64.of_int offset
541                     and adj = Int64.of_int adj in
542                     (* 'addr' is base of the virtual struct *)
543                     let addr = Int64.sub (Int64.add addr offset) adj in
544                     let map =
545                       AddrMap.add addr ($str:dest_struct_name$, None) map in
546                     let dest_addr = Int64.sub dest_addr adj in
547                     let map =
548                       $lid:dest_struct_name^"_follower"$
549                         kernel_version load map dest_addr in
550                     map
551                   >>
552
553               | PP.FStructPointer _ ->
554                   <:expr<
555                     let map =
556                       $lid:dest_struct_name^"_follower"$
557                         kernel_version load map dest_addr in
558                     map
559                   >>
560
561               | _ -> assert false in
562
563             if always_available then
564               <:expr<
565                 let dest_addr = data.$lid:struct_name^"_"^name$ in
566                 let map = $body$ in
567                 $rest$
568               >>
569             else
570               <:expr<
571                 let map =
572                   match data.$lid:struct_name^"_"^name$ with
573                   | None -> map
574                   | Some dest_addr -> $body$ in
575                 $rest$
576               >>
577           )
578       ) all_fields <:expr< map >> in
579
580       let struct_name_uc = String.capitalize struct_name in
581
582       <:binding<
583         $lid:struct_name^"_follower"$ kernel_version load map addr =
584           if addr <> 0L && not (AddrMap.mem addr map) then (
585             let parser_ = $lid:"parser_of_"^struct_name$ kernel_version in
586             let total_size = $lid:"size_of_"^struct_name$ kernel_version in
587             let bits = load $str:struct_name$ addr total_size in
588             let data = parser_ kernel_version bits in
589             let map = AddrMap.add
590               addr ($str:struct_name$,
591                     Some (total_size, bits, $uid:struct_name_uc$ data))
592               map in
593             $followers$
594           )
595           else map
596       >>
597   ) xs in
598   let bindings = concat_bindings bindings in
599   let strs = <:str_item< let rec $bindings$ >> in
600
601   (* Function signatures for the interface. *)
602   let sigs = List.map (
603     fun (struct_name, _) ->
604       <:sig_item<
605         val $lid:struct_name^"_follower"$ :
606           kernel_version -> load_fn -> addrmap -> Virt_mem_mmap.addr ->
607           addrmap
608       >>
609   ) xs in
610   let sigs = concat_sig_items sigs in
611
612   strs, sigs
613
614 let output_interf ~output_file types offsets parsers version_maps followers =
615   (* Some standard code that appears at the top and bottom
616    * of the interface file.
617    *)
618   let prologue =
619     <:sig_item<
620       module AddrMap : sig
621         type key = Virt_mem_mmap.addr
622         type 'a t = 'a Map.Make(Int64).t
623         val empty : 'a t
624         val is_empty : 'a t -> bool
625         val add : key -> 'a -> 'a t -> 'a t
626         val find : key -> 'a t -> 'a
627         val remove : key -> 'a t -> 'a t
628         val mem : key -> 'a t -> bool
629         val iter : (key -> 'a -> unit) -> 'a t -> unit
630         val map : ('a -> 'b) -> 'a t -> 'b t
631         val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
632         val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
633         val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
634         val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
635       end ;;
636       exception ParseError of string * string * string ;;
637
638       type kernel_version = string
639
640       type load_fn =
641           string -> Virt_mem_mmap.addr -> int -> Bitstring.bitstring
642     >>
643   and addrmap =
644     <:sig_item<
645       type addrmap =
646           (string * (int * Bitstring.bitstring * kernel_struct) option)
647             AddrMap.t
648     >> in
649
650   let sigs =
651     concat_sig_items [ prologue;
652                        types;
653                        addrmap;
654                        offsets;
655                        parsers;
656                        version_maps;
657                        followers ] in
658   Printers.OCaml.print_interf ~output_file sigs;
659
660   ignore (Sys.command (sprintf "wc -l %s" (Filename.quote output_file)))
661
662 (* Finally generate the output files. *)
663 let re_subst = Pcre.regexp "^(.*)\"(\\w+_parser_\\d+)\"(.*)$"
664
665 let output_implem ~output_file types offsets parsers parser_subs
666     version_maps followers =
667   (* Some standard code that appears at the top and bottom
668    * of the implementation file.
669    *)
670   let prologue =
671     <:str_item<
672       open Printf ;;
673       module StringMap = Map.Make (String) ;;
674       module AddrMap = Map.Make (Int64) ;;
675       exception ParseError of string * string * string ;;
676
677       let match_err = "failed to match kernel structure"
678
679       let unknown_kernel_version version struct_name =
680         invalid_arg (sprintf "%s: unknown kernel version or
681 struct %s is not supported in this kernel.
682 Try a newer version of virt-mem, or if the guest is not from a
683 supported Linux distribution, see this page about adding support:
684   http://et.redhat.com/~rjones/virt-mem/faq.html\n"
685                        version struct_name)
686
687       type kernel_version = string
688       type load_fn = string -> Virt_mem_mmap.addr -> int -> Bitstring.bitstring
689
690       let zero = 0
691     >>
692   and addrmap =
693     <:str_item<
694       type addrmap =
695           (string * (int * Bitstring.bitstring * kernel_struct) option)
696             AddrMap.t
697     >> in
698
699   let strs =
700     concat_str_items [ prologue;
701                        types;
702                        addrmap;
703                        offsets;
704                        parsers;
705                        version_maps;
706                        followers ] in
707
708   (* Write the new implementation to .ml.new file. *)
709   let new_output_file = output_file ^ ".new" in
710   Printers.OCaml.print_implem ~output_file:new_output_file strs;
711
712   (* Substitute the parser bodies in the output file. *)
713   let ichan = open_in new_output_file in
714   let ochan = open_out output_file in
715
716   output_string ochan "\
717 (* WARNING: This file and the corresponding mli (interface) are
718  * automatically generated by the extract/codegen/ program.
719  *
720  * Any edits you make to this file will be lost.
721  *
722  * To update this file from the latest kernel database, it is recommended
723  * that you do 'make update-kernel-structs'.
724  *)\n\n";
725
726   let rec loop () =
727     let line = input_line ichan in
728     let line =
729       if Pcre.pmatch ~rex:re_subst line then (
730         let subs = Pcre.exec ~rex:re_subst line in
731         let start = Pcre.get_substring subs 1 in
732         let template = Pcre.get_substring subs 2 in
733         let rest = Pcre.get_substring subs 3 in
734         let sub =
735           try Hashtbl.find parser_subs template
736           with Not_found -> assert false in
737         start ^ sub ^ rest
738       ) else line in
739     output_string ochan line; output_char ochan '\n';
740     loop ()
741   in
742   (try loop () with End_of_file -> ());
743
744   close_out ochan;
745   close_in ichan;
746
747   Unix.unlink new_output_file;
748
749   ignore (Sys.command (sprintf "wc -l %s" (Filename.quote output_file)))