annotate src/compiler.sml @ 673:a8effb6159c2

Variable timeouts and client keep-alive
author Adam Chlipala <adamc@hcoop.net>
date Tue, 24 Mar 2009 15:35:46 -0400
parents aa2290c32ce2
children 54ec237a3028
rev   line source
adamc@1 1 (* Copyright (c) 2008, 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@244 28 (* Ur/Web language parser *)
adamc@1 29
adamc@1 30 structure Compiler :> COMPILER = struct
adamc@1 31
adamc@244 32 structure UrwebLrVals = UrwebLrValsFn(structure Token = LrParser.Token)
adamc@244 33 structure Lex = UrwebLexFn(structure Tokens = UrwebLrVals.Tokens)
adamc@244 34 structure UrwebP = Join(structure ParserData = UrwebLrVals.ParserData
adamc@1 35 structure Lex = Lex
adamc@1 36 structure LrParser = LrParser)
adamc@1 37
adamc@270 38 type job = {
adamc@385 39 prefix : string,
adamc@270 40 database : string option,
adamc@274 41 sources : string list,
adamc@274 42 exe : string,
adamc@274 43 sql : string option,
adamc@502 44 debug : bool,
adamc@673 45 profile : bool,
adamc@673 46 timeout : int
adamc@270 47 }
adamc@201 48
adamc@201 49 type ('src, 'dst) phase = {
adamc@201 50 func : 'src -> 'dst,
adamc@201 51 print : 'dst -> Print.PD.pp_desc
adamc@201 52 }
adamc@201 53
adamc@201 54 type pmap = (string * Time.time) list
adamc@201 55
adamc@201 56 type ('src, 'dst) transform = {
adamc@201 57 func : 'src -> 'dst option,
adamc@201 58 print : 'dst -> Print.PD.pp_desc,
adamc@201 59 time : 'src * pmap -> 'dst option * pmap
adamc@201 60 }
adamc@201 61
adamc@201 62 fun transform (ph : ('src, 'dst) phase) name = {
adamc@201 63 func = fn input => let
adamc@201 64 val v = #func ph input
adamc@201 65 in
adamc@201 66 if ErrorMsg.anyErrors () then
adamc@201 67 NONE
adamc@201 68 else
adamc@201 69 SOME v
adamc@201 70 end,
adamc@201 71 print = #print ph,
adamc@201 72 time = fn (input, pmap) => let
adamc@201 73 val befor = Time.now ()
adamc@201 74 val v = #func ph input
adamc@201 75 val elapsed = Time.- (Time.now (), befor)
adamc@201 76 in
adamc@201 77 (if ErrorMsg.anyErrors () then
adamc@201 78 NONE
adamc@201 79 else
adamc@201 80 SOME v,
adamc@201 81 (name, elapsed) :: pmap)
adamc@201 82 end
adamc@201 83 }
adamc@201 84
adamc@270 85 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@201 86 func = fn input => case #func tr1 input of
adamc@201 87 NONE => NONE
adamc@201 88 | SOME v => #func tr2 v,
adamc@201 89 print = #print tr2,
adamc@201 90 time = fn (input, pmap) => let
adamc@201 91 val (ro, pmap) = #time tr1 (input, pmap)
adamc@201 92 in
adamc@201 93 case ro of
adamc@201 94 NONE => (NONE, pmap)
adamc@201 95 | SOME v => #time tr2 (v, pmap)
adamc@201 96 end
adamc@201 97 }
adamc@201 98
adamc@346 99 fun check (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@346 100 ignore (#func tr x))
adamc@346 101
adamc@280 102 fun run (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@280 103 #func tr x)
adamc@201 104
adamc@201 105 fun runPrint (tr : ('src, 'dst) transform) input =
adamc@280 106 (ErrorMsg.resetErrors ();
adamc@280 107 case #func tr input of
adamc@280 108 NONE => print "Failure\n"
adamc@280 109 | SOME v =>
adamc@280 110 (print "Success\n";
adamc@280 111 Print.print (#print tr v);
adamc@280 112 print "\n"))
adamc@201 113
adamc@201 114 fun time (tr : ('src, 'dst) transform) input =
adamc@55 115 let
adamc@201 116 val (_, pmap) = #time tr (input, [])
adamc@201 117 in
adamc@201 118 app (fn (name, time) =>
adamc@201 119 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 120 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 121 print "\n"
adamc@201 122 end
adamc@55 123
adamc@201 124 fun timePrint (tr : ('src, 'dst) transform) input =
adamc@201 125 let
adamc@201 126 val (ro, pmap) = #time tr (input, [])
adamc@55 127 in
adamc@201 128 app (fn (name, time) =>
adamc@201 129 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 130 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 131 print "\n";
adamc@201 132 case ro of
adamc@201 133 NONE => print "Failure\n"
adamc@201 134 | SOME v =>
adamc@201 135 (print "Success\n";
adamc@201 136 Print.print (#print tr v);
adamc@201 137 print "\n")
adamc@55 138 end
adamc@55 139
adamc@244 140 val parseUrs =
adamc@201 141 {func = fn filename => let
adamc@201 142 val fname = OS.FileSys.tmpName ()
adamc@201 143 val outf = TextIO.openOut fname
adamc@201 144 val () = TextIO.output (outf, "sig\n")
adamc@201 145 val inf = TextIO.openIn filename
adamc@201 146 fun loop () =
adamc@201 147 case TextIO.inputLine inf of
adamc@201 148 NONE => ()
adamc@201 149 | SOME line => (TextIO.output (outf, line);
adamc@201 150 loop ())
adamc@201 151 val () = loop ()
adamc@201 152 val () = TextIO.closeIn inf
adamc@201 153 val () = TextIO.closeOut outf
adamc@201 154
adamc@201 155 val () = (ErrorMsg.resetErrors ();
adamc@201 156 ErrorMsg.resetPositioning filename;
adamc@201 157 Lex.UserDeclarations.initialize ())
adamc@201 158 val file = TextIO.openIn fname
adamc@201 159 fun get _ = TextIO.input file
adamc@201 160 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 161 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 162 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 163 in
adamc@201 164 TextIO.closeIn file;
adamc@201 165 case absyn of
adamc@201 166 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
adamc@201 167 | _ => (ErrorMsg.errorAt {file = filename,
adamc@201 168 first = {line = 0,
adamc@201 169 char = 0},
adamc@201 170 last = {line = 0,
adamc@201 171 char = 0}} "Not a signature";
adamc@201 172 [])
adamc@201 173 end
adamc@201 174 handle LrParser.ParseError => [],
adamc@201 175 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
adamc@55 176
adamc@1 177 (* The main parsing routine *)
adamc@244 178 val parseUr = {
adamc@201 179 func = fn filename =>
adamc@201 180 let
adamc@201 181 val () = (ErrorMsg.resetErrors ();
adamc@201 182 ErrorMsg.resetPositioning filename;
adamc@201 183 Lex.UserDeclarations.initialize ())
adamc@201 184 val file = TextIO.openIn filename
adamc@201 185 fun get _ = TextIO.input file
adamc@201 186 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 187 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 188 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 189 in
adamc@201 190 TextIO.closeIn file;
adamc@201 191 case absyn of
adamc@201 192 [(Source.DSgn ("?", _), _)] =>
adamc@201 193 (ErrorMsg.errorAt {file = filename,
adamc@201 194 first = {line = 0,
adamc@201 195 char = 0},
adamc@201 196 last = {line = 0,
adamc@201 197 char = 0}} "File starts with 'sig'";
adamc@201 198 [])
adamc@201 199 | _ => absyn
adamc@201 200 end
adamc@201 201 handle LrParser.ParseError => [],
adamc@201 202 print = SourcePrint.p_file}
adamc@56 203
adamc@673 204 fun p_job {prefix, database, exe, sql, sources, debug, profile, timeout} =
adamc@270 205 let
adamc@270 206 open Print.PD
adamc@270 207 open Print
adamc@270 208 in
adamc@274 209 box [if debug then
adamc@274 210 box [string "DEBUG", newline]
adamc@274 211 else
adamc@274 212 box [],
adamc@502 213 if profile then
adamc@502 214 box [string "PROFILE", newline]
adamc@502 215 else
adamc@502 216 box [],
adamc@274 217 case database of
adamc@270 218 NONE => string "No database."
adamc@270 219 | SOME db => string ("Database: " ^ db),
adamc@270 220 newline,
adamc@274 221 string "Exe: ",
adamc@274 222 string exe,
adamc@274 223 newline,
adamc@274 224 case sql of
adamc@274 225 NONE => string "No SQL file."
adamc@274 226 | SOME sql => string ("SQL fle: " ^ sql),
adamc@673 227 newline,
adamc@673 228 string "Timeout: ",
adamc@673 229 string (Int.toString timeout),
adamc@673 230 newline,
adamc@270 231 string "Sources:",
adamc@270 232 p_list string sources,
adamc@270 233 newline]
adamc@270 234 end
adamc@270 235
adamc@270 236 fun trim s =
adamc@270 237 let
adamc@270 238 val (_, s) = Substring.splitl Char.isSpace s
adamc@270 239 val (s, _) = Substring.splitr Char.isSpace s
adamc@270 240 in
adamc@270 241 s
adamc@270 242 end
adamc@270 243
adamc@270 244 val parseUrp = {
adamc@270 245 func = fn filename =>
adamc@270 246 let
adamc@270 247 val dir = OS.Path.dir filename
adamc@270 248 val inf = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@270 249
adamc@274 250 fun relify fname =
adamc@274 251 OS.Path.concat (dir, fname)
adamc@274 252 handle OS.Path.Path => fname
adamc@274 253
adamc@270 254 fun readSources acc =
adamc@270 255 case TextIO.inputLine inf of
adamc@270 256 NONE => rev acc
adamc@270 257 | SOME line =>
adamc@270 258 let
adamc@270 259 val acc = if CharVector.all Char.isSpace line then
adamc@270 260 acc
adamc@270 261 else
adamc@270 262 let
adamc@270 263 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adamc@270 264 (String.explode line))
adamc@274 265 val fname = relify fname
adamc@270 266 in
adamc@270 267 fname :: acc
adamc@270 268 end
adamc@270 269 in
adamc@270 270 readSources acc
adamc@270 271 end
adamc@270 272
adamc@673 273 fun finish (prefix, database, exe, sql, debug, profile, timeout, sources) =
adamc@385 274 {prefix = Option.getOpt (prefix, "/"),
adamc@385 275 database = database,
adamc@274 276 exe = Option.getOpt (exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adamc@274 277 ext = SOME "exe"}),
adamc@274 278 sql = sql,
adamc@274 279 debug = debug,
adamc@502 280 profile = profile,
adamc@673 281 timeout = Option.getOpt (timeout, 60),
adamc@274 282 sources = sources}
adamc@274 283
adamc@673 284 fun read (prefix, database, exe, sql, debug, profile, timeout) =
adamc@270 285 case TextIO.inputLine inf of
adamc@673 286 NONE => finish (prefix, database, exe, sql, debug, profile, timeout, [])
adamc@673 287 | SOME "\n" => finish (prefix, database, exe, sql, debug, profile, timeout, readSources [])
adamc@270 288 | SOME line =>
adamc@270 289 let
adamc@270 290 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adamc@270 291 val cmd = Substring.string (trim cmd)
adamc@270 292 val arg = Substring.string (trim arg)
adamc@270 293 in
adamc@270 294 case cmd of
adamc@385 295 "prefix" =>
adamc@385 296 (case prefix of
adamc@385 297 NONE => ()
adamc@385 298 | SOME _ => ErrorMsg.error "Duplicate 'prefix' directive";
adamc@673 299 read (SOME arg, database, exe, sql, debug, profile, timeout))
adamc@385 300 | "database" =>
adamc@270 301 (case database of
adamc@270 302 NONE => ()
adamc@270 303 | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
adamc@673 304 read (prefix, SOME arg, exe, sql, debug, profile, timeout))
adamc@274 305 | "exe" =>
adamc@274 306 (case exe of
adamc@274 307 NONE => ()
adamc@274 308 | SOME _ => ErrorMsg.error "Duplicate 'exe' directive";
adamc@673 309 read (prefix, database, SOME (relify arg), sql, debug, profile, timeout))
adamc@274 310 | "sql" =>
adamc@274 311 (case sql of
adamc@274 312 NONE => ()
adamc@274 313 | SOME _ => ErrorMsg.error "Duplicate 'sql' directive";
adamc@673 314 read (prefix, database, exe, SOME (relify arg), debug, profile, timeout))
adamc@673 315 | "debug" => read (prefix, database, exe, sql, true, profile, timeout)
adamc@673 316 | "profile" => read (prefix, database, exe, sql, debug, true, timeout)
adamc@673 317 | "timeout" =>
adamc@673 318 (case timeout of
adamc@673 319 NONE => ()
adamc@673 320 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adamc@673 321 read (prefix, database, exe, sql, debug, profile, SOME (valOf (Int.fromString arg))))
adamc@270 322 | _ => (ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adamc@673 323 read (prefix, database, exe, sql, debug, profile, timeout))
adamc@270 324 end
adamc@385 325
adamc@673 326 val job = read (NONE, NONE, NONE, NONE, false, false, NONE)
adamc@270 327 in
adamc@385 328 TextIO.closeIn inf;
adamc@385 329 Monoize.urlPrefix := #prefix job;
adamc@673 330 CjrPrint.timeout := #timeout job;
adamc@385 331 job
adamc@270 332 end,
adamc@270 333 print = p_job
adamc@270 334 }
adamc@270 335
adamc@270 336 val toParseJob = transform parseUrp "parseJob"
adamc@270 337
adamc@56 338 fun capitalize "" = ""
adamc@56 339 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 340
adamc@201 341 val parse = {
adamc@274 342 func = fn {database, sources = fnames, ...} : job =>
adamc@201 343 let
adamc@201 344 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 345
adamc@201 346 fun parseOne fname =
adamc@201 347 let
adamc@201 348 val mname = nameOf fname
adamc@244 349 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 350 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 351
adamc@201 352 val sgnO =
adamc@244 353 if Posix.FileSys.access (urs, []) then
adamc@244 354 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 355 {file = urs,
adamc@201 356 first = ErrorMsg.dummyPos,
adamc@201 357 last = ErrorMsg.dummyPos})
adamc@201 358 else
adamc@201 359 NONE
adamc@56 360
adamc@244 361 val loc = {file = ur,
adamc@201 362 first = ErrorMsg.dummyPos,
adamc@201 363 last = ErrorMsg.dummyPos}
adamc@56 364
adamc@244 365 val ds = #func parseUr ur
adamc@201 366 in
adamc@201 367 (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@201 368 end
adamc@56 369
adamc@201 370 val ds = map parseOne fnames
adamc@201 371 in
adamc@201 372 let
adamc@201 373 val final = nameOf (List.last fnames)
adamc@271 374
adamc@271 375 val ds = ds @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
adamc@201 376 in
adamc@271 377 case database of
adamc@271 378 NONE => ds
adamc@271 379 | SOME s => (Source.DDatabase s, ErrorMsg.dummySpan) :: ds
adamc@201 380 end handle Empty => ds
adamc@201 381 end,
adamc@201 382 print = SourcePrint.p_file
adamc@201 383 }
adamc@56 384
adamc@270 385 val toParse = transform parse "parse" o toParseJob
adamc@38 386
adamc@378 387 fun libFile s = OS.Path.joinDirFile {dir = Config.libUr,
adamc@378 388 file = s}
adamc@378 389 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
adamc@378 390 file = s}
adamc@378 391
adamc@201 392 val elaborate = {
adamc@201 393 func = fn file => let
adamc@378 394 val basis = #func parseUrs (libFile "basis.urs")
adamc@378 395 val topSgn = #func parseUrs (libFile "top.urs")
adamc@378 396 val topStr = #func parseUr (libFile "top.ur")
adamc@201 397 in
adamc@325 398 Elaborate.elabFile basis topStr topSgn ElabEnv.empty file
adamc@201 399 end,
adamc@201 400 print = ElabPrint.p_file ElabEnv.empty
adamc@201 401 }
adamc@5 402
adamc@270 403 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 404
adamc@448 405 val unnest = {
adamc@448 406 func = Unnest.unnest,
adamc@448 407 print = ElabPrint.p_file ElabEnv.empty
adamc@448 408 }
adamc@448 409
adamc@448 410 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 411
adamc@313 412 val termination = {
adamc@313 413 func = (fn file => (Termination.check file; file)),
adamc@313 414 print = ElabPrint.p_file ElabEnv.empty
adamc@313 415 }
adamc@313 416
adamc@448 417 val toTermination = transform termination "termination" o toUnnest
adamc@313 418
adamc@201 419 val explify = {
adamc@201 420 func = Explify.explify,
adamc@201 421 print = ExplPrint.p_file ExplEnv.empty
adamc@201 422 }
adamc@201 423
adamc@625 424 val toExplify = transform explify "explify" o toUnnest
adamc@201 425
adamc@201 426 val corify = {
adamc@201 427 func = Corify.corify,
adamc@201 428 print = CorePrint.p_file CoreEnv.empty
adamc@201 429 }
adamc@201 430
adamc@270 431 val toCorify = transform corify "corify" o toExplify
adamc@201 432
adamc@482 433 (*val reduce_local = {
adamc@482 434 func = ReduceLocal.reduce,
adamc@482 435 print = CorePrint.p_file CoreEnv.empty
adamc@482 436 }
adamc@482 437
adamc@482 438 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 439
adamc@443 440 val especialize = {
adamc@443 441 func = ESpecialize.specialize,
adamc@443 442 print = CorePrint.p_file CoreEnv.empty
adamc@443 443 }
adamc@443 444
adamc@443 445 val toEspecialize = transform especialize "especialize" o toCorify
adamc@443 446
adamc@454 447 val core_untangle = {
adamc@454 448 func = CoreUntangle.untangle,
adamc@454 449 print = CorePrint.p_file CoreEnv.empty
adamc@454 450 }
adamc@454 451
adamc@454 452 val toCore_untangle = transform core_untangle "core_untangle" o toEspecialize
adamc@454 453
adamc@202 454 val shake = {
adamc@202 455 func = Shake.shake,
adamc@202 456 print = CorePrint.p_file CoreEnv.empty
adamc@202 457 }
adamc@39 458
adamc@454 459 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 460
adamc@607 461 val rpcify = {
adamc@607 462 func = Rpcify.frob,
adamc@607 463 print = CorePrint.p_file CoreEnv.empty
adamc@607 464 }
adamc@607 465
adamc@607 466 val toRpcify = transform rpcify "rpcify" o toShake1
adamc@607 467
adamc@642 468 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 469 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@642 470
adamc@202 471 val tag = {
adamc@202 472 func = Tag.tag,
adamc@202 473 print = CorePrint.p_file CoreEnv.empty
adamc@202 474 }
adamc@193 475
adamc@642 476 val toTag = transform tag "tag" o toCore_untangle2
adamc@20 477
adamc@202 478 val reduce = {
adamc@202 479 func = Reduce.reduce,
adamc@202 480 print = CorePrint.p_file CoreEnv.empty
adamc@202 481 }
adamc@25 482
adamc@270 483 val toReduce = transform reduce "reduce" o toTag
adamc@23 484
adamc@315 485 val unpoly = {
adamc@315 486 func = Unpoly.unpoly,
adamc@315 487 print = CorePrint.p_file CoreEnv.empty
adamc@315 488 }
adamc@315 489
adamc@315 490 val toUnpoly = transform unpoly "unpoly" o toReduce
adamc@315 491
adamc@202 492 val specialize = {
adamc@202 493 func = Specialize.specialize,
adamc@202 494 print = CorePrint.p_file CoreEnv.empty
adamc@202 495 }
adamc@132 496
adamc@315 497 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 498
adamc@642 499 val toShake3 = transform shake "shake3" o toSpecialize
adamc@133 500
adamc@202 501 val monoize = {
adamc@202 502 func = Monoize.monoize CoreEnv.empty,
adamc@202 503 print = MonoPrint.p_file MonoEnv.empty
adamc@202 504 }
adamc@134 505
adamc@642 506 val toMonoize = transform monoize "monoize" o toShake3
adamc@96 507
adamc@202 508 val mono_opt = {
adamc@202 509 func = MonoOpt.optimize,
adamc@202 510 print = MonoPrint.p_file MonoEnv.empty
adamc@202 511 }
adamc@29 512
adamc@270 513 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 514
adamc@202 515 val untangle = {
adamc@202 516 func = Untangle.untangle,
adamc@202 517 print = MonoPrint.p_file MonoEnv.empty
adamc@202 518 }
adamc@1 519
adamc@270 520 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 521
adamc@202 522 val mono_reduce = {
adamc@202 523 func = MonoReduce.reduce,
adamc@202 524 print = MonoPrint.p_file MonoEnv.empty
adamc@202 525 }
adamc@16 526
adamc@270 527 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 528
adamc@202 529 val mono_shake = {
adamc@202 530 func = MonoShake.shake,
adamc@202 531 print = MonoPrint.p_file MonoEnv.empty
adamc@202 532 }
adamc@110 533
adamc@270 534 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 535
adamc@572 536 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 537
adamc@567 538 val jscomp = {
adamc@567 539 func = JsComp.process,
adamc@567 540 print = MonoPrint.p_file MonoEnv.empty
adamc@567 541 }
adamc@567 542
adamc@572 543 val toJscomp = transform jscomp "jscomp" o toMono_opt2
adamc@567 544
adamc@572 545 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 546
adamc@506 547 val fuse = {
adamc@506 548 func = Fuse.fuse,
adamc@506 549 print = MonoPrint.p_file MonoEnv.empty
adamc@506 550 }
adamc@506 551
adamc@572 552 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 553
adamc@506 554 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 555
adamc@601 556 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 557 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@506 558
adamc@377 559 val pathcheck = {
adamc@377 560 func = (fn file => (PathCheck.check file; file)),
adamc@377 561 print = MonoPrint.p_file MonoEnv.empty
adamc@377 562 }
adamc@377 563
adamc@506 564 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake2
adamc@377 565
adamc@202 566 val cjrize = {
adamc@202 567 func = Cjrize.cjrize,
adamc@202 568 print = CjrPrint.p_file CjrEnv.empty
adamc@202 569 }
adamc@23 570
adamc@377 571 val toCjrize = transform cjrize "cjrize" o toPathcheck
adamc@29 572
adamc@643 573 val scriptcheck = {
adamc@643 574 func = ScriptCheck.classify,
adamc@643 575 print = CjrPrint.p_file CjrEnv.empty
adamc@643 576 }
adamc@643 577
adamc@643 578 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 579
adamc@282 580 val prepare = {
adamc@282 581 func = Prepare.prepare,
adamc@282 582 print = CjrPrint.p_file CjrEnv.empty
adamc@282 583 }
adamc@282 584
adamc@643 585 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 586
adamc@274 587 val sqlify = {
adamc@274 588 func = Cjrize.cjrize,
adamc@274 589 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 590 }
adamc@274 591
adamc@274 592 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 593
adamc@502 594 fun compileC {cname, oname, ename, libs, profile} =
adamc@183 595 let
adamc@378 596 val urweb_o = clibFile "urweb.o"
adamc@378 597 val driver_o = clibFile "driver.o"
adamc@378 598
adamc@435 599 val compile = "gcc " ^ Config.gccArgs ^ " -Wstrict-prototypes -Werror -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@432 600 val link = "gcc -Werror -O3 -lm -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname ^ " " ^ driver_o ^ " -o " ^ ename
adamc@502 601
adamc@502 602 val (compile, link) =
adamc@502 603 if profile then
adamc@502 604 (compile ^ " -pg", link ^ " -pg")
adamc@502 605 else
adamc@502 606 (compile, link)
adamc@183 607 in
adamc@183 608 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@183 609 print "C compilation failed\n"
adamc@183 610 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@186 611 print "C linking failed\n"
adamc@183 612 else
adamc@673 613 ()
adamc@183 614 end
adamc@183 615
adamc@202 616 fun compile job =
adamc@282 617 case run toPrepare job of
adamc@244 618 NONE => print "Ur compilation failed\n"
adamc@29 619 | SOME file =>
adamc@202 620 let
adamc@274 621 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 622
adamc@274 623 val (cname, oname, cleanup) =
adamc@274 624 if #debug job then
adamc@457 625 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 626 else
adamc@274 627 let
adamc@274 628 val dir = OS.FileSys.tmpName ()
adamc@403 629 val () = if OS.FileSys.access (dir, []) then
adamc@403 630 OS.FileSys.remove dir
adamc@403 631 else
adamc@403 632 ()
adamc@457 633 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 634 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 635 in
adamc@274 636 OS.FileSys.mkDir dir;
adamc@274 637 (cname, oname,
adamc@274 638 fn () => (OS.FileSys.remove cname;
adamc@274 639 OS.FileSys.remove oname;
adamc@473 640 OS.FileSys.rmDir dir)
adamc@473 641 handle OS.SysErr _ => OS.FileSys.rmDir dir)
adamc@274 642 end
adamc@274 643 val ename = #exe job
adamc@202 644 in
adamc@274 645 let
adamc@274 646 val outf = TextIO.openOut cname
adamc@274 647 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 648
adamc@432 649 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 650 val libs =
adamc@432 651 if hasDb then
adamc@432 652 "-lpq"
adamc@432 653 else
adamc@432 654 ""
adamc@274 655 in
adamc@274 656 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 657 TextIO.output1 (outf, #"\n");
adamc@274 658 TextIO.closeOut outf;
adamc@102 659
adamc@274 660 case #sql job of
adamc@274 661 NONE => ()
adamc@274 662 | SOME sql =>
adamc@274 663 let
adamc@274 664 val outf = TextIO.openOut sql
adamc@274 665 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@274 666 in
adamc@274 667 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adamc@274 668 TextIO.closeOut outf
adamc@274 669 end;
adamc@274 670
adamc@502 671 compileC {cname = cname, oname = oname, ename = ename, libs = libs, profile = #profile job};
adamc@274 672
adamc@274 673 cleanup ()
adamc@274 674 end
adamc@274 675 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 676 end
adamc@29 677
adamc@1 678 end