annotate src/compiler.sml @ 271:42dfb0d61cf0

'database' declaration threaded through compiler
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 10:51:41 -0400
parents b9b02613c0c2
children 4d80d6122df1
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@271 233 handle OS.Path.Path => fname
adamc@270 234 in
adamc@270 235 fname :: acc
adamc@270 236 end
adamc@270 237 in
adamc@270 238 readSources acc
adamc@270 239 end
adamc@270 240
adamc@270 241 fun read database =
adamc@270 242 case TextIO.inputLine inf of
adamc@270 243 NONE => {database = database, sources = []}
adamc@270 244 | SOME "\n" => {database = database, sources = readSources []}
adamc@270 245 | SOME line =>
adamc@270 246 let
adamc@270 247 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adamc@270 248 val cmd = Substring.string (trim cmd)
adamc@270 249 val arg = Substring.string (trim arg)
adamc@270 250 in
adamc@270 251 case cmd of
adamc@270 252 "database" =>
adamc@270 253 (case database of
adamc@270 254 NONE => ()
adamc@270 255 | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
adamc@270 256 read (SOME arg))
adamc@270 257 | _ => (ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adamc@270 258 read database)
adamc@270 259 end
adamc@270 260 in
adamc@270 261 read NONE
adamc@270 262 before TextIO.closeIn inf
adamc@270 263 end,
adamc@270 264 print = p_job
adamc@270 265 }
adamc@270 266
adamc@270 267 val toParseJob = transform parseUrp "parseJob"
adamc@270 268
adamc@56 269 fun capitalize "" = ""
adamc@56 270 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 271
adamc@201 272 val parse = {
adamc@270 273 func = fn {database, sources = fnames} =>
adamc@201 274 let
adamc@201 275 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 276
adamc@201 277 fun parseOne fname =
adamc@201 278 let
adamc@201 279 val mname = nameOf fname
adamc@244 280 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 281 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 282
adamc@201 283 val sgnO =
adamc@244 284 if Posix.FileSys.access (urs, []) then
adamc@244 285 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 286 {file = urs,
adamc@201 287 first = ErrorMsg.dummyPos,
adamc@201 288 last = ErrorMsg.dummyPos})
adamc@201 289 else
adamc@201 290 NONE
adamc@56 291
adamc@244 292 val loc = {file = ur,
adamc@201 293 first = ErrorMsg.dummyPos,
adamc@201 294 last = ErrorMsg.dummyPos}
adamc@56 295
adamc@244 296 val ds = #func parseUr ur
adamc@201 297 in
adamc@201 298 (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@201 299 end
adamc@56 300
adamc@201 301 val ds = map parseOne fnames
adamc@201 302 in
adamc@201 303 let
adamc@201 304 val final = nameOf (List.last fnames)
adamc@271 305
adamc@271 306 val ds = ds @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
adamc@201 307 in
adamc@271 308 case database of
adamc@271 309 NONE => ds
adamc@271 310 | SOME s => (Source.DDatabase s, ErrorMsg.dummySpan) :: ds
adamc@201 311 end handle Empty => ds
adamc@201 312 end,
adamc@201 313 print = SourcePrint.p_file
adamc@201 314 }
adamc@56 315
adamc@270 316 val toParse = transform parse "parse" o toParseJob
adamc@38 317
adamc@201 318 val elaborate = {
adamc@201 319 func = fn file => let
adamc@244 320 val basis = #func parseUrs "lib/basis.urs"
adamc@201 321 in
adamc@201 322 Elaborate.elabFile basis ElabEnv.empty file
adamc@201 323 end,
adamc@201 324 print = ElabPrint.p_file ElabEnv.empty
adamc@201 325 }
adamc@5 326
adamc@270 327 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 328
adamc@201 329 val explify = {
adamc@201 330 func = Explify.explify,
adamc@201 331 print = ExplPrint.p_file ExplEnv.empty
adamc@201 332 }
adamc@201 333
adamc@270 334 val toExplify = transform explify "explify" o toElaborate
adamc@201 335
adamc@201 336 val corify = {
adamc@201 337 func = Corify.corify,
adamc@201 338 print = CorePrint.p_file CoreEnv.empty
adamc@201 339 }
adamc@201 340
adamc@270 341 val toCorify = transform corify "corify" o toExplify
adamc@201 342
adamc@202 343 val shake = {
adamc@202 344 func = Shake.shake,
adamc@202 345 print = CorePrint.p_file CoreEnv.empty
adamc@202 346 }
adamc@39 347
adamc@270 348 val toShake1 = transform shake "shake1" o toCorify
adamc@110 349
adamc@202 350 val tag = {
adamc@202 351 func = Tag.tag,
adamc@202 352 print = CorePrint.p_file CoreEnv.empty
adamc@202 353 }
adamc@193 354
adamc@270 355 val toTag = transform tag "tag" o toShake1
adamc@20 356
adamc@202 357 val reduce = {
adamc@202 358 func = Reduce.reduce,
adamc@202 359 print = CorePrint.p_file CoreEnv.empty
adamc@202 360 }
adamc@25 361
adamc@270 362 val toReduce = transform reduce "reduce" o toTag
adamc@23 363
adamc@202 364 val specialize = {
adamc@202 365 func = Specialize.specialize,
adamc@202 366 print = CorePrint.p_file CoreEnv.empty
adamc@202 367 }
adamc@132 368
adamc@270 369 val toSpecialize = transform specialize "specialize" o toReduce
adamc@131 370
adamc@270 371 val toShake2 = transform shake "shake2" o toSpecialize
adamc@133 372
adamc@202 373 val monoize = {
adamc@202 374 func = Monoize.monoize CoreEnv.empty,
adamc@202 375 print = MonoPrint.p_file MonoEnv.empty
adamc@202 376 }
adamc@134 377
adamc@270 378 val toMonoize = transform monoize "monoize" o toShake2
adamc@96 379
adamc@202 380 val mono_opt = {
adamc@202 381 func = MonoOpt.optimize,
adamc@202 382 print = MonoPrint.p_file MonoEnv.empty
adamc@202 383 }
adamc@29 384
adamc@270 385 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 386
adamc@202 387 val untangle = {
adamc@202 388 func = Untangle.untangle,
adamc@202 389 print = MonoPrint.p_file MonoEnv.empty
adamc@202 390 }
adamc@1 391
adamc@270 392 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 393
adamc@202 394 val mono_reduce = {
adamc@202 395 func = MonoReduce.reduce,
adamc@202 396 print = MonoPrint.p_file MonoEnv.empty
adamc@202 397 }
adamc@16 398
adamc@270 399 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 400
adamc@202 401 val mono_shake = {
adamc@202 402 func = MonoShake.shake,
adamc@202 403 print = MonoPrint.p_file MonoEnv.empty
adamc@202 404 }
adamc@110 405
adamc@270 406 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 407
adamc@270 408 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@20 409
adamc@202 410 val cjrize = {
adamc@202 411 func = Cjrize.cjrize,
adamc@202 412 print = CjrPrint.p_file CjrEnv.empty
adamc@202 413 }
adamc@23 414
adamc@270 415 val toCjrize = transform cjrize "cjrize" o toMono_opt2
adamc@29 416
adamc@183 417 fun compileC {cname, oname, ename} =
adamc@183 418 let
adamc@183 419 val compile = "gcc -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@244 420 val link = "gcc -pthread -O3 clib/urweb.o " ^ oname ^ " clib/driver.o -o " ^ ename
adamc@183 421 in
adamc@183 422 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@183 423 print "C compilation failed\n"
adamc@183 424 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@186 425 print "C linking failed\n"
adamc@183 426 else
adamc@183 427 print "Success\n"
adamc@183 428 end
adamc@183 429
adamc@202 430 fun compile job =
adamc@202 431 case run toCjrize job of
adamc@244 432 NONE => print "Ur compilation failed\n"
adamc@29 433 | SOME file =>
adamc@202 434 let
adamc@244 435 val cname = "/tmp/urweb.c"
adamc@244 436 val oname = "/tmp/urweb.o"
adamc@202 437 val ename = "/tmp/webapp"
adamc@102 438
adamc@202 439 val outf = TextIO.openOut cname
adamc@202 440 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@202 441 in
adamc@202 442 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@202 443 TextIO.closeOut outf;
adamc@102 444
adamc@202 445 compileC {cname = cname, oname = oname, ename = ename}
adamc@202 446 end
adamc@29 447
adamc@1 448 end