src/Pure/ML-Systems/smlnj.ML
author wenzelm
Mon Mar 24 23:34:24 2008 +0100 (2008-03-24)
changeset 26385 ae7564661e76
parent 26220 d34b68c21f9a
child 26474 94735cff132c
permissions -rw-r--r--
ML runtime compilation: pass position, tuned signature;
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2     ID:         $Id$
     3 
     4 Compatibility file for Standard ML of New Jersey 110 or later.
     5 *)
     6 
     7 use "ML-Systems/proper_int.ML";
     8 use "ML-Systems/overloading_smlnj.ML";
     9 use "ML-Systems/exn.ML";
    10 use "ML-Systems/universal.ML";
    11 use "ML-Systems/multithreading.ML";
    12 use "ML-Systems/system_shell.ML";
    13 
    14 
    15 (*low-level pointer equality*)
    16 
    17 CM.autoload "$smlnj/init/init.cmi";
    18 val pointer_eq = InlineT.ptreql;
    19 
    20 
    21 (* restore old-style character / string functions *)
    22 
    23 val ord     = mk_int o SML90.ord;
    24 val chr     = SML90.chr o dest_int;
    25 val explode = SML90.explode;
    26 val implode = SML90.implode;
    27 
    28 
    29 (* New Jersey ML parameters *)
    30 
    31 val _ =
    32  (Control.Print.printLength := 1000;
    33   Control.Print.printDepth := 350;
    34   Control.Print.stringDepth := 250;
    35   Control.Print.signatures := 2;
    36   Control.MC.matchRedundantError := false);
    37 
    38 
    39 (* Poly/ML emulation *)
    40 
    41 val exit = exit o dest_int;
    42 fun quit () = exit 0;
    43 
    44 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    45 local
    46   val depth = ref (10: int);
    47 in
    48   fun get_print_depth () = ! depth;
    49   fun print_depth n =
    50    (depth := n;
    51     Control.Print.printDepth := dest_int n div 2;
    52     Control.Print.printLength := dest_int n);
    53 end;
    54 
    55 (* compiler-independent timing functions *)
    56 
    57 fun start_timing () =
    58   let val CPUtimer = Timer.startCPUTimer();
    59       val time = Timer.checkCPUTimer(CPUtimer)
    60   in  (CPUtimer,time)  end;
    61 
    62 fun end_timing (CPUtimer, {sys,usr}) =
    63   let open Time  (*...for Time.toString, Time.+ and Time.- *)
    64       val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
    65   in  "User " ^ toString (usr2-usr) ^
    66       "  All "^ toString (sys2-sys + usr2-usr) ^
    67       " secs"
    68       handle Time => ""
    69   end;
    70 
    71 fun check_timer timer =
    72   let
    73     val {sys, usr} = Timer.checkCPUTimer timer;
    74     val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
    75   in (sys, usr, gc) end;
    76 
    77 
    78 (*prompts*)
    79 fun ml_prompts p1 p2 =
    80   (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
    81 
    82 (*dummy implementation*)
    83 fun profile (n: int) f x = f x;
    84 
    85 (*dummy implementation*)
    86 fun exception_trace f = f ();
    87 
    88 (*dummy implementation*)
    89 fun print x = x;
    90 
    91 (*dummy implementation*)
    92 fun makestring x = "dummy string for SML New Jersey";
    93 
    94 
    95 (* toplevel pretty printing (see also Pure/pure_setup.ML) *)
    96 
    97 fun make_pp path pprint =
    98   let
    99     open PrettyPrint;
   100 
   101     fun pp pps obj =
   102       pprint obj
   103         (string pps, openHOVBox pps o Rel o dest_int,
   104           fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps,
   105           fn () => closeBox pps);
   106   in (path, pp) end;
   107 
   108 fun install_pp (path, pp) = CompilerPPTable.install_pp path pp;
   109 
   110 
   111 (* ML command execution *)
   112 
   113 fun use_text (tune: string -> string) (line: int, name) (print, err) verbose txt =
   114   let
   115     val ref out_orig = Control.Print.out;
   116 
   117     val out_buffer = ref ([]: string list);
   118     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
   119     fun output () =
   120       let val str = implode (rev (! out_buffer))
   121       in String.substring (str, 0, Int.max (0, size str - 1)) end;
   122   in
   123     Control.Print.out := out;
   124     Backend.Interact.useStream (TextIO.openString (tune txt)) handle exn =>
   125       (Control.Print.out := out_orig;
   126         err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
   127     Control.Print.out := out_orig;
   128     if verbose then print (output ()) else ()
   129   end;
   130 
   131 fun use_file tune output verbose name =
   132   let
   133     val instream = TextIO.openIn name;
   134     val txt = TextIO.inputAll instream before TextIO.closeIn instream;
   135   in use_text tune (1, name) output verbose txt end;
   136 
   137 
   138 
   139 (** interrupts **)
   140 
   141 exception Interrupt;
   142 
   143 local
   144 
   145 fun change_signal new_handler f x =
   146   let
   147     val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
   148     val result = Exn.capture (f old_handler) x;
   149     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   150   in Exn.release result end;
   151 
   152 in
   153 
   154 fun interruptible (f: 'a -> 'b) x =
   155   let
   156     val result = ref (Exn.Exn Interrupt: 'b Exn.result);
   157     val old_handler = Signals.inqHandler Signals.sigINT;
   158   in
   159     SMLofNJ.Cont.callcc (fn cont =>
   160       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
   161         result := Exn.capture f x));
   162     Signals.setHandler (Signals.sigINT, old_handler);
   163     Exn.release (! result)
   164   end;
   165 
   166 fun uninterruptible f =
   167   change_signal Signals.IGNORE
   168     (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
   169 
   170 end;
   171 
   172 
   173 (* basis library fixes *)
   174 
   175 structure TextIO =
   176 struct
   177   open TextIO;
   178   fun inputLine is = TextIO.inputLine is
   179     handle IO.Io _ => raise Interrupt;
   180 end;
   181 
   182 
   183 (** Signal handling: emulation of the Poly/ML Signal structure. Note that types
   184     Posix.Signal.signal and Signals.signal differ **)
   185 
   186 structure IsaSignal =
   187 struct
   188 
   189 datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
   190 
   191 (*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
   192   signal. When signal is delivered to the process, the execution state
   193   of the current thread will be bundled up as a continuation k, then
   194   f(signal,n,k) will be called. The number n is the number of times
   195   signal has been signalled since the last time f was invoked for it."*)
   196 
   197 fun toAction SIG_DFL = Signals.DEFAULT
   198   | toAction SIG_IGN = Signals.IGNORE
   199   | toAction (SIG_HANDLE iu) =
   200       Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
   201 
   202 (*The types are correct, but I'm not sure about the semantics!*)
   203 fun fromAction Signals.DEFAULT = SIG_DFL
   204   | fromAction Signals.IGNORE = SIG_IGN
   205   | fromAction (Signals.HANDLER f) =
   206       SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
   207 
   208 (*Poly/ML version has type  int * sig_handle -> sig_handle*)
   209 fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
   210 
   211 val usr1 = UnixSignals.sigUSR1
   212 val usr2 = UnixSignals.sigUSR2
   213 val alrm = UnixSignals.sigALRM
   214 val chld = UnixSignals.sigCHLD
   215 val cont = UnixSignals.sigCONT
   216 val int = UnixSignals.sigINT
   217 val quit = UnixSignals.sigQUIT
   218 val term = UnixSignals.sigTERM
   219 
   220 end;
   221 
   222 
   223 (** OS related **)
   224 
   225 (* current directory *)
   226 
   227 val cd = OS.FileSys.chDir;
   228 val pwd = OS.FileSys.getDir;
   229 
   230 
   231 (* system command execution *)
   232 
   233 val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
   234 
   235 
   236 (*Convert a process ID to a decimal string (chiefly for tracing)*)
   237 fun string_of_pid pid =
   238   Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
   239 
   240 
   241 (* getenv *)
   242 
   243 fun getenv var =
   244   (case OS.Process.getEnv var of
   245     NONE => ""
   246   | SOME txt => txt);