author | wenzelm |
Fri, 03 Aug 2007 16:28:24 +0200 | |
changeset 24145 | c6402b61d44a |
parent 23965 | f93e509659c1 |
child 24290 | 5607b8b752bb |
permissions | -rw-r--r-- |
4403 | 1 |
(* Title: Pure/ML-Systems/smlnj.ML |
2 |
ID: $Id$ |
|
3 |
||
5708 | 4 |
Compatibility file for Standard ML of New Jersey 110 or later. |
4403 | 5 |
*) |
6 |
||
24145 | 7 |
fun mk_int (i: int) = i; |
8 |
fun dest_int (i: int) = i; |
|
9 |
(*use "ML-Systems/proper_int.ML";*) |
|
10 |
||
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
11 |
use "ML-Systems/exn.ML"; |
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
12 |
use "ML-Systems/multithreading_dummy.ML"; |
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
13 |
|
16542 | 14 |
|
4403 | 15 |
(** ML system related **) |
16 |
||
16542 | 17 |
(*low-level pointer equality*) |
16502 | 18 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
19 |
CM.autoload "$smlnj/init/init.cmi"; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
20 |
val pointer_eq = InlineT.ptreql; |
16528 | 21 |
|
16502 | 22 |
|
4403 | 23 |
(* restore old-style character / string functions *) |
24 |
||
24145 | 25 |
val ord = mk_int o SML90.ord; |
26 |
val chr = SML90.chr o dest_int; |
|
10725 | 27 |
val explode = SML90.explode; |
28 |
val implode = SML90.implode; |
|
4403 | 29 |
|
30 |
||
31 |
(* New Jersey ML parameters *) |
|
32 |
||
33 |
val _ = |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
34 |
(Control.Print.printLength := 1000; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
35 |
Control.Print.printDepth := 350; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
36 |
Control.Print.stringDepth := 250; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
37 |
Control.Print.signatures := 2; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
38 |
Control.MC.matchRedundantError := false); |
4403 | 39 |
|
40 |
||
41 |
(* Poly/ML emulation *) |
|
42 |
||
43 |
fun quit () = exit 0; |
|
44 |
||
45 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*) |
|
46 |
fun print_depth n = |
|
24145 | 47 |
(Control.Print.printDepth := (dest_int n) div 2; |
48 |
Control.Print.printLength := dest_int n); |
|
4403 | 49 |
|
50 |
||
5816 | 51 |
(* compiler-independent timing functions *) |
4403 | 52 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
53 |
fun start_timing () = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
54 |
let val CPUtimer = Timer.startCPUTimer(); |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
55 |
val time = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
56 |
in (CPUtimer,time) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
57 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
58 |
fun end_timing (CPUtimer, {sys,usr}) = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
59 |
let open Time (*...for Time.toString, Time.+ and Time.- *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
60 |
val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
61 |
in "User " ^ toString (usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
62 |
" All "^ toString (sys2-sys + usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
63 |
" secs" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
64 |
handle Time => "" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
65 |
end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
66 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
67 |
fun check_timer timer = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
68 |
let |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
69 |
val {sys, usr} = Timer.checkCPUTimer timer; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
70 |
val gc = Timer.checkGCTime timer; (* FIXME already included in usr? *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
71 |
in (sys, usr, gc) end; |
4403 | 72 |
|
73 |
||
16660 | 74 |
(*prompts*) |
4977 | 75 |
fun ml_prompts p1 p2 = |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
76 |
(Control.primaryPrompt := p1; Control.secondaryPrompt := p2); |
4977 | 77 |
|
17511 | 78 |
(*dummy implementation*) |
16681 | 79 |
fun profile (n: int) f x = f x; |
80 |
||
17511 | 81 |
(*dummy implementation*) |
16681 | 82 |
fun exception_trace f = f (); |
4977 | 83 |
|
18384 | 84 |
(*dummy implementation*) |
85 |
fun print x = x; |
|
16681 | 86 |
|
23770 | 87 |
(*dummy implementation*) |
88 |
fun makestring x = "dummy string for SML New Jersey"; |
|
89 |
||
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
90 |
|
4403 | 91 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *) |
92 |
||
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
93 |
fun make_pp path pprint = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
94 |
let |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
95 |
open PrettyPrint; |
4403 | 96 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
97 |
fun pp pps obj = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
98 |
pprint obj |
24145 | 99 |
(string pps, openHOVBox pps o Rel o dest_int, |
100 |
fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps, |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
101 |
fn () => closeBox pps); |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
102 |
in (path, pp) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
103 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
104 |
fun install_pp (path, pp) = CompilerPPTable.install_pp path pp; |
4403 | 105 |
|
106 |
||
107 |
(* ML command execution *) |
|
108 |
||
22144 | 109 |
fun use_text name (print, err) verbose txt = |
5090 | 110 |
let |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
111 |
val ref out_orig = Control.Print.out; |
5090 | 112 |
|
113 |
val out_buffer = ref ([]: string list); |
|
114 |
val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())}; |
|
10914 | 115 |
fun output () = |
7890 | 116 |
let val str = implode (rev (! out_buffer)) |
10914 | 117 |
in String.substring (str, 0, Int.max (0, size str - 1)) end; |
5090 | 118 |
in |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
119 |
Control.Print.out := out; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
120 |
Backend.Interact.useStream (TextIO.openString txt) handle exn => |
22144 | 121 |
(Control.Print.out := out_orig; |
122 |
err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn); |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
123 |
Control.Print.out := out_orig; |
10914 | 124 |
if verbose then print (output ()) else () |
5090 | 125 |
end; |
4403 | 126 |
|
21770 | 127 |
fun use_file _ _ name = use name; |
128 |
||
129 |
||
4403 | 130 |
|
5816 | 131 |
(** interrupts **) |
132 |
||
12990 | 133 |
exception Interrupt; |
134 |
||
135 |
fun ignore_interrupt f x = |
|
5816 | 136 |
let |
12990 | 137 |
val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE); |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
138 |
val result = Exn.capture f x; |
12990 | 139 |
val _ = Signals.setHandler (Signals.sigINT, old_handler); |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
140 |
in Exn.release result end; |
5816 | 141 |
|
12990 | 142 |
fun raise_interrupt f x = |
143 |
let |
|
5816 | 144 |
val interrupted = ref false; |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
145 |
val result = ref (Exn.Result ()); |
12990 | 146 |
val old_handler = Signals.inqHandler Signals.sigINT; |
5816 | 147 |
in |
12990 | 148 |
SMLofNJ.Cont.callcc (fn cont => |
149 |
(Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont))); |
|
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
150 |
result := Exn.capture f x)); |
12990 | 151 |
Signals.setHandler (Signals.sigINT, old_handler); |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
152 |
if ! interrupted then raise Interrupt else Exn.release (! result) |
12990 | 153 |
end; |
5816 | 154 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
155 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
156 |
(* basis library fixes *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
157 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
158 |
structure TextIO = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
159 |
struct |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
160 |
open TextIO; |
23139
aa899bce7c3b
TextIO.inputLine: use present SML B library version;
wenzelm
parents:
22144
diff
changeset
|
161 |
fun inputLine is = TextIO.inputLine is |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
162 |
handle IO.Io _ => raise Interrupt; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
163 |
end; |
17511 | 164 |
|
18790
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
165 |
|
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
166 |
(* bounded time execution *) |
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
167 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
168 |
fun interrupt_timeout time f x = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
169 |
TimeLimit.timeLimit time f x |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
170 |
handle TimeLimit.TimeOut => raise Interrupt; |
18790
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
171 |
|
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
172 |
|
16542 | 173 |
(** Signal handling: emulation of the Poly/ML Signal structure. Note that types |
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
174 |
Posix.Signal.signal and Signals.signal differ **) |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
175 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
176 |
structure IsaSignal = |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
177 |
struct |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
178 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
179 |
datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit; |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
180 |
|
16542 | 181 |
(*From the SML/NJ documentation: "HANDLER(f) installs a handler for a |
182 |
signal. When signal is delivered to the process, the execution state |
|
183 |
of the current thread will be bundled up as a continuation k, then |
|
184 |
f(signal,n,k) will be called. The number n is the number of times |
|
185 |
signal has been signalled since the last time f was invoked for it."*) |
|
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
186 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
187 |
fun toAction SIG_DFL = Signals.DEFAULT |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
188 |
| toAction SIG_IGN = Signals.IGNORE |
16542 | 189 |
| toAction (SIG_HANDLE iu) = |
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
190 |
Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont)); |
16542 | 191 |
|
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
192 |
(*The types are correct, but I'm not sure about the semantics!*) |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
193 |
fun fromAction Signals.DEFAULT = SIG_DFL |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
194 |
| fromAction Signals.IGNORE = SIG_IGN |
16542 | 195 |
| fromAction (Signals.HANDLER f) = |
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
196 |
SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ()))); |
16542 | 197 |
|
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
198 |
(*Poly/ML version has type int * sig_handle -> sig_handle*) |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
199 |
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh)); |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
200 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
201 |
val usr1 = UnixSignals.sigUSR1 |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
202 |
val usr2 = UnixSignals.sigUSR2 |
17763 | 203 |
val alrm = UnixSignals.sigALRM |
204 |
val chld = UnixSignals.sigCHLD |
|
205 |
val cont = UnixSignals.sigCONT |
|
206 |
val int = UnixSignals.sigINT |
|
207 |
val quit = UnixSignals.sigQUIT |
|
208 |
val term = UnixSignals.sigTERM |
|
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
209 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
210 |
end; |
5816 | 211 |
|
212 |
||
4403 | 213 |
(** OS related **) |
214 |
||
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
215 |
(* current directory *) |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
216 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
217 |
val cd = OS.FileSys.chDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
218 |
val pwd = OS.FileSys.getDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
219 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
220 |
|
4403 | 221 |
(* system command execution *) |
222 |
||
223 |
(*execute Unix command which doesn't take any input from stdin and |
|
224 |
sends its output to stdout; could be done more easily by Unix.execute, |
|
225 |
but that function doesn't use the PATH*) |
|
226 |
fun execute command = |
|
227 |
let |
|
228 |
val tmp_name = OS.FileSys.tmpName (); |
|
229 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
230 |
val result = TextIO.inputAll is; |
|
231 |
in |
|
232 |
TextIO.closeIn is; |
|
233 |
OS.FileSys.remove tmp_name; |
|
234 |
result |
|
235 |
end; |
|
236 |
||
7855 | 237 |
(*plain version; with return code*) |
24145 | 238 |
val system = mk_int o OS.Process.system; |
7855 | 239 |
|
4403 | 240 |
|
17824 | 241 |
(*Convert a process ID to a decimal string (chiefly for tracing)*) |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
242 |
fun string_of_pid pid = |
17824 | 243 |
Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid)); |
244 |
||
245 |
||
4403 | 246 |
(* getenv *) |
247 |
||
248 |
fun getenv var = |
|
249 |
(case OS.Process.getEnv var of |
|
250 |
NONE => "" |
|
251 |
| SOME txt => txt); |