src/Pure/ML-Systems/polyml.ML
author wenzelm
Fri Jul 01 14:42:03 2005 +0200 (2005-07-01)
changeset 16659 1cf39eba29fe
parent 16516 0842635545c3
child 17077 f5af929f0fb4
permissions -rw-r--r--
added profiler interface, keep 'profiling' of PolyML structure;
removed PolyML.Compiler.printInAlphabeticalOrder := false;
wenzelm@2341
     1
(*  Title:      Pure/ML-Systems/polyml.ML
wenzelm@2341
     2
    ID:         $Id$
wenzelm@2341
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@2341
     4
    Copyright   1991  University of Cambridge
wenzelm@2341
     5
wenzelm@14870
     6
Compatibility file for Poly/ML (version 4.0, 4.1, 4.1.x).
wenzelm@2341
     7
*)
wenzelm@2341
     8
wenzelm@16374
     9
(** ML system and platform related **)
wenzelm@16374
    10
wenzelm@16374
    11
(* cygwin *)
wenzelm@16374
    12
wenzelm@16374
    13
val cygwin_platform =
wenzelm@16374
    14
  let val n = size ml_platform
wenzelm@16374
    15
  in n >= 6 andalso String.substring (ml_platform, n - 6, 6) = "cygwin" end;
wenzelm@16374
    16
wenzelm@16374
    17
fun if_cygwin f x = if cygwin_platform then f x else ();
wenzelm@16374
    18
fun unless_cygwin f x = if not cygwin_platform then f x else ();
wenzelm@16374
    19
wenzelm@2341
    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
skalberg@14519
    46
use "ML-Systems/cpu-timer-basis.ML";
wenzelm@2341
    47
wenzelm@14870
    48
webertj@14850
    49
(* bounded time execution *)
webertj@14850
    50
wenzelm@16374
    51
unless_cygwin
wenzelm@16374
    52
  use "ML-Systems/polyml-time-limit.ML";
webertj@14850
    53
wenzelm@14870
    54
wenzelm@4977
    55
(* prompts *)
wenzelm@4977
    56
wenzelm@4984
    57
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
wenzelm@4977
    58
wenzelm@4977
    59
wenzelm@3588
    60
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@2341
    61
wenzelm@11078
    62
fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
wenzelm@2341
    63
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
wenzelm@2341
    64
    fn () => brk (99999, 0), en);
wenzelm@2341
    65
wenzelm@2341
    66
wenzelm@3588
    67
(* ML command execution -- 'eval' *)
wenzelm@3588
    68
wenzelm@7890
    69
local
wenzelm@7890
    70
wenzelm@7890
    71
fun drop_last [] = []
wenzelm@7890
    72
  | drop_last [x] = []
wenzelm@7890
    73
  | drop_last (x :: xs) = x :: drop_last xs;
wenzelm@7890
    74
wenzelm@7890
    75
in
wenzelm@7890
    76
wenzelm@10914
    77
fun use_text (print, err) verbose txt =
wenzelm@3588
    78
  let
wenzelm@5090
    79
    val in_buffer = ref (explode txt);
wenzelm@5090
    80
    val out_buffer = ref ([]: string list);
wenzelm@15832
    81
    fun output () = implode (drop_last (rev (! out_buffer)));
wenzelm@5090
    82
wenzelm@5038
    83
    fun get () =
wenzelm@5090
    84
      (case ! in_buffer of
wenzelm@5038
    85
        [] => ""
wenzelm@5090
    86
      | c :: cs => (in_buffer := cs; c));
wenzelm@5090
    87
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@5090
    88
wenzelm@5038
    89
    fun exec () =
wenzelm@5090
    90
      (case ! in_buffer of
wenzelm@5038
    91
        [] => ()
wenzelm@5090
    92
      | _ => (PolyML.compiler (get, put) (); exec ()));
wenzelm@5090
    93
  in
wenzelm@10914
    94
    exec () handle exn => (err (output ()); raise exn);
wenzelm@10914
    95
    if verbose then print (output ()) else ()
wenzelm@5090
    96
  end;
wenzelm@3588
    97
wenzelm@11078
    98
end;
wenzelm@11078
    99
wenzelm@11078
   100
wenzelm@15832
   101
(*eval command line arguments*)
wenzelm@15832
   102
local
wenzelm@15832
   103
  fun println s =
wenzelm@15832
   104
    (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut);
wenzelm@15851
   105
  val eval = use_text (println, println) true;
wenzelm@15832
   106
in
wenzelm@15832
   107
  val _ = PolyML.onEntry (fn () => app eval (CommandLine.arguments ()));
wenzelm@15832
   108
end;
wenzelm@15832
   109
wenzelm@15832
   110
wenzelm@11078
   111
wenzelm@11078
   112
(** interrupts **)
wenzelm@11078
   113
wenzelm@11078
   114
exception Interrupt = SML90.Interrupt;
wenzelm@11078
   115
wenzelm@11078
   116
local
wenzelm@11078
   117
wenzelm@12989
   118
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
wenzelm@11078
   119
wenzelm@12989
   120
fun release NONE = ()
wenzelm@12989
   121
  | release (SOME exn) = raise exn;
wenzelm@3588
   122
wenzelm@11078
   123
val sig_int = 2;
wenzelm@11078
   124
wenzelm@11078
   125
fun change_signal new_handler f x =
wenzelm@11078
   126
  let
wenzelm@11078
   127
    (*RACE wrt. other signals*)
wenzelm@11078
   128
    val old_handler = Signal.signal (sig_int, new_handler);
wenzelm@11078
   129
    val result = capture f x;
wenzelm@11078
   130
    val _ = Signal.signal (sig_int, old_handler);
wenzelm@11078
   131
  in release result end;
wenzelm@5813
   132
wenzelm@11078
   133
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ());
wenzelm@11078
   134
wenzelm@11078
   135
in
wenzelm@11078
   136
wenzelm@11078
   137
val _ = Signal.signal (sig_int, default_handler);
wenzelm@11078
   138
wenzelm@12989
   139
fun ignore_interrupt f = change_signal Signal.SIG_IGN f;
wenzelm@12989
   140
fun raise_interrupt f = change_signal default_handler f;
wenzelm@11078
   141
wenzelm@11078
   142
end;
wenzelm@5813
   143
wenzelm@5813
   144
wenzelm@5813
   145
wenzelm@3588
   146
(** OS related **)
wenzelm@2341
   147
wenzelm@16659
   148
unless_cygwin
wenzelm@16659
   149
  use "ML-Systems/polyml-posix.ML";
wenzelm@16659
   150
wenzelm@16659
   151
wenzelm@3588
   152
(* system command execution *)
wenzelm@2341
   153
wenzelm@3588
   154
(*execute Unix command which doesn't take any input from stdin and
wenzelm@11078
   155
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@11078
   156
  but that function doesn't use the PATH*)
wenzelm@3588
   157
fun execute command =
wenzelm@3588
   158
  let
wenzelm@11078
   159
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@11078
   160
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@11078
   161
    val result = TextIO.inputAll is;
wenzelm@11078
   162
  in
wenzelm@11078
   163
    TextIO.closeIn is;
wenzelm@11078
   164
    OS.FileSys.remove tmp_name;
wenzelm@11078
   165
    result
wenzelm@11078
   166
  end;
wenzelm@7855
   167
wenzelm@11078
   168
(*plain version; with return code*)
wenzelm@11078
   169
fun system cmd =
wenzelm@11078
   170
  if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1;
wenzelm@2341
   171
wenzelm@2341
   172
wenzelm@3588
   173
(* getenv *)
wenzelm@3588
   174
wenzelm@11078
   175
fun getenv var =
wenzelm@11078
   176
  (case OS.Process.getEnv var of
wenzelm@11078
   177
    NONE => ""
wenzelm@11078
   178
  | SOME txt => txt);
paulson@15028
   179
paulson@15028
   180
wenzelm@16659
   181
(* profile execution *)
wenzelm@16659
   182
wenzelm@16659
   183
local
wenzelm@16659
   184
wenzelm@16659
   185
fun no_profile () =
wenzelm@16659
   186
  RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0;
paulson@15028
   187
wenzelm@16659
   188
in
paulson@15699
   189
wenzelm@16659
   190
fun profile 0 f x = f x
wenzelm@16659
   191
  | profile n f x =
wenzelm@16659
   192
     (RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n;
wenzelm@16659
   193
      let val y = f x handle exn => (no_profile (); raise exn)
wenzelm@16659
   194
      in no_profile (); y end);
wenzelm@16659
   195
wenzelm@16659
   196
end;