author | wenzelm |
Sun, 16 Sep 2007 14:52:33 +0200 | |
changeset 24598 | 44a1c0c68e21 |
parent 24329 | f31594168d27 |
child 24688 | a5754ca5c510 |
permissions | -rw-r--r-- |
2341 | 1 |
(* Title: Pure/ML-Systems/polyml.ML |
2 |
ID: $Id$ |
|
3 |
||
23139
aa899bce7c3b
TextIO.inputLine: use present SML B library version;
wenzelm
parents:
22144
diff
changeset
|
4 |
Compatibility file for Poly/ML (version 4.1.x and 4.2.0). |
2341 | 5 |
*) |
6 |
||
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
7 |
use "ML-Systems/exn.ML"; |
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
8 |
use "ML-Systems/multithreading_dummy.ML"; |
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
9 |
|
24598 | 10 |
val ml_system_fix_ints = false; |
11 |
||
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
12 |
|
16374 | 13 |
(** ML system and platform related **) |
14 |
||
17077 | 15 |
(* String compatibility *) |
16 |
||
16516 | 17 |
(*low-level pointer equality*) |
18 |
val pointer_eq = Address.wordEq; |
|
16502 | 19 |
|
20 |
||
11078 | 21 |
(* old Poly/ML emulation *) |
2341 | 22 |
|
11078 | 23 |
local |
24 |
val orig_exit = exit; |
|
25 |
in |
|
26 |
open PolyML; |
|
27 |
val exit = orig_exit; |
|
28 |
fun quit () = exit 0; |
|
7148 | 29 |
end; |
6042 | 30 |
|
2341 | 31 |
|
11078 | 32 |
(* restore old-style character / string functions *) |
3588 | 33 |
|
11078 | 34 |
val ord = SML90.ord; |
35 |
val chr = SML90.chr; |
|
36 |
val explode = SML90.explode; |
|
37 |
val implode = SML90.implode; |
|
38 |
||
14870 | 39 |
|
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12989
diff
changeset
|
40 |
(* compiler-independent timing functions *) |
2341 | 41 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
42 |
fun start_timing () = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
43 |
let val CPUtimer = Timer.startCPUTimer(); |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
44 |
val time = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
45 |
in (CPUtimer,time) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
46 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
47 |
fun end_timing (CPUtimer, {sys,usr}) = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
48 |
let open Time (*...for Time.toString, Time.+ and Time.- *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
49 |
val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
50 |
in "User " ^ toString (usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
51 |
" All "^ toString (sys2-sys + usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
52 |
" secs" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
53 |
handle Time => "" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
54 |
end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
55 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
56 |
fun check_timer timer = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
57 |
let |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
58 |
val {sys, usr} = Timer.checkCPUTimer timer; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
59 |
val gc = Timer.checkGCTime timer; (* FIXME already included in usr? *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
60 |
in (sys, usr, gc) end; |
2341 | 61 |
|
14870 | 62 |
|
14850 | 63 |
(* bounded time execution *) |
64 |
||
21281
0767e7dad549
no special treatment for cygwin (this is supposed to be actual cygwin, not win32 polyml within cygwin);
wenzelm
parents:
18814
diff
changeset
|
65 |
(*dummy implementation*) |
18760 | 66 |
fun interrupt_timeout time f x = |
67 |
f x; |
|
68 |
||
14870 | 69 |
|
4977 | 70 |
(* prompts *) |
71 |
||
4984 | 72 |
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2); |
4977 | 73 |
|
74 |
||
24290 | 75 |
(* toplevel pretty printing (see also Pure/pure_setup.ML) *) |
2341 | 76 |
|
11078 | 77 |
fun make_pp _ pprint (str, blk, brk, en) _ _ obj = |
2341 | 78 |
pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0), |
79 |
fn () => brk (99999, 0), en); |
|
80 |
||
24329 | 81 |
(*print depth*) |
82 |
local |
|
83 |
val depth = ref 10; |
|
84 |
in |
|
85 |
fun get_print_depth () = ! depth; |
|
86 |
fun print_depth n = (depth := n; PolyML.print_depth n); |
|
87 |
end; |
|
88 |
||
2341 | 89 |
|
3588 | 90 |
(* ML command execution -- 'eval' *) |
91 |
||
24598 | 92 |
fun use_text (tune: string -> string) name (print, err) verbose txt = |
3588 | 93 |
let |
24598 | 94 |
val in_buffer = ref (explode (tune txt)); |
5090 | 95 |
val out_buffer = ref ([]: string list); |
21715 | 96 |
fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs)); |
5090 | 97 |
|
5038 | 98 |
fun get () = |
5090 | 99 |
(case ! in_buffer of |
5038 | 100 |
[] => "" |
5090 | 101 |
| c :: cs => (in_buffer := cs; c)); |
102 |
fun put s = out_buffer := s :: ! out_buffer; |
|
103 |
||
5038 | 104 |
fun exec () = |
5090 | 105 |
(case ! in_buffer of |
5038 | 106 |
[] => () |
5090 | 107 |
| _ => (PolyML.compiler (get, put) (); exec ())); |
108 |
in |
|
22144 | 109 |
exec () handle exn => |
110 |
(err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn); |
|
10914 | 111 |
if verbose then print (output ()) else () |
5090 | 112 |
end; |
3588 | 113 |
|
24598 | 114 |
fun use_file tune output verbose name = |
115 |
let |
|
116 |
val instream = TextIO.openIn name; |
|
117 |
val txt = TextIO.inputAll instream before TextIO.closeIn instream; |
|
118 |
in use_text tune name output verbose txt end; |
|
21770 | 119 |
|
11078 | 120 |
|
15832 | 121 |
(*eval command line arguments*) |
122 |
local |
|
123 |
fun println s = |
|
124 |
(TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut); |
|
21715 | 125 |
fun eval "-q" = () |
24598 | 126 |
| eval txt = use_text (fn x => x) "" (println, println) false txt; |
15832 | 127 |
in |
24296
3479a9fe73e0
removed signal setup from root function to on-entry hook;
wenzelm
parents:
24290
diff
changeset
|
128 |
val _ = PolyML.onEntry (fn () => |
3479a9fe73e0
removed signal setup from root function to on-entry hook;
wenzelm
parents:
24290
diff
changeset
|
129 |
(Signal.signal (2, Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ())); |
3479a9fe73e0
removed signal setup from root function to on-entry hook;
wenzelm
parents:
24290
diff
changeset
|
130 |
app eval (CommandLine.arguments ()))); |
15832 | 131 |
end; |
132 |
||
133 |
||
11078 | 134 |
|
135 |
(** interrupts **) |
|
136 |
||
137 |
exception Interrupt = SML90.Interrupt; |
|
138 |
||
139 |
local |
|
140 |
||
141 |
val sig_int = 2; |
|
142 |
||
143 |
fun change_signal new_handler f x = |
|
144 |
let |
|
145 |
(*RACE wrt. other signals*) |
|
146 |
val old_handler = Signal.signal (sig_int, new_handler); |
|
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
147 |
val result = Exn.capture f x; |
11078 | 148 |
val _ = Signal.signal (sig_int, old_handler); |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
149 |
in Exn.release result end; |
5813 | 150 |
|
11078 | 151 |
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ()); |
152 |
||
153 |
in |
|
154 |
||
155 |
val _ = Signal.signal (sig_int, default_handler); |
|
156 |
||
12989 | 157 |
fun ignore_interrupt f = change_signal Signal.SIG_IGN f; |
158 |
fun raise_interrupt f = change_signal default_handler f; |
|
11078 | 159 |
|
160 |
end; |
|
5813 | 161 |
|
162 |
||
163 |
||
3588 | 164 |
(** OS related **) |
2341 | 165 |
|
21281
0767e7dad549
no special treatment for cygwin (this is supposed to be actual cygwin, not win32 polyml within cygwin);
wenzelm
parents:
18814
diff
changeset
|
166 |
use "ML-Systems/polyml-posix.ML"; |
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
167 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
168 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
169 |
(* current directory *) |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
170 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
171 |
val cd = OS.FileSys.chDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
172 |
val pwd = OS.FileSys.getDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
173 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
174 |
|
3588 | 175 |
(* system command execution *) |
2341 | 176 |
|
3588 | 177 |
(*execute Unix command which doesn't take any input from stdin and |
11078 | 178 |
sends its output to stdout; could be done more easily by Unix.execute, |
179 |
but that function doesn't use the PATH*) |
|
3588 | 180 |
fun execute command = |
181 |
let |
|
11078 | 182 |
val tmp_name = OS.FileSys.tmpName (); |
183 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
184 |
val result = TextIO.inputAll is; |
|
185 |
in |
|
186 |
TextIO.closeIn is; |
|
187 |
OS.FileSys.remove tmp_name; |
|
188 |
result |
|
189 |
end; |
|
7855 | 190 |
|
11078 | 191 |
(*plain version; with return code*) |
192 |
fun system cmd = |
|
193 |
if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1; |
|
2341 | 194 |
|
195 |
||
17824 | 196 |
(*Convert a process ID to a decimal string (chiefly for tracing)*) |
197 |
fun string_of_pid pid = |
|
23139
aa899bce7c3b
TextIO.inputLine: use present SML B library version;
wenzelm
parents:
22144
diff
changeset
|
198 |
Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid)); |
17824 | 199 |
|
200 |
||
3588 | 201 |
(* getenv *) |
202 |
||
11078 | 203 |
fun getenv var = |
204 |
(case OS.Process.getEnv var of |
|
205 |
NONE => "" |
|
206 |
| SOME txt => txt); |
|
15028 | 207 |
|
208 |
||
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
209 |
(* profile execution *) |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
210 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
211 |
local |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
212 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
213 |
fun no_profile () = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
214 |
RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0; |
15028 | 215 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
216 |
in |
15699
7d91dd712ff8
fixing an incompatibility with Posix.IO.mkTextReader
paulson
parents:
15028
diff
changeset
|
217 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
218 |
fun profile 0 f x = f x |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
219 |
| profile n f x = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
220 |
(RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n; |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
221 |
let val y = f x handle exn => (no_profile (); raise exn) |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
222 |
in no_profile (); y end); |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
223 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
224 |
end; |