annotate src/compiler.sml @ 765:a28982de5645

Successfully influenced effectful-ness status of FFI func
author Adam Chlipala <adamc@hcoop.net>
date Sat, 02 May 2009 11:27:26 -0400
parents 7f653298dd66
children df09c95085f8
rev   line source
adamc@764 1 (* Copyright (c) 2008-2009, Adam Chlipala
adamc@1 2 * All rights reserved.
adamc@1 3 *
adamc@1 4 * Redistribution and use in source and binary forms, with or without
adamc@1 5 * modification, are permitted provided that the following conditions are met:
adamc@1 6 *
adamc@1 7 * - Redistributions of source code must retain the above copyright notice,
adamc@1 8 * this list of conditions and the following disclaimer.
adamc@1 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@1 10 * this list of conditions and the following disclaimer in the documentation
adamc@1 11 * and/or other materials provided with the distribution.
adamc@1 12 * - The names of contributors may not be used to endorse or promote products
adamc@1 13 * derived from this software without specific prior written permission.
adamc@1 14 *
adamc@1 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@1 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@1 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@1 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@1 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@1 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@1 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@1 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@1 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@1 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@1 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@1 26 *)
adamc@1 27
adamc@1 28 structure Compiler :> COMPILER = struct
adamc@1 29
adamc@244 30 structure UrwebLrVals = UrwebLrValsFn(structure Token = LrParser.Token)
adamc@244 31 structure Lex = UrwebLexFn(structure Tokens = UrwebLrVals.Tokens)
adamc@244 32 structure UrwebP = Join(structure ParserData = UrwebLrVals.ParserData
adamc@1 33 structure Lex = Lex
adamc@1 34 structure LrParser = LrParser)
adamc@1 35
adamc@270 36 type job = {
adamc@385 37 prefix : string,
adamc@270 38 database : string option,
adamc@274 39 sources : string list,
adamc@274 40 exe : string,
adamc@274 41 sql : string option,
adamc@502 42 debug : bool,
adamc@673 43 profile : bool,
adamc@764 44 timeout : int,
adamc@764 45 ffi : string list,
adamc@764 46 link : string list,
adamc@765 47 headers : string list,
adamc@765 48 clientToServer : Settings.ffi list,
adamc@765 49 effectful : Settings.ffi list,
adamc@765 50 clientOnly : Settings.ffi list,
adamc@765 51 serverOnly : Settings.ffi list,
adamc@765 52 jsFuncs : (Settings.ffi * string) list
adamc@270 53 }
adamc@201 54
adamc@201 55 type ('src, 'dst) phase = {
adamc@201 56 func : 'src -> 'dst,
adamc@201 57 print : 'dst -> Print.PD.pp_desc
adamc@201 58 }
adamc@201 59
adamc@201 60 type pmap = (string * Time.time) list
adamc@201 61
adamc@201 62 type ('src, 'dst) transform = {
adamc@201 63 func : 'src -> 'dst option,
adamc@201 64 print : 'dst -> Print.PD.pp_desc,
adamc@201 65 time : 'src * pmap -> 'dst option * pmap
adamc@201 66 }
adamc@201 67
adamc@201 68 fun transform (ph : ('src, 'dst) phase) name = {
adamc@201 69 func = fn input => let
adamc@201 70 val v = #func ph input
adamc@201 71 in
adamc@201 72 if ErrorMsg.anyErrors () then
adamc@201 73 NONE
adamc@201 74 else
adamc@201 75 SOME v
adamc@201 76 end,
adamc@201 77 print = #print ph,
adamc@201 78 time = fn (input, pmap) => let
adamc@201 79 val befor = Time.now ()
adamc@201 80 val v = #func ph input
adamc@201 81 val elapsed = Time.- (Time.now (), befor)
adamc@201 82 in
adamc@201 83 (if ErrorMsg.anyErrors () then
adamc@201 84 NONE
adamc@201 85 else
adamc@201 86 SOME v,
adamc@201 87 (name, elapsed) :: pmap)
adamc@201 88 end
adamc@201 89 }
adamc@201 90
adamc@270 91 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@201 92 func = fn input => case #func tr1 input of
adamc@201 93 NONE => NONE
adamc@201 94 | SOME v => #func tr2 v,
adamc@201 95 print = #print tr2,
adamc@201 96 time = fn (input, pmap) => let
adamc@201 97 val (ro, pmap) = #time tr1 (input, pmap)
adamc@201 98 in
adamc@201 99 case ro of
adamc@201 100 NONE => (NONE, pmap)
adamc@201 101 | SOME v => #time tr2 (v, pmap)
adamc@201 102 end
adamc@201 103 }
adamc@201 104
adamc@346 105 fun check (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@346 106 ignore (#func tr x))
adamc@346 107
adamc@280 108 fun run (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@280 109 #func tr x)
adamc@201 110
adamc@201 111 fun runPrint (tr : ('src, 'dst) transform) input =
adamc@280 112 (ErrorMsg.resetErrors ();
adamc@280 113 case #func tr input of
adamc@280 114 NONE => print "Failure\n"
adamc@280 115 | SOME v =>
adamc@280 116 (print "Success\n";
adamc@280 117 Print.print (#print tr v);
adamc@280 118 print "\n"))
adamc@201 119
adamc@201 120 fun time (tr : ('src, 'dst) transform) input =
adamc@55 121 let
adamc@201 122 val (_, pmap) = #time tr (input, [])
adamc@201 123 in
adamc@201 124 app (fn (name, time) =>
adamc@201 125 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 126 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 127 print "\n"
adamc@201 128 end
adamc@55 129
adamc@201 130 fun timePrint (tr : ('src, 'dst) transform) input =
adamc@201 131 let
adamc@201 132 val (ro, pmap) = #time tr (input, [])
adamc@55 133 in
adamc@201 134 app (fn (name, time) =>
adamc@201 135 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 136 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 137 print "\n";
adamc@201 138 case ro of
adamc@201 139 NONE => print "Failure\n"
adamc@201 140 | SOME v =>
adamc@201 141 (print "Success\n";
adamc@201 142 Print.print (#print tr v);
adamc@201 143 print "\n")
adamc@55 144 end
adamc@55 145
adamc@244 146 val parseUrs =
adamc@201 147 {func = fn filename => let
adamc@201 148 val fname = OS.FileSys.tmpName ()
adamc@201 149 val outf = TextIO.openOut fname
adamc@201 150 val () = TextIO.output (outf, "sig\n")
adamc@201 151 val inf = TextIO.openIn filename
adamc@201 152 fun loop () =
adamc@201 153 case TextIO.inputLine inf of
adamc@201 154 NONE => ()
adamc@201 155 | SOME line => (TextIO.output (outf, line);
adamc@201 156 loop ())
adamc@201 157 val () = loop ()
adamc@201 158 val () = TextIO.closeIn inf
adamc@201 159 val () = TextIO.closeOut outf
adamc@201 160
adamc@201 161 val () = (ErrorMsg.resetErrors ();
adamc@201 162 ErrorMsg.resetPositioning filename;
adamc@201 163 Lex.UserDeclarations.initialize ())
adamc@201 164 val file = TextIO.openIn fname
adamc@201 165 fun get _ = TextIO.input file
adamc@201 166 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 167 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 168 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 169 in
adamc@201 170 TextIO.closeIn file;
adamc@201 171 case absyn of
adamc@201 172 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
adamc@201 173 | _ => (ErrorMsg.errorAt {file = filename,
adamc@201 174 first = {line = 0,
adamc@201 175 char = 0},
adamc@201 176 last = {line = 0,
adamc@201 177 char = 0}} "Not a signature";
adamc@201 178 [])
adamc@201 179 end
adamc@201 180 handle LrParser.ParseError => [],
adamc@201 181 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
adamc@55 182
adamc@1 183 (* The main parsing routine *)
adamc@244 184 val parseUr = {
adamc@201 185 func = fn filename =>
adamc@201 186 let
adamc@201 187 val () = (ErrorMsg.resetErrors ();
adamc@201 188 ErrorMsg.resetPositioning filename;
adamc@201 189 Lex.UserDeclarations.initialize ())
adamc@201 190 val file = TextIO.openIn filename
adamc@201 191 fun get _ = TextIO.input file
adamc@201 192 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 193 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 194 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 195 in
adamc@201 196 TextIO.closeIn file;
adamc@201 197 case absyn of
adamc@201 198 [(Source.DSgn ("?", _), _)] =>
adamc@201 199 (ErrorMsg.errorAt {file = filename,
adamc@201 200 first = {line = 0,
adamc@201 201 char = 0},
adamc@201 202 last = {line = 0,
adamc@201 203 char = 0}} "File starts with 'sig'";
adamc@201 204 [])
adamc@201 205 | _ => absyn
adamc@201 206 end
adamc@201 207 handle LrParser.ParseError => [],
adamc@201 208 print = SourcePrint.p_file}
adamc@56 209
adamc@765 210 fun p_job {prefix, database, exe, sql, sources, debug, profile,
adamc@765 211 timeout, ffi, link, headers,
adamc@765 212 clientToServer, effectful, clientOnly, serverOnly, jsFuncs} =
adamc@270 213 let
adamc@270 214 open Print.PD
adamc@270 215 open Print
adamc@765 216
adamc@765 217 fun p_ffi name = p_list_sep (box []) (fn (m, s) =>
adamc@765 218 box [string name, space, string m, string ".", string s, newline])
adamc@270 219 in
adamc@274 220 box [if debug then
adamc@274 221 box [string "DEBUG", newline]
adamc@274 222 else
adamc@274 223 box [],
adamc@502 224 if profile then
adamc@502 225 box [string "PROFILE", newline]
adamc@502 226 else
adamc@502 227 box [],
adamc@274 228 case database of
adamc@270 229 NONE => string "No database."
adamc@270 230 | SOME db => string ("Database: " ^ db),
adamc@270 231 newline,
adamc@274 232 string "Exe: ",
adamc@274 233 string exe,
adamc@274 234 newline,
adamc@274 235 case sql of
adamc@274 236 NONE => string "No SQL file."
adamc@274 237 | SOME sql => string ("SQL fle: " ^ sql),
adamc@673 238 newline,
adamc@673 239 string "Timeout: ",
adamc@673 240 string (Int.toString timeout),
adamc@673 241 newline,
adamc@764 242 p_list_sep (box []) (fn s => box [string "Ffi", space, string s, newline]) ffi,
adamc@764 243 p_list_sep (box []) (fn s => box [string "Header", space, string s, newline]) headers,
adamc@764 244 p_list_sep (box []) (fn s => box [string "Link", space, string s, newline]) link,
adamc@765 245 p_ffi "ClientToServer" clientToServer,
adamc@765 246 p_ffi "Effectful" effectful,
adamc@765 247 p_ffi "ClientOnly" clientOnly,
adamc@765 248 p_ffi "ServerOnly" serverOnly,
adamc@765 249 p_list_sep (box []) (fn ((m, s), s') =>
adamc@765 250 box [string "JsFunc", space, string m, string ".", string s,
adamc@765 251 space, string "=", space, string s', newline]) jsFuncs,
adamc@270 252 string "Sources:",
adamc@270 253 p_list string sources,
adamc@270 254 newline]
adamc@270 255 end
adamc@270 256
adamc@270 257 fun trim s =
adamc@270 258 let
adamc@270 259 val (_, s) = Substring.splitl Char.isSpace s
adamc@270 260 val (s, _) = Substring.splitr Char.isSpace s
adamc@270 261 in
adamc@270 262 s
adamc@270 263 end
adamc@270 264
adamc@270 265 val parseUrp = {
adamc@270 266 func = fn filename =>
adamc@270 267 let
adamc@270 268 val dir = OS.Path.dir filename
adamc@270 269 val inf = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@270 270
adamc@274 271 fun relify fname =
adamc@274 272 OS.Path.concat (dir, fname)
adamc@274 273 handle OS.Path.Path => fname
adamc@274 274
adamc@764 275 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
adamc@764 276
adamc@764 277 fun relifyA fname = OS.Path.mkAbsolute {path = fname, relativeTo = absDir}
adamc@764 278
adamc@270 279 fun readSources acc =
adamc@270 280 case TextIO.inputLine inf of
adamc@270 281 NONE => rev acc
adamc@270 282 | SOME line =>
adamc@270 283 let
adamc@270 284 val acc = if CharVector.all Char.isSpace line then
adamc@270 285 acc
adamc@270 286 else
adamc@270 287 let
adamc@270 288 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adamc@270 289 (String.explode line))
adamc@274 290 val fname = relify fname
adamc@270 291 in
adamc@270 292 fname :: acc
adamc@270 293 end
adamc@270 294 in
adamc@270 295 readSources acc
adamc@270 296 end
adamc@270 297
adamc@764 298 val prefix = ref NONE
adamc@764 299 val database = ref NONE
adamc@764 300 val exe = ref NONE
adamc@764 301 val sql = ref NONE
adamc@764 302 val debug = ref false
adamc@764 303 val profile = ref false
adamc@764 304 val timeout = ref NONE
adamc@764 305 val ffi = ref []
adamc@764 306 val link = ref []
adamc@764 307 val headers = ref []
adamc@765 308 val clientToServer = ref []
adamc@765 309 val effectful = ref []
adamc@765 310 val clientOnly = ref []
adamc@765 311 val serverOnly = ref []
adamc@765 312 val jsFuncs = ref []
adamc@764 313
adamc@764 314 fun finish sources =
adamc@764 315 {prefix = Option.getOpt (!prefix, "/"),
adamc@764 316 database = !database,
adamc@764 317 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adamc@764 318 ext = SOME "exe"}),
adamc@764 319 sql = !sql,
adamc@764 320 debug = !debug,
adamc@764 321 profile = !profile,
adamc@764 322 timeout = Option.getOpt (!timeout, 60),
adamc@765 323 ffi = rev (!ffi),
adamc@765 324 link = rev (!link),
adamc@765 325 headers = rev (!headers),
adamc@765 326 clientToServer = rev (!clientToServer),
adamc@765 327 effectful = rev (!effectful),
adamc@765 328 clientOnly = rev (!clientOnly),
adamc@765 329 serverOnly = rev (!serverOnly),
adamc@765 330 jsFuncs = rev (!jsFuncs),
adamc@274 331 sources = sources}
adamc@274 332
adamc@764 333 fun read () =
adamc@270 334 case TextIO.inputLine inf of
adamc@764 335 NONE => finish []
adamc@764 336 | SOME "\n" => finish (readSources [])
adamc@270 337 | SOME line =>
adamc@270 338 let
adamc@270 339 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adamc@270 340 val cmd = Substring.string (trim cmd)
adamc@270 341 val arg = Substring.string (trim arg)
adamc@765 342
adamc@765 343 fun ffiS () =
adamc@765 344 case String.fields (fn ch => ch = #".") arg of
adamc@765 345 [m, x] => (m, x)
adamc@765 346 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
adamc@765 347 ("", ""))
adamc@765 348
adamc@765 349 fun ffiM () =
adamc@765 350 case String.fields (fn ch => ch = #"=") arg of
adamc@765 351 [f, s] =>
adamc@765 352 (case String.fields (fn ch => ch = #".") f of
adamc@765 353 [m, x] => ((m, x), s)
adamc@765 354 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adamc@765 355 (("", ""), "")))
adamc@765 356 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adamc@765 357 (("", ""), ""))
adamc@270 358 in
adamc@270 359 case cmd of
adamc@385 360 "prefix" =>
adamc@764 361 (case !prefix of
adamc@385 362 NONE => ()
adamc@385 363 | SOME _ => ErrorMsg.error "Duplicate 'prefix' directive";
adamc@764 364 prefix := SOME arg)
adamc@385 365 | "database" =>
adamc@764 366 (case !database of
adamc@270 367 NONE => ()
adamc@270 368 | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
adamc@764 369 database := SOME arg)
adamc@274 370 | "exe" =>
adamc@764 371 (case !exe of
adamc@274 372 NONE => ()
adamc@274 373 | SOME _ => ErrorMsg.error "Duplicate 'exe' directive";
adamc@764 374 exe := SOME (relify arg))
adamc@274 375 | "sql" =>
adamc@764 376 (case !sql of
adamc@274 377 NONE => ()
adamc@274 378 | SOME _ => ErrorMsg.error "Duplicate 'sql' directive";
adamc@764 379 sql := SOME (relify arg))
adamc@764 380 | "debug" => debug := true
adamc@764 381 | "profile" => profile := true
adamc@673 382 | "timeout" =>
adamc@764 383 (case !timeout of
adamc@673 384 NONE => ()
adamc@673 385 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adamc@764 386 timeout := SOME (valOf (Int.fromString arg)))
adamc@764 387 | "ffi" => ffi := relify arg :: !ffi
adamc@764 388 | "link" => link := relifyA arg :: !link
adamc@764 389 | "include" => headers := relifyA arg :: !headers
adamc@765 390 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
adamc@765 391 | "effectful" => effectful := ffiS () :: !effectful
adamc@765 392 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
adamc@765 393 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
adamc@765 394 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
adamc@764 395 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adamc@764 396 read ()
adamc@270 397 end
adamc@385 398
adamc@764 399 val job = read ()
adamc@270 400 in
adamc@385 401 TextIO.closeIn inf;
adamc@764 402 Settings.setUrlPrefix (#prefix job);
adamc@764 403 Settings.setTimeout (#timeout job);
adamc@764 404 Settings.setHeaders (#headers job);
adamc@765 405 Settings.setClientToServer (#clientToServer job);
adamc@765 406 Settings.setEffectful (#effectful job);
adamc@765 407 Settings.setClientOnly (#clientOnly job);
adamc@765 408 Settings.setServerOnly (#serverOnly job);
adamc@765 409 Settings.setJsFuncs (#jsFuncs job);
adamc@385 410 job
adamc@270 411 end,
adamc@270 412 print = p_job
adamc@270 413 }
adamc@270 414
adamc@270 415 val toParseJob = transform parseUrp "parseJob"
adamc@270 416
adamc@56 417 fun capitalize "" = ""
adamc@56 418 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 419
adamc@201 420 val parse = {
adamc@764 421 func = fn {database, sources = fnames, ffi, ...} : job =>
adamc@201 422 let
adamc@201 423 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 424
adamc@764 425 fun parseFfi fname =
adamc@764 426 let
adamc@764 427 val mname = nameOf fname
adamc@764 428 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@764 429
adamc@764 430 val loc = {file = urs,
adamc@764 431 first = ErrorMsg.dummyPos,
adamc@764 432 last = ErrorMsg.dummyPos}
adamc@764 433
adamc@764 434 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
adamc@764 435 in
adamc@764 436 (Source.DFfiStr (mname, sgn), loc)
adamc@764 437 end
adamc@764 438
adamc@201 439 fun parseOne fname =
adamc@201 440 let
adamc@201 441 val mname = nameOf fname
adamc@244 442 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 443 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 444
adamc@201 445 val sgnO =
adamc@244 446 if Posix.FileSys.access (urs, []) then
adamc@244 447 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 448 {file = urs,
adamc@201 449 first = ErrorMsg.dummyPos,
adamc@201 450 last = ErrorMsg.dummyPos})
adamc@201 451 else
adamc@201 452 NONE
adamc@56 453
adamc@244 454 val loc = {file = ur,
adamc@201 455 first = ErrorMsg.dummyPos,
adamc@201 456 last = ErrorMsg.dummyPos}
adamc@56 457
adamc@244 458 val ds = #func parseUr ur
adamc@201 459 in
adamc@201 460 (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@201 461 end
adamc@56 462
adamc@764 463 val dsFfi = map parseFfi ffi
adamc@201 464 val ds = map parseOne fnames
adamc@201 465 in
adamc@201 466 let
adamc@201 467 val final = nameOf (List.last fnames)
adamc@271 468
adamc@764 469 val ds = dsFfi @ ds
adamc@764 470 @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
adamc@201 471 in
adamc@271 472 case database of
adamc@271 473 NONE => ds
adamc@271 474 | SOME s => (Source.DDatabase s, ErrorMsg.dummySpan) :: ds
adamc@201 475 end handle Empty => ds
adamc@201 476 end,
adamc@201 477 print = SourcePrint.p_file
adamc@201 478 }
adamc@56 479
adamc@270 480 val toParse = transform parse "parse" o toParseJob
adamc@38 481
adamc@378 482 fun libFile s = OS.Path.joinDirFile {dir = Config.libUr,
adamc@378 483 file = s}
adamc@378 484 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
adamc@378 485 file = s}
adamc@378 486
adamc@201 487 val elaborate = {
adamc@201 488 func = fn file => let
adamc@378 489 val basis = #func parseUrs (libFile "basis.urs")
adamc@378 490 val topSgn = #func parseUrs (libFile "top.urs")
adamc@378 491 val topStr = #func parseUr (libFile "top.ur")
adamc@201 492 in
adamc@325 493 Elaborate.elabFile basis topStr topSgn ElabEnv.empty file
adamc@201 494 end,
adamc@201 495 print = ElabPrint.p_file ElabEnv.empty
adamc@201 496 }
adamc@5 497
adamc@270 498 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 499
adamc@448 500 val unnest = {
adamc@448 501 func = Unnest.unnest,
adamc@448 502 print = ElabPrint.p_file ElabEnv.empty
adamc@448 503 }
adamc@448 504
adamc@448 505 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 506
adamc@313 507 val termination = {
adamc@313 508 func = (fn file => (Termination.check file; file)),
adamc@313 509 print = ElabPrint.p_file ElabEnv.empty
adamc@313 510 }
adamc@313 511
adamc@448 512 val toTermination = transform termination "termination" o toUnnest
adamc@313 513
adamc@201 514 val explify = {
adamc@201 515 func = Explify.explify,
adamc@201 516 print = ExplPrint.p_file ExplEnv.empty
adamc@201 517 }
adamc@201 518
adamc@625 519 val toExplify = transform explify "explify" o toUnnest
adamc@201 520
adamc@201 521 val corify = {
adamc@201 522 func = Corify.corify,
adamc@201 523 print = CorePrint.p_file CoreEnv.empty
adamc@201 524 }
adamc@201 525
adamc@270 526 val toCorify = transform corify "corify" o toExplify
adamc@201 527
adamc@482 528 (*val reduce_local = {
adamc@482 529 func = ReduceLocal.reduce,
adamc@482 530 print = CorePrint.p_file CoreEnv.empty
adamc@482 531 }
adamc@482 532
adamc@482 533 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 534
adamc@443 535 val especialize = {
adamc@443 536 func = ESpecialize.specialize,
adamc@443 537 print = CorePrint.p_file CoreEnv.empty
adamc@443 538 }
adamc@443 539
adamc@443 540 val toEspecialize = transform especialize "especialize" o toCorify
adamc@443 541
adamc@454 542 val core_untangle = {
adamc@454 543 func = CoreUntangle.untangle,
adamc@454 544 print = CorePrint.p_file CoreEnv.empty
adamc@454 545 }
adamc@454 546
adamc@454 547 val toCore_untangle = transform core_untangle "core_untangle" o toEspecialize
adamc@454 548
adamc@202 549 val shake = {
adamc@202 550 func = Shake.shake,
adamc@202 551 print = CorePrint.p_file CoreEnv.empty
adamc@202 552 }
adamc@39 553
adamc@454 554 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 555
adamc@607 556 val rpcify = {
adamc@607 557 func = Rpcify.frob,
adamc@607 558 print = CorePrint.p_file CoreEnv.empty
adamc@607 559 }
adamc@607 560
adamc@607 561 val toRpcify = transform rpcify "rpcify" o toShake1
adamc@607 562
adamc@642 563 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 564 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@642 565
adamc@202 566 val tag = {
adamc@202 567 func = Tag.tag,
adamc@202 568 print = CorePrint.p_file CoreEnv.empty
adamc@202 569 }
adamc@193 570
adamc@642 571 val toTag = transform tag "tag" o toCore_untangle2
adamc@20 572
adamc@202 573 val reduce = {
adamc@202 574 func = Reduce.reduce,
adamc@202 575 print = CorePrint.p_file CoreEnv.empty
adamc@202 576 }
adamc@25 577
adamc@692 578 val toReduce = transform reduce "reduce" o toTag
adamc@23 579
adamc@315 580 val unpoly = {
adamc@315 581 func = Unpoly.unpoly,
adamc@315 582 print = CorePrint.p_file CoreEnv.empty
adamc@315 583 }
adamc@315 584
adamc@315 585 val toUnpoly = transform unpoly "unpoly" o toReduce
adamc@315 586
adamc@202 587 val specialize = {
adamc@202 588 func = Specialize.specialize,
adamc@202 589 print = CorePrint.p_file CoreEnv.empty
adamc@202 590 }
adamc@132 591
adamc@315 592 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 593
adamc@642 594 val toShake3 = transform shake "shake3" o toSpecialize
adamc@133 595
adamc@692 596 val marshalcheck = {
adamc@692 597 func = (fn file => (MarshalCheck.check file; file)),
adamc@692 598 print = CorePrint.p_file CoreEnv.empty
adamc@692 599 }
adamc@692 600
adamc@692 601 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake3
adamc@692 602
adamc@732 603 val effectize = {
adamc@732 604 func = Effective.effectize,
adamc@732 605 print = CorePrint.p_file CoreEnv.empty
adamc@732 606 }
adamc@732 607
adamc@732 608 val toEffectize = transform effectize "effectize" o toMarshalcheck
adamc@732 609
adamc@202 610 val monoize = {
adamc@202 611 func = Monoize.monoize CoreEnv.empty,
adamc@202 612 print = MonoPrint.p_file MonoEnv.empty
adamc@202 613 }
adamc@134 614
adamc@732 615 val toMonoize = transform monoize "monoize" o toEffectize
adamc@96 616
adamc@202 617 val mono_opt = {
adamc@202 618 func = MonoOpt.optimize,
adamc@202 619 print = MonoPrint.p_file MonoEnv.empty
adamc@202 620 }
adamc@29 621
adamc@270 622 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 623
adamc@202 624 val untangle = {
adamc@202 625 func = Untangle.untangle,
adamc@202 626 print = MonoPrint.p_file MonoEnv.empty
adamc@202 627 }
adamc@1 628
adamc@270 629 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 630
adamc@202 631 val mono_reduce = {
adamc@202 632 func = MonoReduce.reduce,
adamc@202 633 print = MonoPrint.p_file MonoEnv.empty
adamc@202 634 }
adamc@16 635
adamc@270 636 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 637
adamc@202 638 val mono_shake = {
adamc@202 639 func = MonoShake.shake,
adamc@202 640 print = MonoPrint.p_file MonoEnv.empty
adamc@202 641 }
adamc@110 642
adamc@270 643 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 644
adamc@572 645 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 646
adamc@567 647 val jscomp = {
adamc@567 648 func = JsComp.process,
adamc@567 649 print = MonoPrint.p_file MonoEnv.empty
adamc@567 650 }
adamc@567 651
adamc@572 652 val toJscomp = transform jscomp "jscomp" o toMono_opt2
adamc@567 653
adamc@572 654 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 655
adamc@506 656 val fuse = {
adamc@506 657 func = Fuse.fuse,
adamc@506 658 print = MonoPrint.p_file MonoEnv.empty
adamc@506 659 }
adamc@506 660
adamc@572 661 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 662
adamc@506 663 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 664
adamc@601 665 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 666 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@506 667
adamc@377 668 val pathcheck = {
adamc@377 669 func = (fn file => (PathCheck.check file; file)),
adamc@377 670 print = MonoPrint.p_file MonoEnv.empty
adamc@377 671 }
adamc@377 672
adamc@506 673 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake2
adamc@377 674
adamc@202 675 val cjrize = {
adamc@202 676 func = Cjrize.cjrize,
adamc@202 677 print = CjrPrint.p_file CjrEnv.empty
adamc@202 678 }
adamc@23 679
adamc@377 680 val toCjrize = transform cjrize "cjrize" o toPathcheck
adamc@29 681
adamc@643 682 val scriptcheck = {
adamc@643 683 func = ScriptCheck.classify,
adamc@643 684 print = CjrPrint.p_file CjrEnv.empty
adamc@643 685 }
adamc@643 686
adamc@643 687 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 688
adamc@282 689 val prepare = {
adamc@282 690 func = Prepare.prepare,
adamc@282 691 print = CjrPrint.p_file CjrEnv.empty
adamc@282 692 }
adamc@282 693
adamc@643 694 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 695
adamc@274 696 val sqlify = {
adamc@274 697 func = Cjrize.cjrize,
adamc@274 698 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 699 }
adamc@274 700
adamc@274 701 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 702
adamc@764 703 fun compileC {cname, oname, ename, libs, profile, debug, link = link'} =
adamc@183 704 let
adamc@378 705 val urweb_o = clibFile "urweb.o"
adamc@378 706 val driver_o = clibFile "driver.o"
adamc@378 707
adamc@435 708 val compile = "gcc " ^ Config.gccArgs ^ " -Wstrict-prototypes -Werror -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@734 709 val link = "gcc -Werror -O3 -lm -lmhash -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname ^ " " ^ driver_o ^ " -o " ^ ename
adamc@502 710
adamc@502 711 val (compile, link) =
adamc@502 712 if profile then
adamc@502 713 (compile ^ " -pg", link ^ " -pg")
adamc@502 714 else
adamc@502 715 (compile, link)
adamc@742 716
adamc@742 717 val (compile, link) =
adamc@742 718 if debug then
adamc@742 719 (compile ^ " -g", link ^ " -g")
adamc@742 720 else
adamc@742 721 (compile, link)
adamc@764 722
adamc@764 723 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
adamc@183 724 in
adamc@183 725 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@183 726 print "C compilation failed\n"
adamc@183 727 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@186 728 print "C linking failed\n"
adamc@183 729 else
adamc@673 730 ()
adamc@183 731 end
adamc@183 732
adamc@202 733 fun compile job =
adamc@282 734 case run toPrepare job of
adamc@244 735 NONE => print "Ur compilation failed\n"
adamc@29 736 | SOME file =>
adamc@202 737 let
adamc@274 738 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 739
adamc@274 740 val (cname, oname, cleanup) =
adamc@274 741 if #debug job then
adamc@457 742 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 743 else
adamc@274 744 let
adamc@274 745 val dir = OS.FileSys.tmpName ()
adamc@403 746 val () = if OS.FileSys.access (dir, []) then
adamc@403 747 OS.FileSys.remove dir
adamc@403 748 else
adamc@403 749 ()
adamc@457 750 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 751 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 752 in
adamc@274 753 OS.FileSys.mkDir dir;
adamc@274 754 (cname, oname,
adamc@274 755 fn () => (OS.FileSys.remove cname;
adamc@274 756 OS.FileSys.remove oname;
adamc@473 757 OS.FileSys.rmDir dir)
adamc@473 758 handle OS.SysErr _ => OS.FileSys.rmDir dir)
adamc@274 759 end
adamc@274 760 val ename = #exe job
adamc@202 761 in
adamc@274 762 let
adamc@274 763 val outf = TextIO.openOut cname
adamc@274 764 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 765
adamc@432 766 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 767 val libs =
adamc@432 768 if hasDb then
adamc@432 769 "-lpq"
adamc@432 770 else
adamc@432 771 ""
adamc@274 772 in
adamc@274 773 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 774 TextIO.output1 (outf, #"\n");
adamc@274 775 TextIO.closeOut outf;
adamc@102 776
adamc@274 777 case #sql job of
adamc@274 778 NONE => ()
adamc@274 779 | SOME sql =>
adamc@274 780 let
adamc@274 781 val outf = TextIO.openOut sql
adamc@274 782 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@274 783 in
adamc@274 784 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adamc@274 785 TextIO.closeOut outf
adamc@274 786 end;
adamc@274 787
adamc@742 788 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
adamc@764 789 profile = #profile job, debug = #debug job, link = #link job};
adamc@274 790
adamc@274 791 cleanup ()
adamc@274 792 end
adamc@274 793 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 794 end
adamc@29 795
adamc@1 796 end