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