src/Pure/RAW/compiler_polyml.ML
author wenzelm
Tue, 01 Mar 2016 19:42:59 +0100
changeset 62490 39d01eaf5292
parent 62489 36f11bc393a2
child 62493 dd154240a53c
permissions -rw-r--r--
ML debugger support in Pure (again, see 3565c9f407ec);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61925
ab52f183f020 clarified directory structure;
wenzelm
parents: 60956
diff changeset
     1
(*  Title:      Pure/RAW/compiler_polyml.ML
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     2
50910
54f06ba192ef tuned comments;
wenzelm
parents: 47980
diff changeset
     3
Basic runtime compilation for Poly/ML (cf. Pure/ML/ml_compiler_polyml.ML).
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     4
*)
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     5
62490
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
     6
type use_context =
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
     7
 {name_space: ML_Name_Space.T,
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
     8
  str_of_pos: int -> string -> string,
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
     9
  print: string -> unit,
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    10
  error: string -> unit};
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    11
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    12
local
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    13
62490
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    14
val boot_context: use_context =
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    15
 {name_space = ML_Name_Space.global,
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    16
  str_of_pos = fn line => fn file => " (line " ^ Int.toString line ^ " of \"" ^ file ^ "\")",
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    17
  print = fn s => (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut),
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    18
  error = fn s => raise Fail s};
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    19
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    20
fun drop_newline s =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    21
  if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    22
  else s;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    23
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    24
in
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    25
62354
fdd6989cc8a0 SML/NJ is no longer supported;
wenzelm
parents: 61925
diff changeset
    26
fun use_text ({name_space, str_of_pos, print, error, ...}: use_context)
60956
10d463883dc2 explicit debug flag for ML compiler;
wenzelm
parents: 60955
diff changeset
    27
    {line, file, verbose, debug} text =
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    28
  let
60955
wenzelm
parents: 56435
diff changeset
    29
    val current_line = Unsynchronized.ref line;
62354
fdd6989cc8a0 SML/NJ is no longer supported;
wenzelm
parents: 61925
diff changeset
    30
    val in_buffer = Unsynchronized.ref (String.explode (ml_positions line file text));
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31480
diff changeset
    31
    val out_buffer = Unsynchronized.ref ([]: string list);
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    32
    fun output () = drop_newline (implode (rev (! out_buffer)));
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    33
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    34
    fun get () =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    35
      (case ! in_buffer of
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    36
        [] => NONE
60955
wenzelm
parents: 56435
diff changeset
    37
      | c :: cs =>
wenzelm
parents: 56435
diff changeset
    38
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    39
    fun put s = out_buffer := s :: ! out_buffer;
60955
wenzelm
parents: 56435
diff changeset
    40
    fun put_message {message = msg1, hard, location = {startLine = message_line, ...}, context} =
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    41
     (put (if hard then "Error: " else "Warning: ");
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    42
      PolyML.prettyPrint (put, 76) msg1;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    43
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 62354
diff changeset
    44
      put ("At" ^ str_of_pos (FixedInt.toInt message_line) file ^ "\n"));
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    45
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    46
    val parameters =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    47
     [PolyML.Compiler.CPOutStream put,
31471
e3987b32e401 use_text: pass file name to compiler, tuned;
wenzelm
parents: 31433
diff changeset
    48
      PolyML.Compiler.CPNameSpace name_space,
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    49
      PolyML.Compiler.CPErrorMessageProc put_message,
60955
wenzelm
parents: 56435
diff changeset
    50
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
wenzelm
parents: 56435
diff changeset
    51
      PolyML.Compiler.CPFileName file,
60956
10d463883dc2 explicit debug flag for ML compiler;
wenzelm
parents: 60955
diff changeset
    52
      PolyML.Compiler.CPPrintInAlphabeticalOrder false] @
10d463883dc2 explicit debug flag for ML compiler;
wenzelm
parents: 60955
diff changeset
    53
      ML_Compiler_Parameters.debug debug;
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    54
    val _ =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    55
      (while not (List.null (! in_buffer)) do
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    56
        PolyML.compiler (get, parameters) ())
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    57
      handle exn =>
39242
28d3809ff91f primitive use_text: let interrupts pass unhindered;
wenzelm
parents: 38470
diff changeset
    58
        if Exn.is_interrupt exn then reraise exn
28d3809ff91f primitive use_text: let interrupts pass unhindered;
wenzelm
parents: 38470
diff changeset
    59
        else
28d3809ff91f primitive use_text: let interrupts pass unhindered;
wenzelm
parents: 38470
diff changeset
    60
         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
28d3809ff91f primitive use_text: let interrupts pass unhindered;
wenzelm
parents: 38470
diff changeset
    61
          error (output ()); reraise exn);
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    62
  in if verbose then print (output ()) else () end;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    63
60956
10d463883dc2 explicit debug flag for ML compiler;
wenzelm
parents: 60955
diff changeset
    64
fun use_file context {verbose, debug} file =
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    65
  let
60955
wenzelm
parents: 56435
diff changeset
    66
    val instream = TextIO.openIn file;
60956
10d463883dc2 explicit debug flag for ML compiler;
wenzelm
parents: 60955
diff changeset
    67
    val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
10d463883dc2 explicit debug flag for ML compiler;
wenzelm
parents: 60955
diff changeset
    68
  in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end;
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    69
62490
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    70
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    71
fun use_debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true"
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    72
  | use_debug_option (SOME debug) = debug;
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    73
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    74
local
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    75
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    76
fun use_ opt_debug file =
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    77
  use_file boot_context {verbose = true, debug = use_debug_option opt_debug} file
62489
36f11bc393a2 use bootstrap compiler earlier;
wenzelm
parents: 62387
diff changeset
    78
    handle Fail msg => (#print boot_context msg; raise Fail "ML error");
36f11bc393a2 use bootstrap compiler earlier;
wenzelm
parents: 62387
diff changeset
    79
62490
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    80
in
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    81
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    82
val use = use_ NONE;
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    83
val use_debug = use_ (SOME true);
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    84
val use_no_debug = use_ (SOME false);
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    85
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    86
end;
62490
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    87
39d01eaf5292 ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents: 62489
diff changeset
    88
end;