src/Pure/ML-Systems/smlnj.ML
author haftmann
Mon Dec 12 15:36:46 2005 +0100 (2005-12-12)
changeset 18384 fa38cca42913
parent 17824 36b2978d339a
child 18760 97aaecb84afe
permissions -rw-r--r--
added dummy 'print' to non-polyml systems
wenzelm@4403
     1
(*  Title:      Pure/ML-Systems/smlnj.ML
wenzelm@4403
     2
    ID:         $Id$
wenzelm@4403
     3
    Author:     Carsten Clasohm and Markus Wenzel, TU Muenchen
wenzelm@4403
     4
wenzelm@5708
     5
Compatibility file for Standard ML of New Jersey 110 or later.
wenzelm@4403
     6
*)
wenzelm@4403
     7
wenzelm@12874
     8
(case #version_id (Compiler.version) of
wenzelm@12874
     9
  [110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else ()
wenzelm@12874
    10
| _ => ());
wenzelm@12874
    11
wenzelm@16542
    12
wenzelm@4403
    13
(** ML system related **)
wenzelm@4403
    14
wenzelm@16542
    15
(*low-level pointer equality*)
wenzelm@16502
    16
wenzelm@16542
    17
(*dummy version -- may get overridden in smlnj-ptreql.ML*)
wenzelm@16542
    18
fun pointer_eq (x:'a, y) = false;
wenzelm@16502
    19
paulson@16528
    20
(case #version_id (Compiler.version) of
paulson@16528
    21
  [110, x] => if x >= 49 then use "ML-Systems/smlnj-ptreql.ML" else ()
paulson@16528
    22
| _ => ());
paulson@16528
    23
wenzelm@16502
    24
wenzelm@4403
    25
(* restore old-style character / string functions *)
wenzelm@4403
    26
paulson@10725
    27
val ord     = SML90.ord;
paulson@10725
    28
val chr     = SML90.chr;
paulson@10725
    29
val explode = SML90.explode;
paulson@10725
    30
val implode = SML90.implode;
wenzelm@4403
    31
wenzelm@4403
    32
wenzelm@4403
    33
(* New Jersey ML parameters *)
wenzelm@4403
    34
wenzelm@4403
    35
val _ =
wenzelm@4403
    36
 (Compiler.Control.Print.printLength := 1000;
wenzelm@4403
    37
  Compiler.Control.Print.printDepth := 350;
wenzelm@4403
    38
  Compiler.Control.Print.stringDepth := 250;
berghofe@12581
    39
  Compiler.Control.Print.signatures := 2;
berghofe@12581
    40
  Compiler.Control.MC.matchRedundantError := false);
wenzelm@4403
    41
wenzelm@4403
    42
wenzelm@4403
    43
(* Poly/ML emulation *)
wenzelm@4403
    44
wenzelm@4403
    45
fun quit () = exit 0;
wenzelm@4403
    46
wenzelm@4403
    47
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
wenzelm@4403
    48
fun print_depth n =
wenzelm@4403
    49
 (Compiler.Control.Print.printDepth := n div 2;
wenzelm@4403
    50
  Compiler.Control.Print.printLength := n);
wenzelm@4403
    51
wenzelm@4403
    52
wenzelm@5816
    53
(* compiler-independent timing functions *)
wenzelm@4403
    54
skalberg@14519
    55
(case #version_id (Compiler.version) of
paulson@14656
    56
  [110, x] => if x >= 44
wenzelm@16542
    57
              then use "ML-Systems/cpu-timer-basis.ML"
wenzelm@16542
    58
              else use "ML-Systems/cpu-timer-gc.ML"
skalberg@14520
    59
| _ => use "ML-Systems/cpu-timer-gc.ML");
wenzelm@4403
    60
wenzelm@4403
    61
wenzelm@16660
    62
(*prompts*)
wenzelm@4977
    63
fun ml_prompts p1 p2 =
wenzelm@4977
    64
  (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
wenzelm@4977
    65
wenzelm@17511
    66
(*dummy implementation*)
wenzelm@16681
    67
fun profile (n: int) f x = f x;
wenzelm@16681
    68
wenzelm@17511
    69
(*dummy implementation*)
wenzelm@16681
    70
fun exception_trace f = f ();
wenzelm@4977
    71
haftmann@18384
    72
(*dummy implementation*)
haftmann@18384
    73
fun print x = x;
wenzelm@16681
    74
wenzelm@4403
    75
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@4403
    76
skalberg@14519
    77
(case #version_id (Compiler.version) of
paulson@14656
    78
  [110, x] => if x >= 44
wenzelm@16542
    79
              then use "ML-Systems/smlnj-pp-new.ML"
wenzelm@16542
    80
              else use "ML-Systems/smlnj-pp-old.ML"
skalberg@14520
    81
| _ => use "ML-Systems/smlnj-pp-old.ML");
wenzelm@4403
    82
wenzelm@4403
    83
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
wenzelm@4403
    84
wenzelm@4403
    85
wenzelm@4403
    86
(* ML command execution *)
wenzelm@4403
    87
wenzelm@10914
    88
fun use_text (print, err) verbose txt =
wenzelm@5090
    89
  let
wenzelm@5090
    90
    val ref out_orig = Compiler.Control.Print.out;
wenzelm@5090
    91
wenzelm@5090
    92
    val out_buffer = ref ([]: string list);
wenzelm@5090
    93
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
wenzelm@10914
    94
    fun output () =
wenzelm@7890
    95
      let val str = implode (rev (! out_buffer))
wenzelm@10914
    96
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
wenzelm@5090
    97
  in
wenzelm@5090
    98
    Compiler.Control.Print.out := out;
wenzelm@5090
    99
    Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
wenzelm@10914
   100
      (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
wenzelm@5090
   101
    Compiler.Control.Print.out := out_orig;
wenzelm@10914
   102
    if verbose then print (output ()) else ()
wenzelm@5090
   103
  end;
wenzelm@4403
   104
wenzelm@4403
   105
wenzelm@4403
   106
wenzelm@5816
   107
(** interrupts **)
wenzelm@5816
   108
wenzelm@12990
   109
exception Interrupt;
wenzelm@12990
   110
wenzelm@5816
   111
local
wenzelm@5816
   112
wenzelm@12990
   113
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
wenzelm@5816
   114
wenzelm@12990
   115
fun release NONE = ()
wenzelm@12990
   116
  | release (SOME exn) = raise exn;
wenzelm@5816
   117
wenzelm@5816
   118
in
wenzelm@5816
   119
wenzelm@12990
   120
fun ignore_interrupt f x =
wenzelm@5816
   121
  let
wenzelm@12990
   122
    val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
wenzelm@12990
   123
    val result = capture f x;
wenzelm@12990
   124
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@12990
   125
  in release result end;
wenzelm@5816
   126
wenzelm@12990
   127
fun raise_interrupt f x =
wenzelm@12990
   128
  let
wenzelm@5816
   129
    val interrupted = ref false;
wenzelm@12990
   130
    val result = ref NONE;
wenzelm@12990
   131
    val old_handler = Signals.inqHandler Signals.sigINT;
wenzelm@5816
   132
  in
wenzelm@12990
   133
    SMLofNJ.Cont.callcc (fn cont =>
wenzelm@12990
   134
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
wenzelm@12990
   135
      result := capture f x));
wenzelm@12990
   136
    Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@12990
   137
    if ! interrupted then raise Interrupt else release (! result)
wenzelm@12990
   138
  end;
wenzelm@5816
   139
wenzelm@5816
   140
end;
wenzelm@5816
   141
wenzelm@17529
   142
(case #version_id (Compiler.version) of
wenzelm@17529
   143
  [110, x] => if x >= 44
wenzelm@17529
   144
              then use "ML-Systems/smlnj-basis-compat.ML"
wenzelm@17529
   145
              else ()
wenzelm@17529
   146
| _ => ());
wenzelm@17529
   147
wenzelm@17511
   148
wenzelm@16542
   149
(** Signal handling: emulation of the Poly/ML Signal structure. Note that types
paulson@15702
   150
    Posix.Signal.signal and Signals.signal differ **)
paulson@15702
   151
paulson@15702
   152
structure IsaSignal =
paulson@15702
   153
struct
paulson@15702
   154
paulson@15702
   155
datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
paulson@15702
   156
wenzelm@16542
   157
(*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
wenzelm@16542
   158
  signal. When signal is delivered to the process, the execution state
wenzelm@16542
   159
  of the current thread will be bundled up as a continuation k, then
wenzelm@16542
   160
  f(signal,n,k) will be called. The number n is the number of times
wenzelm@16542
   161
  signal has been signalled since the last time f was invoked for it."*)
paulson@15702
   162
paulson@15702
   163
fun toAction SIG_DFL = Signals.DEFAULT
paulson@15702
   164
  | toAction SIG_IGN = Signals.IGNORE
wenzelm@16542
   165
  | toAction (SIG_HANDLE iu) =
paulson@15702
   166
      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
wenzelm@16542
   167
paulson@15702
   168
(*The types are correct, but I'm not sure about the semantics!*)
paulson@15702
   169
fun fromAction Signals.DEFAULT = SIG_DFL
paulson@15702
   170
  | fromAction Signals.IGNORE = SIG_IGN
wenzelm@16542
   171
  | fromAction (Signals.HANDLER f) =
paulson@15702
   172
      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
wenzelm@16542
   173
paulson@15702
   174
(*Poly/ML version has type  int * sig_handle -> sig_handle*)
paulson@15702
   175
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
paulson@15702
   176
paulson@15702
   177
val usr1 = UnixSignals.sigUSR1
paulson@15702
   178
val usr2 = UnixSignals.sigUSR2
paulson@17763
   179
val alrm = UnixSignals.sigALRM
paulson@17763
   180
val chld = UnixSignals.sigCHLD
paulson@17763
   181
val cont = UnixSignals.sigCONT
paulson@17763
   182
val int = UnixSignals.sigINT
paulson@17763
   183
val quit = UnixSignals.sigQUIT
paulson@17763
   184
val term = UnixSignals.sigTERM
paulson@15702
   185
paulson@15702
   186
end;
wenzelm@5816
   187
wenzelm@5816
   188
wenzelm@4403
   189
(** OS related **)
wenzelm@4403
   190
wenzelm@4403
   191
(* system command execution *)
wenzelm@4403
   192
wenzelm@4403
   193
(*execute Unix command which doesn't take any input from stdin and
wenzelm@4403
   194
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@4403
   195
  but that function doesn't use the PATH*)
wenzelm@4403
   196
fun execute command =
wenzelm@4403
   197
  let
wenzelm@4403
   198
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@4403
   199
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@4403
   200
    val result = TextIO.inputAll is;
wenzelm@4403
   201
  in
wenzelm@4403
   202
    TextIO.closeIn is;
wenzelm@4403
   203
    OS.FileSys.remove tmp_name;
wenzelm@4403
   204
    result
wenzelm@4403
   205
  end;
wenzelm@4403
   206
wenzelm@7855
   207
(*plain version; with return code*)
wenzelm@12990
   208
val system = OS.Process.system: string -> int;
wenzelm@7855
   209
wenzelm@4403
   210
wenzelm@17824
   211
(*Convert a process ID to a decimal string (chiefly for tracing)*)
wenzelm@17824
   212
fun string_of_pid pid = 
wenzelm@17824
   213
    Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
wenzelm@17824
   214
wenzelm@17824
   215
wenzelm@4403
   216
(* getenv *)
wenzelm@4403
   217
wenzelm@4403
   218
fun getenv var =
wenzelm@4403
   219
  (case OS.Process.getEnv var of
wenzelm@4403
   220
    NONE => ""
wenzelm@4403
   221
  | SOME txt => txt);