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