1 (* Copyright (C
) 2013 Matthew Fluet
.
2 * Copyright (C
) 1999-2009 Henry Cejtin
, Matthew Fluet
, Suresh
3 * Jagannathan
, and Stephen Weeks
.
4 * Copyright (C
) 1997-2000 NEC Research Institute
.
6 * MLton is released under a BSD
-style license
.
7 * See the file MLton
-LICENSE for details
.
13 val openOut
: string -> outstream
18 fun inFd _
= raise Fail
"IO.inFd"
19 fun mkstemps
{prefix
, suffix
} =
21 val name
= concat
[prefix
, MLtonRandom
.alphaNumString
6, suffix
]
23 (* Make sure the temporary file name doesn
't already exist
. *)
24 if OS
.FileSys
.access (name
, [])
25 then mkstemps
{prefix
= prefix
, suffix
= suffix
}
26 else (name
, openOut name
)
28 fun mkstemp s
= mkstemps
{prefix
= s
, suffix
= ""}
29 fun newIn _
= raise Fail
"IO.newIn"
30 fun newOut _
= raise Fail
"IO.newOut"
31 fun outFd _
= raise Fail
"IO.outFd"
32 fun setIn _
= raise Fail
"IO.setIn"
33 fun tempPrefix _
= raise Fail
"IO.tempPrefix"
36 functor MkWord(W
: WORD
) : MLTON_WORD
=
41 val wordSize
= Word.fromInt wordSize
43 val bswap
= fn _
=> raise Fail
"Word.bswap"
44 fun rol (w
: word, w
': Word.word): word =
46 val w
' = Word.mod (w
', wordSize
)
48 orb (>> (w
, Word.- (wordSize
, w
')),
51 fun ror (w
: word, w
': Word.word): word =
53 val w
' = Word.mod (w
', wordSize
)
56 << (w
, Word.- (wordSize
, w
')))
61 (* This file is just a dummy provided
in place
of the
structure that MLton
62 * supplies so that we can compile under SML
/NJ
.
64 structure MLton
: MLTON
=
67 val eq
= fn _
=> raise Fail
"eq"
68 val equal
= fn _
=> raise Fail
"equal"
69 val hash
= fn _
=> raise Fail
"hash"
70 val isMLton
= MLton
.isMLton
72 val share
= fn _
=> raise Fail
"share"
73 val shareAll
= fn _
=> raise Fail
"shareAll"
80 fun unfoldi (n
, a
, f
) =
86 val (b
, a
') = f (i
, !r
)
96 structure BinIO = MkIO (BinIO)
100 val addExnMessager
= fn _
=> raise Fail
"Exn.addExnMessager"
101 val history
= MLton
.Exn
.history
103 val defaultTopLevelHandler
= fn _
=> raise Fail
"Exn.defaultTopLevelHandler"
104 val getTopLevelHandler
= fn _
=> raise Fail
"Exn.getTopLevelHandler"
105 val setTopLevelHandler
= fn _
=> raise Fail
"Exn.setTopLevelHandler"
106 val topLevelHandler
= fn _
=> raise Fail
"Exn.topLevelHandler"
109 structure Finalizable
=
113 fun addFinalizer _
= ()
114 fun finalizeBefore _
= ()
117 fun withValue (x
, f
) = f x
122 val collect
= MLton
.GC
.collect
123 val pack
= MLton
.GC
.pack
124 val setMessages
= MLton
.GC
.setMessages
125 fun setSummary _
= ()
128 structure Statistics
=
130 val bytesAllocated
= fn _
=> raise Fail
"GC.Statistics.bytesAllocated"
131 val lastBytesLive
= fn _
=> raise Fail
"GC.Statistics.lastBytesLive"
132 val numCopyingGCs
= fn _
=> raise Fail
"GC.Statistics.numCopyingGCs"
133 val numMarkCompactGCs
= fn _
=> raise Fail
"GC.Statistics.numMarkCompactGCs"
134 val numMinorGCs
= fn _
=> raise Fail
"GC.Statistics.numMinorGCs"
135 val maxBytesLive
= fn _
=> raise Fail
"GC.Statistics.maxBytesLive"
141 datatype t
= Prof |
Real | Virtual
143 fun signal _
= Posix
.Signal
.alrm
144 fun set _
= raise Fail
"Itimer.set"
149 fun peek (l
, f
) = List.find f l
150 fun omap (opt
, f
) = Option
.map f opt
156 val toLower
= CharVector
.map
Char.toLower
161 datatype t
= Alpha | AMD64 | ARM | ARM64 | HPPA | IA64 | m68k |
162 MIPS | PowerPC | PowerPC64 | S390 | Sparc | X86
164 val all
= [(Alpha
, "Alpha"),
172 (PowerPC
, "PowerPC"),
173 (PowerPC64
, "PowerPC64"),
180 val s
= String.toLower s
182 omap (peek (all
, fn (_
, s
') => s
= String.toLower s
'),
186 fun toString a
= #
2 (valOf (peek (all
, fn (a
', _
) => a
= a
')))
189 case fromString (MLton
.Platform
.Arch
.toString MLton
.Platform
.Arch
.host
) of
190 NONE
=> raise Fail
"MLton.Platform.Arch.host: strange arch"
209 val all
= [(AIX
, "AIX"),
212 (FreeBSD
, "FreeBSD"),
218 (OpenBSD
, "OpenBSD"),
219 (Solaris
, "Solaris")]
223 val s
= String.toLower s
225 omap (peek (all
, fn (_
, s
') => s
= String.toLower s
'),
229 fun toString a
= #
2 (valOf (peek (all
, fn (a
', _
) => a
= a
')))
232 case fromString (MLton
.Platform
.OS
.toString MLton
.Platform
.OS
.host
) of
233 NONE
=> raise Fail
"MLton.Platform.OS.host: strange os"
240 type gid
= Posix
.ProcEnv
.gid
242 fun setenv _
= raise Fail
"setenv"
243 fun setgroups _
= raise Fail
"setgroups"
248 type ('stdin
, 'stdout
, 'stderr
) t
= unit
255 exception MisuseOfForget
256 exception DoublyRedirected
260 type ('use
, 'dir
) t
= unit
262 val binIn
= fn _
=> raise Fail
"Child.binIn"
263 val binOut
= fn _
=> raise Fail
"Child.binOut"
264 val fd
= fn _
=> raise Fail
"Child.fd"
265 val remember
= fn _
=> raise Fail
"Child.remember"
266 val textIn
= fn _
=> raise Fail
"Child.textIn"
267 val textOut
= fn _
=> raise Fail
"Child.textOut"
272 type ('use
, 'dir
) t
= unit
274 val child
= fn _
=> raise Fail
"Param.child"
275 val fd
= fn _
=> raise Fail
"Param.fd"
276 val file
= fn _
=> raise Fail
"Param.file"
277 val forget
= fn _
=> raise Fail
"Param.forget"
283 val create
= fn _
=> raise Fail
"Process.create"
284 val getStderr
= fn _
=> raise Fail
"Process.getStderr"
285 val getStdin
= fn _
=> raise Fail
"Process.getStdin"
286 val getStdout
= fn _
=> raise Fail
"Process.getStdout"
287 val kill
= fn _
=> raise Fail
"Process.kill"
288 val reap
= fn _
=> raise Fail
"Process.reap"
290 type pid
= Posix
.Process
.pid
292 val atExit
= OS
.Process
.atExit
298 exit (if n
= 0 then success
else failure
)
301 fun spawne
{path
, args
, env
} =
302 case Posix
.Process
.fork () of
303 NONE
=> Posix
.Process
.exece (path
, args
, env
)
306 fun spawn
{path
, args
} =
307 spawne
{path
= path
, args
= args
, env
= Posix
.ProcEnv
.environ ()}
309 fun spawnp
{file
, args
} =
310 case Posix
.Process
.fork () of
311 NONE
=> Posix
.Process
.execp (file
, args
)
321 val equals
= fn _
=> raise Fail
"Profile.Data.equals"
322 val free
= fn _
=> raise Fail
"Profile.Data.free"
323 val malloc
= fn _
=> raise Fail
"Profile.Data.malloc"
324 val write
= fn _
=> raise Fail
"Profile.Data.write"
327 val withData
= fn _
=> raise Fail
"Profile.withData"
330 structure Random
= MLtonRandom
334 type t
= {stime
: Time
.time
, utime
: Time
.time
}
338 (* Fake it
with Posix
.ProcEnv
.times
339 * and Timer
.totalCPUTimer
and Timer
.checkCPUTimes
.
343 val zero
= {utime
= Time
.zeroTime
, stime
= Time
.zeroTime
}
346 val {gc
= {usr
= gcutime
, sys
= gcstime
}, ...} =
347 Timer
.checkCPUTimes (Timer
.totalCPUTimer ())
348 val {utime
, stime
, cutime
, cstime
, ...} =
349 Posix
.ProcEnv
.times ()
351 {self
= {utime
= utime
, stime
= stime
},
352 children
= {utime
= cutime
, stime
= cstime
},
353 gc
= {utime
= gcutime
, stime
= gcstime
}}
354 end handle Time
=> {children
= zero
, gc
= zero
, self
= zero
}
355 (* The
handle Time is there because
of a bug
in SML
/NJ that
356 * causes a Time
exception to be raised on machines
with a
357 * large
uptime (enough that the number
of clock ticks is
377 val handler
= fn _
=> ()
379 val isDefault
= fn _
=> raise Fail
"Signal.Handler.isDefault"
380 val isIgnore
= fn _
=> raise Fail
"Signal.Handler.isIgnore"
390 fun block _
= raise Fail
"Signal.Mask.block"
391 fun getBlocked _
= ()
392 fun isMember _
= raise Fail
"Signal.Mask.isMember"
394 fun setBlocked _
= raise Fail
"Signal.Mask.setBlocked"
396 fun unblock _
= raise Fail
"Signal.Mask.unblock"
399 fun getHandler _
= raise Fail
"Signal.getHandler"
400 fun handled _
= raise Fail
"Signal.handled"
401 val restart
= ref
true
402 fun setHandler _
= raise Fail
"Signal.setHandler"
403 fun suspend _
= raise Fail
"Signal.suspend"
406 structure TextIO = MkIO (TextIO)
408 structure Thread
= MLtonThread
416 val r
= ref (Array
.fromList
[])
419 if 0 <= i
andalso i
< !subLim
420 then Array
.sub (!r
, i
)
422 val updateLim
= ref
0
424 if 0 <= i
andalso i
< !updateLim
425 then if i
= !updateLim
andalso i
< n
426 then (r
:= (Array
.tabulate (i
+ 1, fn j
=>
429 else Array
.sub (!r
, j
)));
434 Array
.update (!r
, i
, x
)
435 val gotIt
= ref
false
438 raise Fail
"already got vector"
440 if n
= !updateLim
then
445 raise Fail
"vector not full"
452 fun unfoldi (n
, a
, f
) =
458 val (b
, a
') = f (i
, !r
)
478 datatype status
= Clone | Original
479 fun load _
= raise Fail
"World.load"
480 fun save _
= raise Fail
"World.save"
481 fun saveThread _
= raise Fail
"World.saveThread"
484 structure Word = MkWord(Word)
485 structure Word8 = MkWord(Word8)