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