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