author | paulson |
Fri, 05 Oct 2007 09:59:03 +0200 | |
changeset 24854 | 0ebcd575d3c6 |
parent 24851 | 4e304aac841a |
child 25023 | 52eb78ebb370 |
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"; |
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24598
diff
changeset
|
8 |
use "ML-Systems/multithreading.ML"; |
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24598
diff
changeset
|
9 |
use "ML-Systems/time_limit.ML"; |
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
10 |
|
24598 | 11 |
val ml_system_fix_ints = false; |
12 |
||
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
13 |
|
16374 | 14 |
(** ML system and platform related **) |
15 |
||
17077 | 16 |
(* String compatibility *) |
17 |
||
16516 | 18 |
(*low-level pointer equality*) |
19 |
val pointer_eq = Address.wordEq; |
|
16502 | 20 |
|
21 |
||
11078 | 22 |
(* old Poly/ML emulation *) |
2341 | 23 |
|
11078 | 24 |
local |
25 |
val orig_exit = exit; |
|
26 |
in |
|
27 |
open PolyML; |
|
28 |
val exit = orig_exit; |
|
29 |
fun quit () = exit 0; |
|
7148 | 30 |
end; |
6042 | 31 |
|
2341 | 32 |
|
11078 | 33 |
(* restore old-style character / string functions *) |
3588 | 34 |
|
11078 | 35 |
val ord = SML90.ord; |
36 |
val chr = SML90.chr; |
|
37 |
val explode = SML90.explode; |
|
38 |
val implode = SML90.implode; |
|
39 |
||
14870 | 40 |
|
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12989
diff
changeset
|
41 |
(* compiler-independent timing functions *) |
2341 | 42 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
43 |
fun start_timing () = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
44 |
let val CPUtimer = Timer.startCPUTimer(); |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
45 |
val time = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
46 |
in (CPUtimer,time) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
47 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
48 |
fun end_timing (CPUtimer, {sys,usr}) = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
49 |
let open Time (*...for Time.toString, Time.+ and Time.- *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
50 |
val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
51 |
in "User " ^ toString (usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
52 |
" All "^ toString (sys2-sys + usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
53 |
" secs" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
54 |
handle Time => "" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
55 |
end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
56 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
57 |
fun check_timer timer = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
58 |
let |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
59 |
val {sys, usr} = Timer.checkCPUTimer timer; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21281
diff
changeset
|
60 |
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
|
61 |
in (sys, usr, gc) end; |
2341 | 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 |
||
24290 | 69 |
(* toplevel pretty printing (see also Pure/pure_setup.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 |
||
24329 | 75 |
(*print depth*) |
76 |
local |
|
77 |
val depth = ref 10; |
|
78 |
in |
|
79 |
fun get_print_depth () = ! depth; |
|
80 |
fun print_depth n = (depth := n; PolyML.print_depth n); |
|
81 |
end; |
|
82 |
||
2341 | 83 |
|
3588 | 84 |
(* ML command execution -- 'eval' *) |
85 |
||
24598 | 86 |
fun use_text (tune: string -> string) name (print, err) verbose txt = |
3588 | 87 |
let |
24598 | 88 |
val in_buffer = ref (explode (tune txt)); |
5090 | 89 |
val out_buffer = ref ([]: string list); |
21715 | 90 |
fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs)); |
5090 | 91 |
|
5038 | 92 |
fun get () = |
5090 | 93 |
(case ! in_buffer of |
5038 | 94 |
[] => "" |
5090 | 95 |
| c :: cs => (in_buffer := cs; c)); |
96 |
fun put s = out_buffer := s :: ! out_buffer; |
|
97 |
||
5038 | 98 |
fun exec () = |
5090 | 99 |
(case ! in_buffer of |
5038 | 100 |
[] => () |
5090 | 101 |
| _ => (PolyML.compiler (get, put) (); exec ())); |
102 |
in |
|
22144 | 103 |
exec () handle exn => |
104 |
(err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn); |
|
10914 | 105 |
if verbose then print (output ()) else () |
5090 | 106 |
end; |
3588 | 107 |
|
24598 | 108 |
fun use_file tune output verbose name = |
109 |
let |
|
110 |
val instream = TextIO.openIn name; |
|
111 |
val txt = TextIO.inputAll instream before TextIO.closeIn instream; |
|
112 |
in use_text tune name output verbose txt end; |
|
21770 | 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" = () |
24598 | 120 |
| eval txt = use_text (fn x => x) "" (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 |
fun profile 0 f x = f x |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
206 |
| profile n f x = |
24851 | 207 |
let |
208 |
val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n; |
|
209 |
val res = Exn.capture f x; |
|
210 |
val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0; |
|
211 |
in Exn.release res end; |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
212 |