package / ocaml-base-compiler.4.10.0 / tools / make_opcodes.mll
1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Nicolas Ojeda Bar, LexiFi *)
6 (* *)
7 (* Copyright 2016 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 let ident = ['a'-'z''A'-'Z''_']['a'-'z''A'-'Z''0'-'9''_']*
17 let space = [' ''\n''\r''\t']*
18
19 rule find_enum = parse
20 | "enum" space (ident as id) space '{' { id, opnames lexbuf }
21 | _ { find_enum lexbuf }
22
23 and opnames = parse
24 | space (ident as op) space ',' { op :: opnames lexbuf }
25 | space ident space '}' { [] }
26
27 {
28 let print_opnames = ref false
29 let print_opcodes = ref false
30
31 open Printf
32
33 let () =
34 let spec =
35 [
36 "-opnames", Arg.Set print_opnames, " Dump opcode names";
37 "-opcodes", Arg.Set print_opcodes, " Dump opcode numbers";
38 ]
39 in
40 Arg.parse (Arg.align spec) ignore "Extract opcode info from instruct.h";
41 let lexbuf = Lexing.from_channel stdin in
42 let id, opnames = find_enum lexbuf in
43 if !print_opnames then begin
44 printf "let names_of_%s = [|\n" id;
45 List.iter (fun s -> printf " %S;\n" s) opnames;
46 printf "|]\n"
47 end;
48 if !print_opcodes then
49 List.iteri (fun i op -> printf "let op%s = %i\n" op i) opnames
50 }
51