annotate src/compiler.sml @ 270:b9b02613c0c2

Parsing jobs
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 10:31:16 -0400
parents bacd0ba869e1
children 42dfb0d61cf0
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@270 39 database : string option,
adamc@270 40 sources : string list
adamc@270 41 }
adamc@201 42
adamc@201 43 type ('src, 'dst) phase = {
adamc@201 44 func : 'src -> 'dst,
adamc@201 45 print : 'dst -> Print.PD.pp_desc
adamc@201 46 }
adamc@201 47
adamc@201 48 type pmap = (string * Time.time) list
adamc@201 49
adamc@201 50 type ('src, 'dst) transform = {
adamc@201 51 func : 'src -> 'dst option,
adamc@201 52 print : 'dst -> Print.PD.pp_desc,
adamc@201 53 time : 'src * pmap -> 'dst option * pmap
adamc@201 54 }
adamc@201 55
adamc@201 56 fun transform (ph : ('src, 'dst) phase) name = {
adamc@201 57 func = fn input => let
adamc@201 58 val v = #func ph input
adamc@201 59 in
adamc@201 60 if ErrorMsg.anyErrors () then
adamc@201 61 NONE
adamc@201 62 else
adamc@201 63 SOME v
adamc@201 64 end,
adamc@201 65 print = #print ph,
adamc@201 66 time = fn (input, pmap) => let
adamc@201 67 val befor = Time.now ()
adamc@201 68 val v = #func ph input
adamc@201 69 val elapsed = Time.- (Time.now (), befor)
adamc@201 70 in
adamc@201 71 (if ErrorMsg.anyErrors () then
adamc@201 72 NONE
adamc@201 73 else
adamc@201 74 SOME v,
adamc@201 75 (name, elapsed) :: pmap)
adamc@201 76 end
adamc@201 77 }
adamc@201 78
adamc@270 79 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@201 80 func = fn input => case #func tr1 input of
adamc@201 81 NONE => NONE
adamc@201 82 | SOME v => #func tr2 v,
adamc@201 83 print = #print tr2,
adamc@201 84 time = fn (input, pmap) => let
adamc@201 85 val (ro, pmap) = #time tr1 (input, pmap)
adamc@201 86 in
adamc@201 87 case ro of
adamc@201 88 NONE => (NONE, pmap)
adamc@201 89 | SOME v => #time tr2 (v, pmap)
adamc@201 90 end
adamc@201 91 }
adamc@201 92
adamc@201 93 fun run (tr : ('src, 'dst) transform) = #func tr
adamc@201 94
adamc@201 95 fun runPrint (tr : ('src, 'dst) transform) input =
adamc@201 96 case #func tr input of
adamc@201 97 NONE => print "Failure\n"
adamc@201 98 | SOME v =>
adamc@201 99 (print "Success\n";
adamc@201 100 Print.print (#print tr v);
adamc@201 101 print "\n")
adamc@201 102
adamc@201 103 fun time (tr : ('src, 'dst) transform) input =
adamc@55 104 let
adamc@201 105 val (_, pmap) = #time tr (input, [])
adamc@201 106 in
adamc@201 107 app (fn (name, time) =>
adamc@201 108 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 109 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 110 print "\n"
adamc@201 111 end
adamc@55 112
adamc@201 113 fun timePrint (tr : ('src, 'dst) transform) input =
adamc@201 114 let
adamc@201 115 val (ro, pmap) = #time tr (input, [])
adamc@55 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 case ro of
adamc@201 122 NONE => print "Failure\n"
adamc@201 123 | SOME v =>
adamc@201 124 (print "Success\n";
adamc@201 125 Print.print (#print tr v);
adamc@201 126 print "\n")
adamc@55 127 end
adamc@55 128
adamc@244 129 val parseUrs =
adamc@201 130 {func = fn filename => let
adamc@201 131 val fname = OS.FileSys.tmpName ()
adamc@201 132 val outf = TextIO.openOut fname
adamc@201 133 val () = TextIO.output (outf, "sig\n")
adamc@201 134 val inf = TextIO.openIn filename
adamc@201 135 fun loop () =
adamc@201 136 case TextIO.inputLine inf of
adamc@201 137 NONE => ()
adamc@201 138 | SOME line => (TextIO.output (outf, line);
adamc@201 139 loop ())
adamc@201 140 val () = loop ()
adamc@201 141 val () = TextIO.closeIn inf
adamc@201 142 val () = TextIO.closeOut outf
adamc@201 143
adamc@201 144 val () = (ErrorMsg.resetErrors ();
adamc@201 145 ErrorMsg.resetPositioning filename;
adamc@201 146 Lex.UserDeclarations.initialize ())
adamc@201 147 val file = TextIO.openIn fname
adamc@201 148 fun get _ = TextIO.input file
adamc@201 149 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 150 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 151 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 152 in
adamc@201 153 TextIO.closeIn file;
adamc@201 154 case absyn of
adamc@201 155 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
adamc@201 156 | _ => (ErrorMsg.errorAt {file = filename,
adamc@201 157 first = {line = 0,
adamc@201 158 char = 0},
adamc@201 159 last = {line = 0,
adamc@201 160 char = 0}} "Not a signature";
adamc@201 161 [])
adamc@201 162 end
adamc@201 163 handle LrParser.ParseError => [],
adamc@201 164 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
adamc@55 165
adamc@1 166 (* The main parsing routine *)
adamc@244 167 val parseUr = {
adamc@201 168 func = fn filename =>
adamc@201 169 let
adamc@201 170 val () = (ErrorMsg.resetErrors ();
adamc@201 171 ErrorMsg.resetPositioning filename;
adamc@201 172 Lex.UserDeclarations.initialize ())
adamc@201 173 val file = TextIO.openIn filename
adamc@201 174 fun get _ = TextIO.input file
adamc@201 175 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 176 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 177 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 178 in
adamc@201 179 TextIO.closeIn file;
adamc@201 180 case absyn of
adamc@201 181 [(Source.DSgn ("?", _), _)] =>
adamc@201 182 (ErrorMsg.errorAt {file = filename,
adamc@201 183 first = {line = 0,
adamc@201 184 char = 0},
adamc@201 185 last = {line = 0,
adamc@201 186 char = 0}} "File starts with 'sig'";
adamc@201 187 [])
adamc@201 188 | _ => absyn
adamc@201 189 end
adamc@201 190 handle LrParser.ParseError => [],
adamc@201 191 print = SourcePrint.p_file}
adamc@56 192
adamc@270 193 fun p_job {database, sources} =
adamc@270 194 let
adamc@270 195 open Print.PD
adamc@270 196 open Print
adamc@270 197 in
adamc@270 198 box [case database of
adamc@270 199 NONE => string "No database."
adamc@270 200 | SOME db => string ("Database: " ^ db),
adamc@270 201 newline,
adamc@270 202 string "Sources:",
adamc@270 203 p_list string sources,
adamc@270 204 newline]
adamc@270 205 end
adamc@270 206
adamc@270 207 fun trim s =
adamc@270 208 let
adamc@270 209 val (_, s) = Substring.splitl Char.isSpace s
adamc@270 210 val (s, _) = Substring.splitr Char.isSpace s
adamc@270 211 in
adamc@270 212 s
adamc@270 213 end
adamc@270 214
adamc@270 215 val parseUrp = {
adamc@270 216 func = fn filename =>
adamc@270 217 let
adamc@270 218 val dir = OS.Path.dir filename
adamc@270 219 val inf = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@270 220
adamc@270 221 fun readSources acc =
adamc@270 222 case TextIO.inputLine inf of
adamc@270 223 NONE => rev acc
adamc@270 224 | SOME line =>
adamc@270 225 let
adamc@270 226 val acc = if CharVector.all Char.isSpace line then
adamc@270 227 acc
adamc@270 228 else
adamc@270 229 let
adamc@270 230 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adamc@270 231 (String.explode line))
adamc@270 232 val fname = OS.Path.concat (dir, fname)
adamc@270 233 in
adamc@270 234 fname :: acc
adamc@270 235 end
adamc@270 236 in
adamc@270 237 readSources acc
adamc@270 238 end
adamc@270 239
adamc@270 240 fun read database =
adamc@270 241 case TextIO.inputLine inf of
adamc@270 242 NONE => {database = database, sources = []}
adamc@270 243 | SOME "\n" => {database = database, sources = readSources []}
adamc@270 244 | SOME line =>
adamc@270 245 let
adamc@270 246 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adamc@270 247 val cmd = Substring.string (trim cmd)
adamc@270 248 val arg = Substring.string (trim arg)
adamc@270 249 in
adamc@270 250 case cmd of
adamc@270 251 "database" =>
adamc@270 252 (case database of
adamc@270 253 NONE => ()
adamc@270 254 | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
adamc@270 255 read (SOME arg))
adamc@270 256 | _ => (ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adamc@270 257 read database)
adamc@270 258 end
adamc@270 259 in
adamc@270 260 read NONE
adamc@270 261 before TextIO.closeIn inf
adamc@270 262 end,
adamc@270 263 print = p_job
adamc@270 264 }
adamc@270 265
adamc@270 266 val toParseJob = transform parseUrp "parseJob"
adamc@270 267
adamc@56 268 fun capitalize "" = ""
adamc@56 269 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 270
adamc@201 271 val parse = {
adamc@270 272 func = fn {database, sources = fnames} =>
adamc@201 273 let
adamc@201 274 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 275
adamc@201 276 fun parseOne fname =
adamc@201 277 let
adamc@201 278 val mname = nameOf fname
adamc@244 279 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 280 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 281
adamc@201 282 val sgnO =
adamc@244 283 if Posix.FileSys.access (urs, []) then
adamc@244 284 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 285 {file = urs,
adamc@201 286 first = ErrorMsg.dummyPos,
adamc@201 287 last = ErrorMsg.dummyPos})
adamc@201 288 else
adamc@201 289 NONE
adamc@56 290
adamc@244 291 val loc = {file = ur,
adamc@201 292 first = ErrorMsg.dummyPos,
adamc@201 293 last = ErrorMsg.dummyPos}
adamc@56 294
adamc@244 295 val ds = #func parseUr ur
adamc@201 296 in
adamc@201 297 (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@201 298 end
adamc@56 299
adamc@201 300 val ds = map parseOne fnames
adamc@201 301 in
adamc@201 302 let
adamc@201 303 val final = nameOf (List.last fnames)
adamc@201 304 in
adamc@201 305 ds @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
adamc@201 306 end handle Empty => ds
adamc@201 307 end,
adamc@201 308 print = SourcePrint.p_file
adamc@201 309 }
adamc@56 310
adamc@270 311 val toParse = transform parse "parse" o toParseJob
adamc@38 312
adamc@201 313 val elaborate = {
adamc@201 314 func = fn file => let
adamc@244 315 val basis = #func parseUrs "lib/basis.urs"
adamc@201 316 in
adamc@201 317 Elaborate.elabFile basis ElabEnv.empty file
adamc@201 318 end,
adamc@201 319 print = ElabPrint.p_file ElabEnv.empty
adamc@201 320 }
adamc@5 321
adamc@270 322 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 323
adamc@201 324 val explify = {
adamc@201 325 func = Explify.explify,
adamc@201 326 print = ExplPrint.p_file ExplEnv.empty
adamc@201 327 }
adamc@201 328
adamc@270 329 val toExplify = transform explify "explify" o toElaborate
adamc@201 330
adamc@201 331 val corify = {
adamc@201 332 func = Corify.corify,
adamc@201 333 print = CorePrint.p_file CoreEnv.empty
adamc@201 334 }
adamc@201 335
adamc@270 336 val toCorify = transform corify "corify" o toExplify
adamc@201 337
adamc@202 338 val shake = {
adamc@202 339 func = Shake.shake,
adamc@202 340 print = CorePrint.p_file CoreEnv.empty
adamc@202 341 }
adamc@39 342
adamc@270 343 val toShake1 = transform shake "shake1" o toCorify
adamc@110 344
adamc@202 345 val tag = {
adamc@202 346 func = Tag.tag,
adamc@202 347 print = CorePrint.p_file CoreEnv.empty
adamc@202 348 }
adamc@193 349
adamc@270 350 val toTag = transform tag "tag" o toShake1
adamc@20 351
adamc@202 352 val reduce = {
adamc@202 353 func = Reduce.reduce,
adamc@202 354 print = CorePrint.p_file CoreEnv.empty
adamc@202 355 }
adamc@25 356
adamc@270 357 val toReduce = transform reduce "reduce" o toTag
adamc@23 358
adamc@202 359 val specialize = {
adamc@202 360 func = Specialize.specialize,
adamc@202 361 print = CorePrint.p_file CoreEnv.empty
adamc@202 362 }
adamc@132 363
adamc@270 364 val toSpecialize = transform specialize "specialize" o toReduce
adamc@131 365
adamc@270 366 val toShake2 = transform shake "shake2" o toSpecialize
adamc@133 367
adamc@202 368 val monoize = {
adamc@202 369 func = Monoize.monoize CoreEnv.empty,
adamc@202 370 print = MonoPrint.p_file MonoEnv.empty
adamc@202 371 }
adamc@134 372
adamc@270 373 val toMonoize = transform monoize "monoize" o toShake2
adamc@96 374
adamc@202 375 val mono_opt = {
adamc@202 376 func = MonoOpt.optimize,
adamc@202 377 print = MonoPrint.p_file MonoEnv.empty
adamc@202 378 }
adamc@29 379
adamc@270 380 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 381
adamc@202 382 val untangle = {
adamc@202 383 func = Untangle.untangle,
adamc@202 384 print = MonoPrint.p_file MonoEnv.empty
adamc@202 385 }
adamc@1 386
adamc@270 387 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 388
adamc@202 389 val mono_reduce = {
adamc@202 390 func = MonoReduce.reduce,
adamc@202 391 print = MonoPrint.p_file MonoEnv.empty
adamc@202 392 }
adamc@16 393
adamc@270 394 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 395
adamc@202 396 val mono_shake = {
adamc@202 397 func = MonoShake.shake,
adamc@202 398 print = MonoPrint.p_file MonoEnv.empty
adamc@202 399 }
adamc@110 400
adamc@270 401 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 402
adamc@270 403 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@20 404
adamc@202 405 val cjrize = {
adamc@202 406 func = Cjrize.cjrize,
adamc@202 407 print = CjrPrint.p_file CjrEnv.empty
adamc@202 408 }
adamc@23 409
adamc@270 410 val toCjrize = transform cjrize "cjrize" o toMono_opt2
adamc@29 411
adamc@183 412 fun compileC {cname, oname, ename} =
adamc@183 413 let
adamc@183 414 val compile = "gcc -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@244 415 val link = "gcc -pthread -O3 clib/urweb.o " ^ oname ^ " clib/driver.o -o " ^ ename
adamc@183 416 in
adamc@183 417 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@183 418 print "C compilation failed\n"
adamc@183 419 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@186 420 print "C linking failed\n"
adamc@183 421 else
adamc@183 422 print "Success\n"
adamc@183 423 end
adamc@183 424
adamc@202 425 fun compile job =
adamc@202 426 case run toCjrize job of
adamc@244 427 NONE => print "Ur compilation failed\n"
adamc@29 428 | SOME file =>
adamc@202 429 let
adamc@244 430 val cname = "/tmp/urweb.c"
adamc@244 431 val oname = "/tmp/urweb.o"
adamc@202 432 val ename = "/tmp/webapp"
adamc@102 433
adamc@202 434 val outf = TextIO.openOut cname
adamc@202 435 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@202 436 in
adamc@202 437 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@202 438 TextIO.closeOut outf;
adamc@102 439
adamc@202 440 compileC {cname = cname, oname = oname, ename = ename}
adamc@202 441 end
adamc@29 442
adamc@1 443 end