annotate src/compiler.sml @ 681:6c9b8875f347

Switch back to starting listener on load
author Adam Chlipala <adamc@hcoop.net>
date Sat, 28 Mar 2009 11:15:42 -0400
parents 54ec237a3028
children 09df0c85f306
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@680 478 val marshalcheck = {
adamc@680 479 func = (fn file => (MarshalCheck.check file; file)),
adamc@680 480 print = CorePrint.p_file CoreEnv.empty
adamc@680 481 }
adamc@680 482
adamc@680 483 val toMarshalcheck = transform marshalcheck "marshalcheck" o toTag
adamc@680 484
adamc@202 485 val reduce = {
adamc@202 486 func = Reduce.reduce,
adamc@202 487 print = CorePrint.p_file CoreEnv.empty
adamc@202 488 }
adamc@25 489
adamc@680 490 val toReduce = transform reduce "reduce" o toMarshalcheck
adamc@23 491
adamc@315 492 val unpoly = {
adamc@315 493 func = Unpoly.unpoly,
adamc@315 494 print = CorePrint.p_file CoreEnv.empty
adamc@315 495 }
adamc@315 496
adamc@315 497 val toUnpoly = transform unpoly "unpoly" o toReduce
adamc@315 498
adamc@202 499 val specialize = {
adamc@202 500 func = Specialize.specialize,
adamc@202 501 print = CorePrint.p_file CoreEnv.empty
adamc@202 502 }
adamc@132 503
adamc@315 504 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 505
adamc@642 506 val toShake3 = transform shake "shake3" o toSpecialize
adamc@133 507
adamc@202 508 val monoize = {
adamc@202 509 func = Monoize.monoize CoreEnv.empty,
adamc@202 510 print = MonoPrint.p_file MonoEnv.empty
adamc@202 511 }
adamc@134 512
adamc@642 513 val toMonoize = transform monoize "monoize" o toShake3
adamc@96 514
adamc@202 515 val mono_opt = {
adamc@202 516 func = MonoOpt.optimize,
adamc@202 517 print = MonoPrint.p_file MonoEnv.empty
adamc@202 518 }
adamc@29 519
adamc@270 520 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 521
adamc@202 522 val untangle = {
adamc@202 523 func = Untangle.untangle,
adamc@202 524 print = MonoPrint.p_file MonoEnv.empty
adamc@202 525 }
adamc@1 526
adamc@270 527 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 528
adamc@202 529 val mono_reduce = {
adamc@202 530 func = MonoReduce.reduce,
adamc@202 531 print = MonoPrint.p_file MonoEnv.empty
adamc@202 532 }
adamc@16 533
adamc@270 534 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 535
adamc@202 536 val mono_shake = {
adamc@202 537 func = MonoShake.shake,
adamc@202 538 print = MonoPrint.p_file MonoEnv.empty
adamc@202 539 }
adamc@110 540
adamc@270 541 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 542
adamc@572 543 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 544
adamc@567 545 val jscomp = {
adamc@567 546 func = JsComp.process,
adamc@567 547 print = MonoPrint.p_file MonoEnv.empty
adamc@567 548 }
adamc@567 549
adamc@572 550 val toJscomp = transform jscomp "jscomp" o toMono_opt2
adamc@567 551
adamc@572 552 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 553
adamc@506 554 val fuse = {
adamc@506 555 func = Fuse.fuse,
adamc@506 556 print = MonoPrint.p_file MonoEnv.empty
adamc@506 557 }
adamc@506 558
adamc@572 559 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 560
adamc@506 561 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 562
adamc@601 563 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 564 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@506 565
adamc@377 566 val pathcheck = {
adamc@377 567 func = (fn file => (PathCheck.check file; file)),
adamc@377 568 print = MonoPrint.p_file MonoEnv.empty
adamc@377 569 }
adamc@377 570
adamc@506 571 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake2
adamc@377 572
adamc@202 573 val cjrize = {
adamc@202 574 func = Cjrize.cjrize,
adamc@202 575 print = CjrPrint.p_file CjrEnv.empty
adamc@202 576 }
adamc@23 577
adamc@377 578 val toCjrize = transform cjrize "cjrize" o toPathcheck
adamc@29 579
adamc@643 580 val scriptcheck = {
adamc@643 581 func = ScriptCheck.classify,
adamc@643 582 print = CjrPrint.p_file CjrEnv.empty
adamc@643 583 }
adamc@643 584
adamc@643 585 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 586
adamc@282 587 val prepare = {
adamc@282 588 func = Prepare.prepare,
adamc@282 589 print = CjrPrint.p_file CjrEnv.empty
adamc@282 590 }
adamc@282 591
adamc@643 592 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 593
adamc@274 594 val sqlify = {
adamc@274 595 func = Cjrize.cjrize,
adamc@274 596 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 597 }
adamc@274 598
adamc@274 599 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 600
adamc@502 601 fun compileC {cname, oname, ename, libs, profile} =
adamc@183 602 let
adamc@378 603 val urweb_o = clibFile "urweb.o"
adamc@378 604 val driver_o = clibFile "driver.o"
adamc@378 605
adamc@435 606 val compile = "gcc " ^ Config.gccArgs ^ " -Wstrict-prototypes -Werror -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@432 607 val link = "gcc -Werror -O3 -lm -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname ^ " " ^ driver_o ^ " -o " ^ ename
adamc@502 608
adamc@502 609 val (compile, link) =
adamc@502 610 if profile then
adamc@502 611 (compile ^ " -pg", link ^ " -pg")
adamc@502 612 else
adamc@502 613 (compile, link)
adamc@183 614 in
adamc@183 615 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@183 616 print "C compilation failed\n"
adamc@183 617 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@186 618 print "C linking failed\n"
adamc@183 619 else
adamc@673 620 ()
adamc@183 621 end
adamc@183 622
adamc@202 623 fun compile job =
adamc@282 624 case run toPrepare job of
adamc@244 625 NONE => print "Ur compilation failed\n"
adamc@29 626 | SOME file =>
adamc@202 627 let
adamc@274 628 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 629
adamc@274 630 val (cname, oname, cleanup) =
adamc@274 631 if #debug job then
adamc@457 632 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 633 else
adamc@274 634 let
adamc@274 635 val dir = OS.FileSys.tmpName ()
adamc@403 636 val () = if OS.FileSys.access (dir, []) then
adamc@403 637 OS.FileSys.remove dir
adamc@403 638 else
adamc@403 639 ()
adamc@457 640 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 641 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 642 in
adamc@274 643 OS.FileSys.mkDir dir;
adamc@274 644 (cname, oname,
adamc@274 645 fn () => (OS.FileSys.remove cname;
adamc@274 646 OS.FileSys.remove oname;
adamc@473 647 OS.FileSys.rmDir dir)
adamc@473 648 handle OS.SysErr _ => OS.FileSys.rmDir dir)
adamc@274 649 end
adamc@274 650 val ename = #exe job
adamc@202 651 in
adamc@274 652 let
adamc@274 653 val outf = TextIO.openOut cname
adamc@274 654 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 655
adamc@432 656 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 657 val libs =
adamc@432 658 if hasDb then
adamc@432 659 "-lpq"
adamc@432 660 else
adamc@432 661 ""
adamc@274 662 in
adamc@274 663 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 664 TextIO.output1 (outf, #"\n");
adamc@274 665 TextIO.closeOut outf;
adamc@102 666
adamc@274 667 case #sql job of
adamc@274 668 NONE => ()
adamc@274 669 | SOME sql =>
adamc@274 670 let
adamc@274 671 val outf = TextIO.openOut sql
adamc@274 672 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@274 673 in
adamc@274 674 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adamc@274 675 TextIO.closeOut outf
adamc@274 676 end;
adamc@274 677
adamc@502 678 compileC {cname = cname, oname = oname, ename = ename, libs = libs, profile = #profile job};
adamc@274 679
adamc@274 680 cleanup ()
adamc@274 681 end
adamc@274 682 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 683 end
adamc@29 684
adamc@1 685 end