annotate src/compiler.sml @ 642:4a125bbc602d

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