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