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