Debian Codegraph @

basis-library/primitive/prim-mlton.sml

raw
1 (* Copyright (C) 2010-2011,2013-2014,2017,2019,2021-2022 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.
5 *
6 * MLton is released under a HPND-style license.
7 * See the file MLton-LICENSE for details.
8 *)
9
10 (* Primitive names are special -- see atoms/prim.fun. *)
11
12 structure Primitive = struct
13
14 open Primitive
15
16 structure MLton = struct
17
18 structure GCState =
19 struct
20 type t = Pointer.t
21
22 val gcState = _prim "GC_state": unit -> t;
23 end
24
25 val bug = _prim "MLton_bug": String8.string -> unit;
26 val eq = _prim "MLton_eq": 'a * 'a -> bool;
27 val equal = _prim "MLton_equal": 'a * 'a -> bool;
28 (* val deserialize = _prim "MLton_deserialize": Word8Vector.vector -> 'a ref; *)
29 val halt = _prim "MLton_halt": C_Status.t -> unit;
30 val hash = _prim "MLton_hash": 'a -> Word32.word;
31 (* val serialize = _prim "MLton_serialize": 'a ref -> Word8Vector.vector; *)
32 val share = _prim "MLton_share": 'a -> unit;
33 val size = _prim "MLton_size": 'a -> C_Size.t;
34 val sizeAll = _import "GC_sizeAll" runtime private: GCState.t -> C_Size.t;
35
36 val installSignalHandler =
37 _prim "MLton_installSignalHandler": unit -> unit;
38
39 structure Align =
40 struct
41 datatype t = Align4 | Align8
42
43 val align =
44 case _build_const "MLton_Align_align": Int32.int; of
45 4 => Align4
46 | 8 => Align8
47 | _ => raise Primitive.Exn.Fail8 "MLton_Align_align"
48 end
49
50 structure CallStack =
51 struct
52 (* The most recent caller is at index 0 in the array. *)
53 datatype t = T of Word32.word array
54
55 val callStack =
56 _import "GC_callStack" runtime private: GCState.t * Word32.word array -> unit;
57 val frameIndexSourceSeq =
58 _import "GC_frameIndexSourceSeq" private: GCState.t * Word32.word -> Pointer.t;
59 val keep = _command_line_const "CallStack.keep": bool = false;
60 val numStackFrames =
61 _import "GC_numStackFrames" runtime private: GCState.t -> Word32.word;
62 val sourceName = _import "GC_sourceName" private: GCState.t * Word32.word -> C_String.t;
63 end
64
65 structure Codegen =
66 struct
67 datatype t = AMD64 | C | LLVM | X86
68
69 val codegen =
70 case _build_const "MLton_Codegen_codegen": Int32.int; of
71 0 => C
72 | 1 => X86
73 | 2 => AMD64
74 | 3 => LLVM
75 | _ => raise Primitive.Exn.Fail8 "MLton_Codegen_codegen"
76
77 val isC = codegen = C
78 val isAMD64 = codegen = AMD64
79 val isLLVM = codegen = LLVM
80 val isX86 = codegen = X86
81 end
82
83 structure Exn =
84 struct
85 (* The polymorphism with extra and setInitExtra is because primitives
86 * are only supposed to deal with basic types. The polymorphism
87 * allows the various passes like monomorphisation to translate
88 * the types appropriately.
89 *)
90 type extra = CallStack.t option
91
92 val extra = _prim "Exn_extra": exn -> 'a;
93 val extra: exn -> extra = extra
94 val keepHistory = _command_line_const "Exn.keepHistory": bool = false;
95 val setExtendExtra = _prim "Exn_setExtendExtra": ('a -> 'a) -> unit;
96 val setExtendExtra: (extra -> extra) -> unit = setExtendExtra
97
98 (* Ensure that setExtendExtra is initialized.
99 * Important for -const 'Exn.keepHistory true', so that
100 * exceptions can be raised (and handled) during Basis Library
101 * initialization.
102 *)
103 val setExtendExtra : (extra -> extra) -> unit =
104 if keepHistory
105 then (setExtendExtra (fn _ => NONE)
106 ; setExtendExtra)
107 else fn _ => ()
108 end
109
110 structure FFI =
111 struct
112 val getOpArgsResPtr = _import "GC_getCallFromCOpArgsResPtr" private: GCState.t -> Pointer.t;
113 val numExports = _build_const "MLton_FFI_numExports": Int32.int;
114 end
115
116 structure Finalizable =
117 struct
118 val touch = _prim "MLton_touch": 'a -> unit;
119 end
120
121 structure GC =
122 struct
123 val collect = _prim "GC_collect": unit -> unit;
124 val pack = _import "GC_pack" runtime private: GCState.t -> unit;
125 val getBytesAllocated =
126 _import "GC_getCumulativeStatisticsBytesAllocated" private: GCState.t -> C_UIntmax.t;
127 val getNumCopyingGCs =
128 _import "GC_getCumulativeStatisticsNumCopyingGCs" private: GCState.t -> C_UIntmax.t;
129 val getNumMarkCompactGCs =
130 _import "GC_getCumulativeStatisticsNumMarkCompactGCs" private: GCState.t -> C_UIntmax.t;
131 val getNumMinorGCs =
132 _import "GC_getCumulativeStatisticsNumMinorGCs" private: GCState.t -> C_UIntmax.t;
133 val getLastBytesLive =
134 _import "GC_getLastMajorStatisticsBytesLive" private: GCState.t -> C_Size.t;
135 val getMaxBytesLive =
136 _import "GC_getCumulativeStatisticsMaxBytesLive" private: GCState.t -> C_Size.t;
137 val setHashConsDuringGC =
138 _import "GC_setHashConsDuringGC" private: GCState.t * bool -> unit;
139 val setMessages = _import "GC_setControlsMessages" private: GCState.t * bool -> unit;
140 val setRusageMeasureGC =
141 _import "GC_setControlsRusageMeasureGC" private: GCState.t * bool -> unit;
142 val setSummary = _import "GC_setControlsSummary" private: GCState.t * bool -> unit;
143 val unpack = _import "GC_unpack" runtime private: GCState.t -> unit;
144 end
145
146 structure Platform =
147 struct
148 structure Arch =
149 struct
150 val host = _const "MLton_Platform_Arch_host": String8.string;
151 val hostIsBigEndian = _const "MLton_Platform_Arch_bigendian": bool;
152 end
153
154 structure Format =
155 struct
156 val host = _build_const "MLton_Platform_Format": String8.string;
157 end
158
159 structure OS =
160 struct
161 val host = _const "MLton_Platform_OS_host": String8.string;
162 val cygwinUseMmap =
163 #1 _symbol "MLton_Platform_CygwinUseMmap" private: bool GetSet.t;
164 end
165 end
166
167 structure Pointer =
168 struct
169 open Pointer
170 type pointer = t
171
172 val add =
173 _prim "CPointer_add": t * C_Ptrdiff.t -> t;
174 val sub =
175 _prim "CPointer_sub": t * C_Ptrdiff.t -> t;
176 val diff =
177 _prim "CPointer_diff": t * t -> C_Ptrdiff.t;
178 val < = _prim "CPointer_lt": t * t -> bool;
179 local
180 structure S = IntegralComparisons(type t = t
181 val < = <)
182 in
183 open S
184 end
185
186 val fromWord =
187 _prim "CPointer_fromWord": C_Size.t -> t;
188 val toWord =
189 _prim "CPointer_toWord": t -> C_Size.t;
190
191 val null: t = fromWord 0w0
192
193 fun isNull p = p = null
194
195 val getCPointer = _prim "CPointer_getCPointer": t * C_Ptrdiff.t -> t;
196 val getInt8 = _prim "CPointer_getWord8": t * C_Ptrdiff.t -> Int8.int;
197 val getInt16 = _prim "CPointer_getWord16": t * C_Ptrdiff.t -> Int16.int;
198 val getInt32 = _prim "CPointer_getWord32": t * C_Ptrdiff.t -> Int32.int;
199 val getInt64 = _prim "CPointer_getWord64": t * C_Ptrdiff.t -> Int64.int;
200 val getObjptr = _prim "CPointer_getObjptr": t * C_Ptrdiff.t -> 'a;
201 val getReal32 = _prim "CPointer_getReal32": t * C_Ptrdiff.t -> Real32.real;
202 val getReal64 = _prim "CPointer_getReal64": t * C_Ptrdiff.t -> Real64.real;
203 val getWord8 = _prim "CPointer_getWord8": t * C_Ptrdiff.t -> Word8.word;
204 val getWord16 = _prim "CPointer_getWord16": t * C_Ptrdiff.t -> Word16.word;
205 val getWord32 = _prim "CPointer_getWord32": t * C_Ptrdiff.t -> Word32.word;
206 val getWord64 = _prim "CPointer_getWord64": t * C_Ptrdiff.t -> Word64.word;
207 val setCPointer = _prim "CPointer_setCPointer": t * C_Ptrdiff.t * t -> unit;
208 val setInt8 = _prim "CPointer_setWord8": t * C_Ptrdiff.t * Int8.int -> unit;
209 val setInt16 = _prim "CPointer_setWord16": t * C_Ptrdiff.t * Int16.int -> unit;
210 val setInt32 = _prim "CPointer_setWord32": t * C_Ptrdiff.t * Int32.int -> unit;
211 val setInt64 = _prim "CPointer_setWord64": t * C_Ptrdiff.t * Int64.int -> unit;
212 val setObjptr = _prim "CPointer_setObjptr": t * C_Ptrdiff.t * 'a -> unit;
213 val setReal32 = _prim "CPointer_setReal32": t * C_Ptrdiff.t * Real32.real -> unit;
214 val setReal64 = _prim "CPointer_setReal64": t * C_Ptrdiff.t * Real64.real -> unit;
215 val setWord8 = _prim "CPointer_setWord8": t * C_Ptrdiff.t * Word8.word -> unit;
216 val setWord16 = _prim "CPointer_setWord16": t * C_Ptrdiff.t * Word16.word -> unit;
217 val setWord32 = _prim "CPointer_setWord32": t * C_Ptrdiff.t * Word32.word -> unit;
218 val setWord64 = _prim "CPointer_setWord64": t * C_Ptrdiff.t * Word64.word -> unit;
219 end
220
221 structure Profile =
222 struct
223 val isOn = _build_const "MLton_Profile_isOn": bool;
224 structure Data =
225 struct
226 type t = Pointer.t
227
228 val dummy = Pointer.null
229 val free = _import "GC_profileFree" private: GCState.t * t -> unit;
230 val malloc = _import "GC_profileMalloc" private: GCState.t -> t;
231 val write =
232 _import "GC_profileWrite" private: GCState.t * t * NullString8.t -> unit;
233 end
234 val done = _import "GC_profileDone" private: GCState.t -> unit;
235 val getCurrent = _import "GC_getProfileCurrent" private: GCState.t -> Data.t;
236 val setCurrent = _import "GC_setProfileCurrent" private : GCState.t * Data.t -> unit;
237 end
238
239 structure Thread =
240 struct
241 type preThread = PreThread.t
242 type thread = Thread.t
243
244 val atomicState = _prim "Thread_atomicState": unit -> Word32.word;
245 val atomicBegin = _prim "Thread_atomicBegin": unit -> unit;
246 fun atomicEnd () =
247 if atomicState () = 0w0
248 then raise Primitive.Exn.Fail8 "Thread.atomicEnd"
249 else _prim "Thread_atomicEnd": unit -> unit; ()
250 val copy = _prim "Thread_copy": preThread -> thread;
251 (* copyCurrent's result is accesible via savedPre ().
252 * It is not possible to have the type of copyCurrent as
253 * unit -> preThread, because there are two different ways to
254 * return from the call to copyCurrent. One way is the direct
255 * obvious way, in the thread that called copyCurrent. That one,
256 * of course, wants to call savedPre (). However, another way to
257 * return is by making a copy of the preThread and then switching
258 * to it. In that case, there is no preThread to return. Making
259 * copyCurrent return a preThread creates nasty bugs where the
260 * return code from the CCall expects to see a preThread result
261 * according to the C return convention, but there isn't one when
262 * switching to a copy.
263 *)
264 val copyCurrent = _prim "Thread_copyCurrent": unit -> unit;
265 val current = _import "GC_getCurrentThread" private: GCState.t -> thread;
266 val finishSignalHandler = _import "GC_finishSignalHandler" private: GCState.t -> unit;
267 val returnToC = _prim "Thread_returnToC": unit -> unit;
268 val saved = _import "GC_getSavedThread" private: GCState.t -> thread;
269 val savedPre = _import "GC_getSavedThread" private: GCState.t -> preThread;
270 val setCallFromCHandler =
271 _import "GC_setCallFromCHandlerThread" private: GCState.t * thread -> unit;
272 val setSignalHandler =
273 _import "GC_setSignalHandlerThread" private: GCState.t * thread -> unit;
274 val setSaved = _import "GC_setSavedThread" private: GCState.t * thread -> unit;
275 val startSignalHandler = _import "GC_startSignalHandler" private: GCState.t -> unit;
276 val switchTo = _prim "Thread_switchTo": thread -> unit;
277 end
278
279 structure Weak =
280 struct
281 open Weak
282
283 val canGet = _prim "Weak_canGet": 'a t -> bool;
284 val get = _prim "Weak_get": 'a t -> 'a;
285 val new = _prim "Weak_new": 'a -> 'a t;
286 end
287
288 structure World =
289 struct
290 val getAmOriginal = _import "GC_getAmOriginal" private: GCState.t -> bool;
291 val setAmOriginal = _import "GC_setAmOriginal" private: GCState.t * bool -> unit;
292 val getSaveStatus = _import "GC_getSaveWorldStatus" private: GCState.t -> bool C_Errno.t;
293 (* save's result status is accesible via getSaveStatus ().
294 * It is not possible to have the type of save as
295 * NullString8.t -> bool C_Errno.t, because there are two
296 * different ways to return from the call to save. One way is
297 * the direct obvious way, in the program instance that called
298 * save. However, another way to return is in the program
299 * instance that loads the world. Making save return a bool
300 * creates nasty bugs where the return code from the CCall
301 * expects to see a bool result according to the C return
302 * convention, but there isn't one when returning in the load
303 * world.
304 *)
305 val save = _prim "World_save": NullString8.t -> unit;
306 end
307
308 end
309
310 structure GCState = MLton.GCState
311
312 end