Debian Codegraph @

runtime/gen/gen-basis-ffi.sml

raw
1 (* Copyright (C) 2019-2020 Matthew Fluet.
2 * Copyright (C) 2004-2006, 2008 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 *
5 * MLton is released under a HPND-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9 structure List =
10 struct
11 open List
12
13 fun sort f l =
14 let
15 fun qsort l =
16 case l of
17 [] => []
18 | hd::tl =>
19 let
20 val (lt,eq,gt) =
21 List.foldr
22 (fn (x,(lt,eq,gt)) =>
23 case f (x,hd) of
24 LESS => (x::lt,eq,gt)
25 | EQUAL => (lt,x::eq,gt)
26 | GREATER => (lt,eq,x::gt))
27 ([],[],[])
28 tl
29 val lt = qsort lt
30 val gt = qsort gt
31 in
32 lt @ (hd :: eq) @ gt
33 end
34 in
35 qsort l
36 end
37 end
38
39 structure Substring =
40 struct
41 open Substring
42
43 fun droplSpace ss =
44 dropl Char.isSpace ss
45 fun droprSpace ss =
46 dropr Char.isSpace ss
47
48 end
49
50 structure Name =
51 struct
52 datatype t = T of string list
53
54 fun compare (T ss1, T ss2) =
55 List.collate
56 (fn (s1,s2) =>
57 String.compare (CharVector.map Char.toLower s1,
58 CharVector.map Char.toLower s2))
59 (ss1, ss2)
60
61 fun last (T ss) = List.last ss
62 fun toC (T ss) =
63 String.concatWith "_" ss
64 fun toML (T ss) =
65 String.concatWith "." ss
66
67 fun parse ss =
68 let
69 val ss = Substring.droplSpace ss
70 val (names, rest) =
71 Substring.splitl
72 (fn c => Char.isAlphaNum c
73 orelse c = #"." orelse c = #"_")
74 ss
75 val rest = Substring.droplSpace rest
76 in
77 if Substring.isEmpty names
78 then NONE
79 else let
80 val names = Substring.fields (fn c => #"." = c) names
81 val names = List.map Substring.string names
82 in
83 SOME (T names, rest)
84 end
85 end
86 end
87
88
89 structure Type =
90 struct
91 datatype t =
92 Array of t
93 | Base of Name.t
94 | Con of Name.t * t
95 | Ref of t
96 | Unit
97 | Vector of t
98
99 local
100 fun make s t =
101 case t of
102 Base name =>
103 Name.compare (name, Name.T [s, "t"]) = EQUAL
104 | _ => false
105 in
106 val isString = make "String8"
107 val isBool = make "Bool"
108 end
109
110 fun toC t =
111 case t of
112 Array t => concat ["Array(", toC t, ")"]
113 | Base name => Name.toC name
114 | Con (name, t) => concat [Name.toC name, "(", toC t, ")"]
115 | Ref t => concat ["Ref(", toC t, ")"]
116 | Unit => "void"
117 | Vector t => concat ["Vector(", toC t, ")"]
118 fun toML t =
119 case t of
120 Array t => concat ["(", toML t, ") array"]
121 | Base name => Name.toML name
122 | Con (name, t) => concat ["(", toML t, ") ", Name.toML name]
123 | Ref t => concat ["(", toML t, ") ref"]
124 | Unit => "unit"
125 | Vector t => concat ["(", toML t, ") vector"]
126
127 fun parse s =
128 let
129 fun loop (s, t) =
130 case Name.parse s of
131 NONE => (t, s)
132 | SOME (Name.T ["array"], rest) => loop (rest, Array t)
133 | SOME (Name.T ["ref"], rest) => loop (rest, Ref t)
134 | SOME (Name.T ["vector"], rest) => loop (rest, Vector t)
135 | SOME (name, rest) => loop (rest, Con (name, t))
136 in
137 case Name.parse s of
138 NONE => raise Fail (concat ["Type.parse: \"", Substring.string s, "\""])
139 | SOME (Name.T ["unit"], rest) => loop (rest, Unit)
140 | SOME (name, rest) => loop (rest, Base name)
141 end
142
143 fun parseFn s =
144 let
145 fun loop (s, args) =
146 let
147 val (arg, rest) = parse s
148 in
149 if Substring.isPrefix "*" rest
150 then let
151 val rest = #2 (Substring.splitAt (rest, 1))
152 in
153 loop (rest, arg::args)
154 end
155 else if Substring.isPrefix "->" rest
156 then let
157 val rest = #2 (Substring.splitAt (rest, 2))
158 val (ret, rest) = parse rest
159 in
160 ({args = List.rev (arg::args),
161 ret = ret},
162 rest)
163 end
164 else raise Fail (concat ["Type.parseFn: \"", Substring.string s, "\""])
165 end
166 in
167 loop (s, [])
168 end
169 end
170
171 structure Entry =
172 struct
173 datatype t =
174 Const of {name: Name.t,
175 ty: Type.t}
176 | Import of {attrs: string,
177 name: Name.t,
178 ty: {args: Type.t list,
179 ret: Type.t}}
180 | SymConst of {name: Name.t,
181 ty: Type.t}
182 | Symbol of {name: Name.t,
183 ty: Type.t}
184
185 fun name entry =
186 case entry of
187 Const {name,...} => name
188 | Import {name,...} => name
189 | SymConst {name, ...} => name
190 | Symbol {name,...} => name
191
192 fun compare (entry1, entry2) =
193 Name.compare (name entry1, name entry2)
194
195 fun toC entry =
196 case entry of
197 Const {name, ty} =>
198 String.concat
199 ["PRIVATE extern const ",
200 Type.toC ty,
201 " ",
202 Name.toC name,
203 ";"]
204 | Import {attrs, name, ty = {args, ret}} =>
205 String.concat
206 ["PRIVATE ",
207 attrs,
208 if String.size attrs > 0 then " " else "",
209 Type.toC ret,
210 " ",
211 Name.toC name,
212 "(",
213 String.concatWith "," (List.map Type.toC args),
214 ");"]
215 | SymConst {name, ty} =>
216 String.concat
217 ["PRIVATE extern const ",
218 Type.toC ty,
219 " ",
220 Name.toC name,
221 ";"]
222 | Symbol {name, ty} =>
223 String.concat
224 ["PRIVATE extern ",
225 Type.toC ty,
226 " ",
227 Name.toC name,
228 ";"]
229 fun toML entry =
230 case entry of
231 Const {name, ty} =>
232 String.concat
233 ["val ",
234 Name.last name,
235 " = _const \"",
236 Name.toC name,
237 "\" : ",
238 Type.toML ty,
239 ";"]
240 | Import {attrs, name, ty = {args, ret}} =>
241 String.concat
242 ["val ",
243 Name.last name,
244 " = _import \"",
245 Name.toC name,
246 "\" private ",
247 if List.exists (fn s => s = "INLINE") (String.tokens Char.isSpace attrs)
248 then "inline " else "",
249 ": ",
250 String.concatWith " * " (List.map Type.toML args),
251 " -> ",
252 Type.toML ret,
253 ";"]
254 | SymConst {name, ty} =>
255 String.concat
256 ["val ",
257 Name.last name,
258 " = #1 (_symbol \"",
259 Name.toC name,
260 "\" private : (unit -> (",
261 Type.toML ty,
262 ")) * ((",
263 Type.toML ty,
264 ") -> unit);) ()"]
265 | Symbol {name, ty} =>
266 String.concat
267 ["val (",
268 Name.last name,
269 "Get, ",
270 Name.last name,
271 "Set) = _symbol \"",
272 Name.toC name,
273 "\" private : (unit -> (",
274 Type.toML ty,
275 ")) * ((",
276 Type.toML ty,
277 ") -> unit);"]
278
279 fun parseType (s, kw) =
280 let
281 val s = #2 (Substring.splitAt (s, 1 + String.size kw))
282 val s = Substring.droplSpace s
283 val s = if Substring.isPrefix ":" s
284 then #2 (Substring.splitAt (s, 1))
285 else raise Fail (concat ["Entry.parse", kw, ": \"", Substring.string s, "\""])
286 val (ret, rest) = Type.parse s
287 val () = if Substring.isEmpty rest
288 then ()
289 else raise Fail (concat ["Entry.parse", kw, ": \"", Substring.string s, "\""])
290 in
291 ret
292 end
293
294
295 fun parseConst (s, name) =
296 let
297 val ty = parseType (s, "Const")
298 in
299 Const {name = name,
300 ty = ty}
301 end
302
303 fun parseImport (s, name) =
304 let
305 val s = #2 (Substring.splitAt (s, 7))
306 val s = Substring.droplSpace s
307 val (attrs, s) =
308 case CharVectorSlice.findi (fn (_, c) => c = #":") s of
309 NONE => raise Fail (concat ["Entry.parseImport: \"", Substring.string s, "\""])
310 | SOME (i, _) => Substring.splitAt (s, i)
311 val attrs = Substring.droprSpace attrs
312 val s = if Substring.isPrefix ":" s
313 then #2 (Substring.splitAt (s, 1))
314 else raise Fail (concat ["Entry.parseImport: \"", Substring.string s, "\""])
315 val ({args, ret}, rest) = Type.parseFn s
316 val () = if Substring.isEmpty rest
317 then ()
318 else raise Fail (concat ["Entry.parseImport: \"", Substring.string s, "\""])
319 in
320 Import {attrs = Substring.string attrs,
321 name = name,
322 ty = {args = args, ret = ret}}
323 end
324
325 fun parseSymConst (s, name) =
326 let
327 val ty = parseType (s, "SymConst")
328 in
329 SymConst {name = name,
330 ty = ty}
331 end
332
333 fun parseSymbol (s, name) =
334 let
335 val ty = parseType (s, "Symbol")
336 in
337 Symbol {name = name,
338 ty = ty}
339 end
340
341 fun parse s =
342 case Name.parse s of
343 NONE => raise Fail "Entry.parse"
344 | SOME (name, rest) =>
345 if Substring.isPrefix "=" rest
346 then let
347 val rest = #2 (Substring.splitAt (rest, 1))
348 val rest = Substring.droplSpace rest
349 in
350 if Substring.isPrefix "_const" rest
351 then parseConst (rest, name)
352 else if Substring.isPrefix "_import" rest
353 then parseImport (rest, name)
354 else if Substring.isPrefix "_symconst" rest
355 then parseSymConst (rest, name)
356 else if Substring.isPrefix "_symbol" rest
357 then parseSymbol (rest, name)
358 else raise Fail (concat ["Entry.parse: \"", Substring.string s, "\""])
359 end
360 else raise Fail (concat ["Entry.parse: \"", Substring.string s, "\""])
361 end
362
363 val entries =
364 let
365 val f = TextIO.stdIn
366 fun loop entries =
367 case TextIO.inputLine f of
368 NONE => List.rev entries
369 | SOME s =>
370 if String.isPrefix "#" s
371 then loop entries
372 else let
373 val entry = Entry.parse (Substring.full s)
374 in
375 loop (entry :: entries)
376 end
377 val entries = loop []
378 val () = TextIO.closeIn f
379 val entries = List.sort Entry.compare entries
380 in
381 entries
382 end
383
384 fun outputC entries =
385 let
386 fun println s = if s <> "" then (print s; print "\n") else ()
387
388 val () = println "/* This file is automatically generated. Do not edit. */\n"
389 val () = println "#ifndef _MLTON_BASIS_FFI_H_"
390 val () = println "#define _MLTON_BASIS_FFI_H_"
391 val () = List.app (fn entry => println (Entry.toC entry)) entries
392 val () = println "#endif /* _MLTON_BASIS_FFI_H_ */"
393 in
394 ()
395 end
396
397 fun outputML entries =
398 let
399 fun println s = if s <> "" then (print s; print "\n") else ()
400
401 val primStrs =
402 (List.map (fn n => "Char" ^ n) ["8", "16", "32"]) @
403 (List.map (fn n => "Int" ^ n) ["8", "16", "32", "64"]) @
404 (List.map (fn n => "Real" ^ n) ["32", "64"]) @
405 (List.map (fn n => "Word" ^ n) ["8", "16", "32", "64"])
406
407 val () = println "(* This file is automatically generated. Do not edit. *)\n"
408 val () = println "local open Primitive in "
409 val () = println "structure PrimitiveFFI ="
410 val () = println "struct"
411 val cur =
412 List.foldl
413 (fn (entry, cur) =>
414 let
415 val Name.T names = Entry.name entry
416 val str = List.rev (List.tl (List.rev names))
417 fun loop (cur, str) =
418 case (cur, str) of
419 ([], []) => ()
420 | ([], str) =>
421 List.app (fn s =>
422 (println ("structure " ^ s ^ " = ")
423 ; println "struct"
424 ; if List.exists (fn s' => s = s') primStrs
425 then println ("type t = " ^ s ^ ".t")
426 else ()))
427 str
428 | (cur, []) =>
429 List.app (fn _ => println "end") cur
430 | (c::cur,s::str) =>
431 if c = s
432 then loop (cur, str)
433 else (println "end"
434 ; loop (cur, s::str))
435 in
436 loop (cur, str)
437 ; println (Entry.toML entry)
438 ; str
439 end)
440 []
441 entries
442 val () = List.app (fn _ => println "end") cur
443 val () = println "end"
444 val () = println "end"
445 in
446 ()
447 end
448
449 fun outputGenConsts entries =
450 let
451 fun println s = if s <> "" then (print s; print "\n") else ()
452
453 val () = println "/* This file is automatically generated. Do not edit. */\n"
454 val () = List.app (fn entry =>
455 case entry of
456 Entry.Const {name, ty} =>
457 if Type.isBool ty
458 then println (concat ["MkBoolConst (",
459 Name.toC name,
460 ");"])
461 else if Type.isString ty
462 then println (concat ["MkStrConst (",
463 Name.toC name,
464 ");"])
465 else println (concat ["MkNumConst (",
466 Name.toC name,
467 ", ",
468 Type.toC ty,
469 ");"])
470 | _ => ())
471 entries
472 in
473 ()
474 end
475
476 val () =
477 case CommandLine.arguments () of
478 ["basis-ffi.h"] => outputC entries
479 | ["basis-ffi.sml"] => outputML entries
480 | ["gen-basis-ffi-consts.c"] => outputGenConsts entries
481 | _ => (TextIO.output (TextIO.stdErr,
482 concat ["usage: ",
483 CommandLine.name (),
484 " basis-ffi.h|basis-ffi.sml|gen-basis-ffi-consts.c\n"])
485 ; OS.Process.exit OS.Process.failure)