src/Pure/ML-Systems/polyml_common.ML
author wenzelm
Thu Sep 04 16:03:47 2008 +0200 (2008-09-04)
changeset 28124 10a1f1f4c6ae
parent 26474 94735cff132c
child 28151 61f9c918b410
permissions -rw-r--r--
moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm@26215
     1
(*  Title:      Pure/ML-Systems/polyml_common.ML
wenzelm@26215
     2
    ID:         $Id$
wenzelm@26215
     3
wenzelm@26215
     4
Compatibility file for Poly/ML -- common part for 4.x and 5.x.
wenzelm@26215
     5
*)
wenzelm@26215
     6
wenzelm@26215
     7
use "ML-Systems/exn.ML";
wenzelm@26215
     8
if List.exists (fn s => s = "Universal") (PolyML.Compiler.structureNames ()) then ()
wenzelm@26215
     9
else use "ML-Systems/universal.ML";
wenzelm@26215
    10
use "ML-Systems/multithreading.ML";
wenzelm@26215
    11
use "ML-Systems/time_limit.ML";
wenzelm@26220
    12
use "ML-Systems/system_shell.ML";
wenzelm@26220
    13
wenzelm@26215
    14
wenzelm@26215
    15
wenzelm@26215
    16
(** ML system and platform related **)
wenzelm@26215
    17
wenzelm@26474
    18
val forget_structure = PolyML.Compiler.forgetStructure;
wenzelm@26474
    19
wenzelm@26474
    20
wenzelm@26215
    21
(* Compiler options *)
wenzelm@26215
    22
wenzelm@26215
    23
val ml_system_fix_ints = false;
wenzelm@26215
    24
wenzelm@26215
    25
PolyML.Compiler.printInAlphabeticalOrder := false;
wenzelm@26215
    26
PolyML.Compiler.maxInlineSize := 80;
wenzelm@26215
    27
wenzelm@26215
    28
wenzelm@26215
    29
(* old Poly/ML emulation *)
wenzelm@26215
    30
wenzelm@26215
    31
local
wenzelm@26215
    32
  val orig_exit = exit;
wenzelm@26215
    33
in
wenzelm@26215
    34
  open PolyML;
wenzelm@26215
    35
  val exit = orig_exit;
wenzelm@26215
    36
  fun quit () = exit 0;
wenzelm@26215
    37
end;
wenzelm@26215
    38
wenzelm@26215
    39
wenzelm@26215
    40
(* restore old-style character / string functions *)
wenzelm@26215
    41
wenzelm@26215
    42
val ord = SML90.ord;
wenzelm@26215
    43
val chr = SML90.chr;
wenzelm@26215
    44
val explode = SML90.explode;
wenzelm@26215
    45
val implode = SML90.implode;
wenzelm@26215
    46
wenzelm@26215
    47
wenzelm@26215
    48
(* compiler-independent timing functions *)
wenzelm@26215
    49
wenzelm@26215
    50
fun start_timing () =
wenzelm@26215
    51
  let val CPUtimer = Timer.startCPUTimer();
wenzelm@26215
    52
      val time = Timer.checkCPUTimer(CPUtimer)
wenzelm@26215
    53
  in  (CPUtimer,time)  end;
wenzelm@26215
    54
wenzelm@26215
    55
fun end_timing (CPUtimer, {sys,usr}) =
wenzelm@26215
    56
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
wenzelm@26215
    57
      val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
wenzelm@26215
    58
  in  "User " ^ toString (usr2-usr) ^
wenzelm@26215
    59
      "  All "^ toString (sys2-sys + usr2-usr) ^
wenzelm@26215
    60
      " secs"
wenzelm@26215
    61
      handle Time => ""
wenzelm@26215
    62
  end;
wenzelm@26215
    63
wenzelm@26215
    64
fun check_timer timer =
wenzelm@26215
    65
  let
wenzelm@26215
    66
    val {sys, usr} = Timer.checkCPUTimer timer;
wenzelm@26215
    67
    val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
wenzelm@26215
    68
  in (sys, usr, gc) end;
wenzelm@26215
    69
wenzelm@26215
    70
wenzelm@26215
    71
(* prompts *)
wenzelm@26215
    72
wenzelm@26215
    73
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
wenzelm@26215
    74
wenzelm@26215
    75
wenzelm@26215
    76
(* toplevel pretty printing (see also Pure/pure_setup.ML) *)
wenzelm@26215
    77
wenzelm@26215
    78
fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
wenzelm@26215
    79
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
wenzelm@26215
    80
    fn () => brk (99999, 0), en);
wenzelm@26215
    81
wenzelm@26215
    82
(*print depth*)
wenzelm@26215
    83
local
wenzelm@26215
    84
  val depth = ref 10;
wenzelm@26215
    85
in
wenzelm@26215
    86
  fun get_print_depth () = ! depth;
wenzelm@26215
    87
  fun print_depth n = (depth := n; PolyML.print_depth n);
wenzelm@26215
    88
end;
wenzelm@26215
    89
wenzelm@26215
    90
wenzelm@26215
    91
wenzelm@26215
    92
(** interrupts **)
wenzelm@26215
    93
wenzelm@26215
    94
exception Interrupt = SML90.Interrupt;
wenzelm@26215
    95
wenzelm@26215
    96
local
wenzelm@26215
    97
wenzelm@26215
    98
val sig_int = 2;
wenzelm@26215
    99
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ());
wenzelm@26215
   100
wenzelm@26215
   101
val _ = Signal.signal (sig_int, default_handler);
wenzelm@26380
   102
val _ = PolyML.onEntry (fn () => (Signal.signal (sig_int, default_handler); ()));
wenzelm@26215
   103
wenzelm@26215
   104
fun change_signal new_handler f x =
wenzelm@26215
   105
  let
wenzelm@26215
   106
    (*RACE wrt. other signals!*)
wenzelm@26215
   107
    val old_handler = Signal.signal (sig_int, new_handler);
wenzelm@26215
   108
    val result = Exn.capture (f old_handler) x;
wenzelm@26215
   109
    val _ = Signal.signal (sig_int, old_handler);
wenzelm@26215
   110
  in Exn.release result end;
wenzelm@26215
   111
wenzelm@26215
   112
in
wenzelm@26215
   113
wenzelm@26215
   114
fun interruptible f = change_signal default_handler (fn _ => f);
wenzelm@26215
   115
wenzelm@26215
   116
fun uninterruptible f =
wenzelm@26215
   117
  change_signal Signal.SIG_IGN
wenzelm@26215
   118
    (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
wenzelm@26215
   119
wenzelm@26215
   120
end;
wenzelm@26215
   121
wenzelm@26215
   122
wenzelm@26215
   123
wenzelm@26215
   124
(** OS related **)
wenzelm@26215
   125
wenzelm@26215
   126
(* Posix patches *)
wenzelm@26215
   127
wenzelm@26215
   128
(*This extension of the Poly/ML Signal structure is only necessary
wenzelm@26215
   129
  because in SML/NJ, types Posix.Signal.signal and Signals.signal differ.*)
wenzelm@26215
   130
structure IsaSignal =
wenzelm@26215
   131
struct
wenzelm@26215
   132
  open Signal
wenzelm@26215
   133
  val usr1 = Posix.Signal.usr1
wenzelm@26215
   134
  val usr2 = Posix.Signal.usr2
wenzelm@26215
   135
  val alrm = Posix.Signal.alrm
wenzelm@26215
   136
  val chld = Posix.Signal.chld
wenzelm@26215
   137
  val cont = Posix.Signal.cont
wenzelm@26215
   138
  val int  = Posix.Signal.int
wenzelm@26215
   139
  val quit = Posix.Signal.quit
wenzelm@26215
   140
end;
wenzelm@26215
   141
wenzelm@26215
   142
wenzelm@26215
   143
(* current directory *)
wenzelm@26215
   144
wenzelm@26215
   145
val cd = OS.FileSys.chDir;
wenzelm@26215
   146
val pwd = OS.FileSys.getDir;
wenzelm@26215
   147
wenzelm@26215
   148
wenzelm@26215
   149
(*Convert a process ID to a decimal string (chiefly for tracing)*)
wenzelm@26215
   150
fun string_of_pid pid =
wenzelm@26215
   151
  Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
wenzelm@26215
   152
wenzelm@26215
   153
wenzelm@26215
   154
(* getenv *)
wenzelm@26215
   155
wenzelm@26215
   156
fun getenv var =
wenzelm@26215
   157
  (case OS.Process.getEnv var of
wenzelm@26215
   158
    NONE => ""
wenzelm@26215
   159
  | SOME txt => txt);
wenzelm@26215
   160
wenzelm@26215
   161
wenzelm@26215
   162
(* profile execution *)
wenzelm@26215
   163
wenzelm@26215
   164
fun profile 0 f x = f x
wenzelm@26215
   165
  | profile n f x =
wenzelm@26215
   166
      let
wenzelm@26215
   167
        val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n;
wenzelm@26215
   168
        val res = Exn.capture f x;
wenzelm@26215
   169
        val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0;
wenzelm@26215
   170
      in Exn.release res end;
wenzelm@26215
   171