File: z_pp.ml

package info (click to toggle)
ocaml-zarith 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 404 kB
  • ctags: 540
  • sloc: ansic: 2,346; sh: 1,539; ml: 1,209; asm: 972; perl: 40; makefile: 28
file content (77 lines) | stat: -rw-r--r-- 1,785 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
let archname = ref ""
let usage = "Usage: './z_pp architecture"

let () =
  Arg.parse 
    [] (* no options *)
    (fun name -> archname := name)
    usage;
  if !archname = "" 
  then begin
      print_endline usage; 
      exit 1
    end

let asmfilename = "caml_z_" ^ !archname ^ ".S"

module StringSet = Set.Make(String)

let funcnames = ref StringSet.empty

let () =
  let rPROLOG = Str.regexp "[ 	]*PROLOG(\\([^)]*\\))" in
  let input = open_in asmfilename in
  Printf.printf "found assembly file %s\n" asmfilename;
  try
    while true do
      let s = input_line input in
      if Str.string_match rPROLOG s 0
      then
	let funcname = Str.matched_group 1 s in
	Printf.printf "  found %s\n" funcname;
	funcnames := StringSet.add funcname !funcnames
    done
  with
    End_of_file -> 
      close_in input


let treat_file =
  let rASM = Str.regexp "\\(.*\\) \\([A-Za-z0-9_]+\\)@ASM\\(.*\\)" in
  let funcnames = !funcnames in
  function extension ->
    let outputname = "z." ^ extension in
    let inputname = outputname ^ "p" in

    let input = open_in inputname in
    let output = open_out outputname in
    Printf.fprintf output
      "(* This file was automatically generated by z_pp.ml from %s *)\n"
      inputname;
    try
      while true do
	let line_in = input_line input in
	let line_out = 
	  if Str.string_match rASM line_in 0
	  then
	    let funcname = Str.matched_group 2 line_in in
	    if StringSet.mem funcname funcnames
	    then Str.replace_matched
	      "\\1 \"ml_z_\\2\" \"ml_as_z_\\2\"\\3"
	      line_in
	    else
	      Str.replace_matched
		"\\1 \"ml_z_\\2\"\\3"
		line_in
	  else line_in
	in
	Printf.fprintf output "%s\n" line_out
      done
    with
      End_of_file -> 
	close_in input
;;

let _ = treat_file "ml"
let _ = treat_file "mli"