src/Pure/ML-Systems/polyml.ML
author wenzelm
Thu Oct 04 21:10:41 2007 +0200 (2007-10-04)
changeset 24851 4e304aac841a
parent 24688 a5754ca5c510
child 25023 52eb78ebb370
permissions -rw-r--r--
tuned;
wenzelm@2341
     1
(*  Title:      Pure/ML-Systems/polyml.ML
wenzelm@2341
     2
    ID:         $Id$
wenzelm@2341
     3
wenzelm@23139
     4
Compatibility file for Poly/ML (version 4.1.x and 4.2.0).
wenzelm@2341
     5
*)
wenzelm@2341
     6
wenzelm@23965
     7
use "ML-Systems/exn.ML";
wenzelm@24688
     8
use "ML-Systems/multithreading.ML";
wenzelm@24688
     9
use "ML-Systems/time_limit.ML";
wenzelm@23921
    10
wenzelm@24598
    11
val ml_system_fix_ints = false;
wenzelm@24598
    12
wenzelm@23921
    13
wenzelm@16374
    14
(** ML system and platform related **)
wenzelm@16374
    15
wenzelm@17077
    16
(* String compatibility *)
wenzelm@17077
    17
wenzelm@16516
    18
(*low-level pointer equality*)
wenzelm@16516
    19
val pointer_eq = Address.wordEq;
wenzelm@16502
    20
wenzelm@16502
    21
wenzelm@11078
    22
(* old Poly/ML emulation *)
wenzelm@2341
    23
wenzelm@11078
    24
local
wenzelm@11078
    25
  val orig_exit = exit;
wenzelm@11078
    26
in
wenzelm@11078
    27
  open PolyML;
wenzelm@11078
    28
  val exit = orig_exit;
wenzelm@11078
    29
  fun quit () = exit 0;
wenzelm@7148
    30
end;
paulson@6042
    31
wenzelm@2341
    32
wenzelm@11078
    33
(* restore old-style character / string functions *)
wenzelm@3588
    34
wenzelm@11078
    35
val ord = SML90.ord;
wenzelm@11078
    36
val chr = SML90.chr;
wenzelm@11078
    37
val explode = SML90.explode;
wenzelm@11078
    38
val implode = SML90.implode;
wenzelm@11078
    39
wenzelm@14870
    40
skalberg@14519
    41
(* compiler-independent timing functions *)
wenzelm@2341
    42
wenzelm@21298
    43
fun start_timing () =
wenzelm@21298
    44
  let val CPUtimer = Timer.startCPUTimer();
wenzelm@21298
    45
      val time = Timer.checkCPUTimer(CPUtimer)
wenzelm@21298
    46
  in  (CPUtimer,time)  end;
wenzelm@21298
    47
wenzelm@21298
    48
fun end_timing (CPUtimer, {sys,usr}) =
wenzelm@21298
    49
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
wenzelm@21298
    50
      val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
wenzelm@21298
    51
  in  "User " ^ toString (usr2-usr) ^
wenzelm@21298
    52
      "  All "^ toString (sys2-sys + usr2-usr) ^
wenzelm@21298
    53
      " secs"
wenzelm@21298
    54
      handle Time => ""
wenzelm@21298
    55
  end;
wenzelm@21298
    56
wenzelm@21298
    57
fun check_timer timer =
wenzelm@21298
    58
  let
wenzelm@21298
    59
    val {sys, usr} = Timer.checkCPUTimer timer;
wenzelm@21298
    60
    val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
wenzelm@21298
    61
  in (sys, usr, gc) end;
wenzelm@2341
    62
wenzelm@14870
    63
wenzelm@4977
    64
(* prompts *)
wenzelm@4977
    65
wenzelm@4984
    66
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
wenzelm@4977
    67
wenzelm@4977
    68
wenzelm@24290
    69
(* toplevel pretty printing (see also Pure/pure_setup.ML) *)
wenzelm@2341
    70
wenzelm@11078
    71
fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
wenzelm@2341
    72
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
wenzelm@2341
    73
    fn () => brk (99999, 0), en);
wenzelm@2341
    74
wenzelm@24329
    75
(*print depth*)
wenzelm@24329
    76
local
wenzelm@24329
    77
  val depth = ref 10;
wenzelm@24329
    78
in
wenzelm@24329
    79
  fun get_print_depth () = ! depth;
wenzelm@24329
    80
  fun print_depth n = (depth := n; PolyML.print_depth n);
wenzelm@24329
    81
end;
wenzelm@24329
    82
wenzelm@2341
    83
wenzelm@3588
    84
(* ML command execution -- 'eval' *)
wenzelm@3588
    85
wenzelm@24598
    86
fun use_text (tune: string -> string) name (print, err) verbose txt =
wenzelm@3588
    87
  let
wenzelm@24598
    88
    val in_buffer = ref (explode (tune txt));
wenzelm@5090
    89
    val out_buffer = ref ([]: string list);
wenzelm@21715
    90
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));
wenzelm@5090
    91
wenzelm@5038
    92
    fun get () =
wenzelm@5090
    93
      (case ! in_buffer of
wenzelm@5038
    94
        [] => ""
wenzelm@5090
    95
      | c :: cs => (in_buffer := cs; c));
wenzelm@5090
    96
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@5090
    97
wenzelm@5038
    98
    fun exec () =
wenzelm@5090
    99
      (case ! in_buffer of
wenzelm@5038
   100
        [] => ()
wenzelm@5090
   101
      | _ => (PolyML.compiler (get, put) (); exec ()));
wenzelm@5090
   102
  in
wenzelm@22144
   103
    exec () handle exn =>
wenzelm@22144
   104
      (err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
wenzelm@10914
   105
    if verbose then print (output ()) else ()
wenzelm@5090
   106
  end;
wenzelm@3588
   107
wenzelm@24598
   108
fun use_file tune output verbose name =
wenzelm@24598
   109
  let
wenzelm@24598
   110
    val instream = TextIO.openIn name;
wenzelm@24598
   111
    val txt = TextIO.inputAll instream before TextIO.closeIn instream;
wenzelm@24598
   112
  in use_text tune name output verbose txt end;
wenzelm@21770
   113
wenzelm@11078
   114
wenzelm@15832
   115
(*eval command line arguments*)
wenzelm@15832
   116
local
wenzelm@15832
   117
  fun println s =
wenzelm@15832
   118
    (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut);
wenzelm@21715
   119
  fun eval "-q" = ()
wenzelm@24598
   120
    | eval txt = use_text (fn x => x) "" (println, println) false txt;
wenzelm@15832
   121
in
wenzelm@24296
   122
  val _ = PolyML.onEntry (fn () =>
wenzelm@24296
   123
   (Signal.signal (2, Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ()));
wenzelm@24296
   124
    app eval (CommandLine.arguments ())));
wenzelm@15832
   125
end;
wenzelm@15832
   126
wenzelm@15832
   127
wenzelm@11078
   128
wenzelm@11078
   129
(** interrupts **)
wenzelm@11078
   130
wenzelm@11078
   131
exception Interrupt = SML90.Interrupt;
wenzelm@11078
   132
wenzelm@11078
   133
local
wenzelm@11078
   134
wenzelm@11078
   135
val sig_int = 2;
wenzelm@11078
   136
wenzelm@11078
   137
fun change_signal new_handler f x =
wenzelm@11078
   138
  let
wenzelm@11078
   139
    (*RACE wrt. other signals*)
wenzelm@11078
   140
    val old_handler = Signal.signal (sig_int, new_handler);
wenzelm@23965
   141
    val result = Exn.capture f x;
wenzelm@11078
   142
    val _ = Signal.signal (sig_int, old_handler);
wenzelm@23965
   143
  in Exn.release result end;
wenzelm@5813
   144
wenzelm@11078
   145
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ());
wenzelm@11078
   146
wenzelm@11078
   147
in
wenzelm@11078
   148
wenzelm@11078
   149
val _ = Signal.signal (sig_int, default_handler);
wenzelm@11078
   150
wenzelm@12989
   151
fun ignore_interrupt f = change_signal Signal.SIG_IGN f;
wenzelm@12989
   152
fun raise_interrupt f = change_signal default_handler f;
wenzelm@11078
   153
wenzelm@11078
   154
end;
wenzelm@5813
   155
wenzelm@5813
   156
wenzelm@5813
   157
wenzelm@3588
   158
(** OS related **)
wenzelm@2341
   159
wenzelm@21281
   160
use "ML-Systems/polyml-posix.ML";
wenzelm@16659
   161
wenzelm@16659
   162
wenzelm@23826
   163
(* current directory *)
wenzelm@23826
   164
wenzelm@23826
   165
val cd = OS.FileSys.chDir;
wenzelm@23826
   166
val pwd = OS.FileSys.getDir;
wenzelm@23826
   167
wenzelm@23826
   168
wenzelm@3588
   169
(* system command execution *)
wenzelm@2341
   170
wenzelm@3588
   171
(*execute Unix command which doesn't take any input from stdin and
wenzelm@11078
   172
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@11078
   173
  but that function doesn't use the PATH*)
wenzelm@3588
   174
fun execute command =
wenzelm@3588
   175
  let
wenzelm@11078
   176
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@11078
   177
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@11078
   178
    val result = TextIO.inputAll is;
wenzelm@11078
   179
  in
wenzelm@11078
   180
    TextIO.closeIn is;
wenzelm@11078
   181
    OS.FileSys.remove tmp_name;
wenzelm@11078
   182
    result
wenzelm@11078
   183
  end;
wenzelm@7855
   184
wenzelm@11078
   185
(*plain version; with return code*)
wenzelm@11078
   186
fun system cmd =
wenzelm@11078
   187
  if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1;
wenzelm@2341
   188
wenzelm@2341
   189
wenzelm@17824
   190
(*Convert a process ID to a decimal string (chiefly for tracing)*)
wenzelm@17824
   191
fun string_of_pid pid = 
wenzelm@23139
   192
  Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
wenzelm@17824
   193
wenzelm@17824
   194
wenzelm@3588
   195
(* getenv *)
wenzelm@3588
   196
wenzelm@11078
   197
fun getenv var =
wenzelm@11078
   198
  (case OS.Process.getEnv var of
wenzelm@11078
   199
    NONE => ""
wenzelm@11078
   200
  | SOME txt => txt);
paulson@15028
   201
paulson@15028
   202
wenzelm@16659
   203
(* profile execution *)
wenzelm@16659
   204
wenzelm@16659
   205
fun profile 0 f x = f x
wenzelm@16659
   206
  | profile n f x =
wenzelm@24851
   207
      let
wenzelm@24851
   208
        val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n;
wenzelm@24851
   209
        val res = Exn.capture f x;
wenzelm@24851
   210
        val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0;
wenzelm@24851
   211
      in Exn.release res end;
wenzelm@16659
   212