author | wenzelm |
Tue, 24 Jul 2007 19:44:37 +0200 | |
changeset 23965 | f93e509659c1 |
parent 23921 | 947152add153 |
child 24290 | 5607b8b752bb |
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 |
||
3588 | 73 |
(* toplevel pretty printing (see also Pure/install_pp.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 |
||
79 |
||
3588 | 80 |
(* ML command execution -- 'eval' *) |
81 |
||
22144 | 82 |
fun use_text name (print, err) verbose txt = |
3588 | 83 |
let |
5090 | 84 |
val in_buffer = ref (explode txt); |
85 |
val out_buffer = ref ([]: string list); |
|
21715 | 86 |
fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs)); |
5090 | 87 |
|
5038 | 88 |
fun get () = |
5090 | 89 |
(case ! in_buffer of |
5038 | 90 |
[] => "" |
5090 | 91 |
| c :: cs => (in_buffer := cs; c)); |
92 |
fun put s = out_buffer := s :: ! out_buffer; |
|
93 |
||
5038 | 94 |
fun exec () = |
5090 | 95 |
(case ! in_buffer of |
5038 | 96 |
[] => () |
5090 | 97 |
| _ => (PolyML.compiler (get, put) (); exec ())); |
98 |
in |
|
22144 | 99 |
exec () handle exn => |
100 |
(err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn); |
|
10914 | 101 |
if verbose then print (output ()) else () |
5090 | 102 |
end; |
3588 | 103 |
|
21770 | 104 |
fun use_file _ _ name = use name; |
105 |
||
11078 | 106 |
|
15832 | 107 |
(*eval command line arguments*) |
108 |
local |
|
109 |
fun println s = |
|
110 |
(TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut); |
|
21715 | 111 |
fun eval "-q" = () |
22144 | 112 |
| eval txt = use_text "" (println, println) false txt; |
15832 | 113 |
in |
114 |
val _ = PolyML.onEntry (fn () => app eval (CommandLine.arguments ())); |
|
115 |
end; |
|
116 |
||
117 |
||
11078 | 118 |
|
119 |
(** interrupts **) |
|
120 |
||
121 |
exception Interrupt = SML90.Interrupt; |
|
122 |
||
123 |
local |
|
124 |
||
125 |
val sig_int = 2; |
|
126 |
||
127 |
fun change_signal new_handler f x = |
|
128 |
let |
|
129 |
(*RACE wrt. other signals*) |
|
130 |
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
|
131 |
val result = Exn.capture f x; |
11078 | 132 |
val _ = Signal.signal (sig_int, old_handler); |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
133 |
in Exn.release result end; |
5813 | 134 |
|
11078 | 135 |
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ()); |
136 |
||
137 |
in |
|
138 |
||
139 |
val _ = Signal.signal (sig_int, default_handler); |
|
140 |
||
12989 | 141 |
fun ignore_interrupt f = change_signal Signal.SIG_IGN f; |
142 |
fun raise_interrupt f = change_signal default_handler f; |
|
11078 | 143 |
|
144 |
end; |
|
5813 | 145 |
|
146 |
||
147 |
||
3588 | 148 |
(** OS related **) |
2341 | 149 |
|
21281
0767e7dad549
no special treatment for cygwin (this is supposed to be actual cygwin, not win32 polyml within cygwin);
wenzelm
parents:
18814
diff
changeset
|
150 |
use "ML-Systems/polyml-posix.ML"; |
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
151 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
152 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
153 |
(* current directory *) |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
154 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
155 |
val cd = OS.FileSys.chDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
156 |
val pwd = OS.FileSys.getDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
157 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23139
diff
changeset
|
158 |
|
3588 | 159 |
(* system command execution *) |
2341 | 160 |
|
3588 | 161 |
(*execute Unix command which doesn't take any input from stdin and |
11078 | 162 |
sends its output to stdout; could be done more easily by Unix.execute, |
163 |
but that function doesn't use the PATH*) |
|
3588 | 164 |
fun execute command = |
165 |
let |
|
11078 | 166 |
val tmp_name = OS.FileSys.tmpName (); |
167 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
168 |
val result = TextIO.inputAll is; |
|
169 |
in |
|
170 |
TextIO.closeIn is; |
|
171 |
OS.FileSys.remove tmp_name; |
|
172 |
result |
|
173 |
end; |
|
7855 | 174 |
|
11078 | 175 |
(*plain version; with return code*) |
176 |
fun system cmd = |
|
177 |
if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1; |
|
2341 | 178 |
|
179 |
||
17824 | 180 |
(*Convert a process ID to a decimal string (chiefly for tracing)*) |
181 |
fun string_of_pid pid = |
|
23139
aa899bce7c3b
TextIO.inputLine: use present SML B library version;
wenzelm
parents:
22144
diff
changeset
|
182 |
Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid)); |
17824 | 183 |
|
184 |
||
3588 | 185 |
(* getenv *) |
186 |
||
11078 | 187 |
fun getenv var = |
188 |
(case OS.Process.getEnv var of |
|
189 |
NONE => "" |
|
190 |
| SOME txt => txt); |
|
15028 | 191 |
|
192 |
||
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
193 |
(* profile execution *) |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
194 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
195 |
local |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
196 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
197 |
fun no_profile () = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
198 |
RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0; |
15028 | 199 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
200 |
in |
15699
7d91dd712ff8
fixing an incompatibility with Posix.IO.mkTextReader
paulson
parents:
15028
diff
changeset
|
201 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
202 |
fun profile 0 f x = f x |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
203 |
| profile n f x = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
204 |
(RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n; |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
205 |
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
|
206 |
in no_profile (); y end); |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
207 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
208 |
end; |