annotate src/compiler.sml @ 134:18299126a343

MonoShake
author Adam Chlipala <adamc@hcoop.net>
date Thu, 17 Jul 2008 13:12:16 -0400
parents 55d8cfa4d024
children b1cfe49ce692
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@1 28 (* Laconic/Web language parser *)
adamc@1 29
adamc@1 30 structure Compiler :> COMPILER = struct
adamc@1 31
adamc@1 32 structure LacwebLrVals = LacwebLrValsFn(structure Token = LrParser.Token)
adamc@1 33 structure Lex = LacwebLexFn(structure Tokens = LacwebLrVals.Tokens)
adamc@1 34 structure LacwebP = Join(structure ParserData = LacwebLrVals.ParserData
adamc@1 35 structure Lex = Lex
adamc@1 36 structure LrParser = LrParser)
adamc@1 37
adamc@55 38 fun parseLig filename =
adamc@55 39 let
adamc@55 40 val fname = OS.FileSys.tmpName ()
adamc@55 41 val outf = TextIO.openOut fname
adamc@55 42 val () = TextIO.output (outf, "sig\n")
adamc@55 43 val inf = TextIO.openIn filename
adamc@55 44 fun loop () =
adamc@55 45 case TextIO.inputLine inf of
adamc@55 46 NONE => ()
adamc@55 47 | SOME line => (TextIO.output (outf, line);
adamc@55 48 loop ())
adamc@55 49 val () = loop ()
adamc@55 50 val () = TextIO.closeIn inf
adamc@55 51 val () = TextIO.closeOut outf
adamc@55 52
adamc@55 53 val () = (ErrorMsg.resetErrors ();
adamc@55 54 ErrorMsg.resetPositioning filename)
adamc@55 55 val file = TextIO.openIn fname
adamc@55 56 fun get _ = TextIO.input file
adamc@55 57 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@55 58 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@55 59 val (absyn, _) = LacwebP.parse (30, lexer, parseerror, ())
adamc@55 60 in
adamc@55 61 TextIO.closeIn file;
adamc@55 62 if ErrorMsg.anyErrors () then
adamc@55 63 NONE
adamc@55 64 else
adamc@55 65 case absyn of
adamc@55 66 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => SOME sgis
adamc@55 67 | _ => NONE
adamc@55 68 end
adamc@55 69 handle LrParser.ParseError => NONE
adamc@55 70
adamc@55 71 fun testLig fname =
adamc@55 72 case parseLig fname of
adamc@55 73 NONE => ()
adamc@55 74 | SOME sgis =>
adamc@55 75 app (fn sgi => (Print.print (SourcePrint.p_sgn_item sgi);
adamc@55 76 print "\n")) sgis
adamc@55 77
adamc@1 78 (* The main parsing routine *)
adamc@56 79 fun parseLac filename =
adamc@1 80 let
adamc@1 81 val () = (ErrorMsg.resetErrors ();
adamc@1 82 ErrorMsg.resetPositioning filename)
adamc@1 83 val file = TextIO.openIn filename
adamc@1 84 fun get _ = TextIO.input file
adamc@1 85 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@1 86 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@1 87 val (absyn, _) = LacwebP.parse (30, lexer, parseerror, ())
adamc@1 88 in
adamc@1 89 TextIO.closeIn file;
adamc@5 90 if ErrorMsg.anyErrors () then
adamc@5 91 NONE
adamc@5 92 else
adamc@55 93 case absyn of
adamc@55 94 [(Source.DSgn ("?", _), _)] =>
adamc@55 95 (ErrorMsg.error "File starts with 'sig'";
adamc@55 96 NONE)
adamc@55 97 | _ => SOME absyn
adamc@1 98 end
adamc@1 99 handle LrParser.ParseError => NONE
adamc@1 100
adamc@56 101 fun testLac fname =
adamc@56 102 case parseLac fname of
adamc@56 103 NONE => ()
adamc@56 104 | SOME file => (Print.print (SourcePrint.p_file file);
adamc@56 105 print "\n")
adamc@56 106
adamc@56 107 type job = string list
adamc@56 108
adamc@56 109 fun capitalize "" = ""
adamc@56 110 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 111
adamc@56 112 fun parse fnames =
adamc@56 113 let
adamc@109 114 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 115
adamc@56 116 fun parseOne fname =
adamc@56 117 let
adamc@109 118 val mname = nameOf fname
adamc@56 119 val lac = OS.Path.joinBaseExt {base = fname, ext = SOME "lac"}
adamc@56 120 val lig = OS.Path.joinBaseExt {base = fname, ext = SOME "lig"}
adamc@56 121
adamc@56 122 val sgnO =
adamc@56 123 if Posix.FileSys.access (lig, []) then
adamc@56 124 case parseLig lig of
adamc@56 125 NONE => NONE
adamc@56 126 | SOME sgis => SOME (Source.SgnConst sgis, {file = lig,
adamc@56 127 first = ErrorMsg.dummyPos,
adamc@56 128 last = ErrorMsg.dummyPos})
adamc@56 129 else
adamc@56 130 NONE
adamc@56 131
adamc@56 132 val loc = {file = lac,
adamc@56 133 first = ErrorMsg.dummyPos,
adamc@56 134 last = ErrorMsg.dummyPos}
adamc@56 135 in
adamc@56 136 case parseLac lac of
adamc@56 137 NONE => NONE
adamc@56 138 | SOME ds =>
adamc@56 139 SOME (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@56 140 end
adamc@56 141
adamc@109 142 val ds = List.mapPartial parseOne fnames
adamc@109 143 val ds =
adamc@109 144 let
adamc@109 145 val final = nameOf (List.last fnames)
adamc@109 146 in
adamc@109 147 ds @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
adamc@109 148 end handle Empty => ds
adamc@56 149 in
adamc@56 150 if ErrorMsg.anyErrors () then
adamc@56 151 NONE
adamc@56 152 else
adamc@56 153 SOME ds
adamc@56 154 end
adamc@56 155
adamc@56 156 fun elaborate job =
adamc@56 157 case parseLig "lib/basis.lig" of
adamc@56 158 NONE => NONE
adamc@56 159 | SOME empty =>
adamc@56 160 case parse job of
adamc@56 161 NONE => NONE
adamc@56 162 | SOME file =>
adamc@56 163 let
adamc@56 164 val out = Elaborate.elabFile empty ElabEnv.empty file
adamc@56 165 in
adamc@56 166 if ErrorMsg.anyErrors () then
adamc@56 167 NONE
adamc@56 168 else
adamc@56 169 SOME out
adamc@56 170 end
adamc@56 171
adamc@56 172 fun explify job =
adamc@56 173 case elaborate job of
adamc@5 174 NONE => NONE
adamc@5 175 | SOME file =>
adamc@38 176 if ErrorMsg.anyErrors () then
adamc@38 177 NONE
adamc@38 178 else
adamc@38 179 SOME (Explify.explify file)
adamc@38 180
adamc@56 181 fun corify job =
adamc@56 182 case explify job of
adamc@16 183 NONE => NONE
adamc@39 184 | SOME file =>
adamc@25 185 if ErrorMsg.anyErrors () then
adamc@25 186 NONE
adamc@25 187 else
adamc@25 188 SOME (Corify.corify file)
adamc@5 189
adamc@56 190 fun shake' job =
adamc@56 191 case corify job of
adamc@39 192 NONE => NONE
adamc@39 193 | SOME file =>
adamc@39 194 if ErrorMsg.anyErrors () then
adamc@39 195 NONE
adamc@39 196 else
adamc@39 197 SOME (Shake.shake file)
adamc@39 198
adamc@110 199 fun tag job =
adamc@110 200 case shake' job of
adamc@110 201 NONE => NONE
adamc@110 202 | SOME file =>
adamc@110 203 if ErrorMsg.anyErrors () then
adamc@110 204 NONE
adamc@110 205 else
adamc@110 206 SOME (Tag.tag file)
adamc@110 207
adamc@56 208 fun reduce job =
adamc@110 209 case tag job of
adamc@20 210 NONE => NONE
adamc@25 211 | SOME file =>
adamc@25 212 if ErrorMsg.anyErrors () then
adamc@25 213 NONE
adamc@25 214 else
adamc@25 215 SOME (Reduce.reduce (Shake.shake file))
adamc@20 216
adamc@56 217 fun shake job =
adamc@56 218 case reduce job of
adamc@23 219 NONE => NONE
adamc@25 220 | SOME file =>
adamc@25 221 if ErrorMsg.anyErrors () then
adamc@25 222 NONE
adamc@25 223 else
adamc@25 224 SOME (Shake.shake file)
adamc@25 225
adamc@56 226 fun monoize job =
adamc@56 227 case shake job of
adamc@25 228 NONE => NONE
adamc@25 229 | SOME file =>
adamc@25 230 if ErrorMsg.anyErrors () then
adamc@25 231 NONE
adamc@25 232 else
adamc@56 233 SOME (Monoize.monoize CoreEnv.empty file)
adamc@23 234
adamc@132 235 fun mono_opt' job =
adamc@132 236 case monoize job of
adamc@132 237 NONE => NONE
adamc@132 238 | SOME file =>
adamc@132 239 if ErrorMsg.anyErrors () then
adamc@132 240 NONE
adamc@132 241 else
adamc@132 242 SOME (MonoOpt.optimize file)
adamc@132 243
adamc@131 244 fun untangle job =
adamc@132 245 case mono_opt' job of
adamc@131 246 NONE => NONE
adamc@131 247 | SOME file =>
adamc@131 248 if ErrorMsg.anyErrors () then
adamc@131 249 NONE
adamc@131 250 else
adamc@131 251 SOME (Untangle.untangle file)
adamc@131 252
adamc@133 253 fun mono_reduce job =
adamc@133 254 case untangle job of
adamc@133 255 NONE => NONE
adamc@133 256 | SOME file =>
adamc@133 257 if ErrorMsg.anyErrors () then
adamc@133 258 NONE
adamc@133 259 else
adamc@133 260 SOME (MonoReduce.reduce file)
adamc@133 261
adamc@134 262 fun mono_shake job =
adamc@134 263 case mono_reduce job of
adamc@134 264 NONE => NONE
adamc@134 265 | SOME file =>
adamc@134 266 if ErrorMsg.anyErrors () then
adamc@134 267 NONE
adamc@134 268 else
adamc@134 269 SOME (MonoShake.shake file)
adamc@134 270
adamc@96 271 fun mono_opt job =
adamc@134 272 case mono_shake job of
adamc@96 273 NONE => NONE
adamc@96 274 | SOME file =>
adamc@96 275 if ErrorMsg.anyErrors () then
adamc@96 276 NONE
adamc@96 277 else
adamc@96 278 SOME (MonoOpt.optimize file)
adamc@96 279
adamc@109 280 fun cjrize job =
adamc@96 281 case mono_opt job of
adamc@26 282 NONE => NONE
adamc@26 283 | SOME file =>
adamc@26 284 if ErrorMsg.anyErrors () then
adamc@26 285 NONE
adamc@26 286 else
adamc@29 287 SOME (Cjrize.cjrize file)
adamc@29 288
adamc@56 289 fun testParse job =
adamc@56 290 case parse job of
adamc@5 291 NONE => print "Failed\n"
adamc@1 292 | SOME file =>
adamc@5 293 (Print.print (SourcePrint.p_file file);
adamc@5 294 print "\n")
adamc@5 295
adamc@56 296 fun testElaborate job =
adamc@56 297 (case elaborate job of
adamc@5 298 NONE => print "Failed\n"
adamc@56 299 | SOME file =>
adamc@32 300 (print "Succeeded\n";
adamc@56 301 Print.print (ElabPrint.p_file ElabEnv.empty file);
adamc@5 302 print "\n"))
adamc@5 303 handle ElabEnv.UnboundNamed n =>
adamc@5 304 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@1 305
adamc@56 306 fun testExplify job =
adamc@56 307 (case explify job of
adamc@38 308 NONE => print "Failed\n"
adamc@38 309 | SOME file =>
adamc@56 310 (Print.print (ExplPrint.p_file ExplEnv.empty file);
adamc@38 311 print "\n"))
adamc@38 312 handle ExplEnv.UnboundNamed n =>
adamc@38 313 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@38 314
adamc@56 315 fun testCorify job =
adamc@56 316 (case corify job of
adamc@16 317 NONE => print "Failed\n"
adamc@16 318 | SOME file =>
adamc@56 319 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@16 320 print "\n"))
adamc@16 321 handle CoreEnv.UnboundNamed n =>
adamc@16 322 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@16 323
adamc@56 324 fun testShake' job =
adamc@56 325 (case shake' job of
adamc@39 326 NONE => print "Failed\n"
adamc@39 327 | SOME file =>
adamc@56 328 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@39 329 print "\n"))
adamc@39 330 handle CoreEnv.UnboundNamed n =>
adamc@39 331 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@39 332
adamc@110 333 fun testTag job =
adamc@110 334 (case tag job of
adamc@110 335 NONE => print "Failed\n"
adamc@110 336 | SOME file =>
adamc@110 337 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@110 338 print "\n"))
adamc@110 339 handle CoreEnv.UnboundNamed n =>
adamc@110 340 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@110 341
adamc@56 342 fun testReduce job =
adamc@131 343 (case tag job of
adamc@20 344 NONE => print "Failed\n"
adamc@20 345 | SOME file =>
adamc@56 346 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@20 347 print "\n"))
adamc@20 348 handle CoreEnv.UnboundNamed n =>
adamc@20 349 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@20 350
adamc@56 351 fun testShake job =
adamc@56 352 (case shake job of
adamc@23 353 NONE => print "Failed\n"
adamc@23 354 | SOME file =>
adamc@56 355 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@23 356 print "\n"))
adamc@23 357 handle CoreEnv.UnboundNamed n =>
adamc@23 358 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@23 359
adamc@56 360 fun testMonoize job =
adamc@56 361 (case monoize job of
adamc@25 362 NONE => print "Failed\n"
adamc@25 363 | SOME file =>
adamc@56 364 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@25 365 print "\n"))
adamc@25 366 handle MonoEnv.UnboundNamed n =>
adamc@25 367 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@25 368
adamc@132 369 fun testMono_opt' job =
adamc@132 370 (case mono_opt' job of
adamc@132 371 NONE => print "Failed\n"
adamc@132 372 | SOME file =>
adamc@132 373 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@132 374 print "\n"))
adamc@132 375 handle MonoEnv.UnboundNamed n =>
adamc@132 376 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@132 377
adamc@131 378 fun testUntangle job =
adamc@131 379 (case untangle job of
adamc@131 380 NONE => print "Failed\n"
adamc@131 381 | SOME file =>
adamc@131 382 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@131 383 print "\n"))
adamc@131 384 handle MonoEnv.UnboundNamed n =>
adamc@131 385 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@131 386
adamc@133 387 fun testMono_reduce job =
adamc@133 388 (case mono_reduce job of
adamc@133 389 NONE => print "Failed\n"
adamc@133 390 | SOME file =>
adamc@133 391 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@133 392 print "\n"))
adamc@133 393 handle MonoEnv.UnboundNamed n =>
adamc@133 394 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@133 395
adamc@134 396 fun testMono_shake job =
adamc@134 397 (case mono_shake job of
adamc@134 398 NONE => print "Failed\n"
adamc@134 399 | SOME file =>
adamc@134 400 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@134 401 print "\n"))
adamc@134 402 handle MonoEnv.UnboundNamed n =>
adamc@134 403 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@134 404
adamc@96 405 fun testMono_opt job =
adamc@96 406 (case mono_opt job of
adamc@96 407 NONE => print "Failed\n"
adamc@96 408 | SOME file =>
adamc@96 409 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@96 410 print "\n"))
adamc@96 411 handle MonoEnv.UnboundNamed n =>
adamc@96 412 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@96 413
adamc@56 414 fun testCjrize job =
adamc@56 415 (case cjrize job of
adamc@29 416 NONE => print "Failed\n"
adamc@29 417 | SOME file =>
adamc@56 418 (Print.print (CjrPrint.p_file CjrEnv.empty file);
adamc@29 419 print "\n"))
adamc@29 420 handle CjrEnv.UnboundNamed n =>
adamc@29 421 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@29 422
adamc@56 423 fun compile job =
adamc@56 424 case cjrize job of
adamc@114 425 NONE => print "Laconic compilation failed\n"
adamc@29 426 | SOME file =>
adamc@29 427 let
adamc@102 428 val cname = "/tmp/lacweb.c"
adamc@102 429 val oname = "/tmp/lacweb.o"
adamc@102 430 val ename = "/tmp/webapp"
adamc@102 431
adamc@113 432 val compile = "gcc -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@113 433 val link = "gcc -O3 clib/lacweb.o " ^ oname ^ " clib/driver.o -o " ^ ename
adamc@102 434
adamc@102 435 val outf = TextIO.openOut cname
adamc@29 436 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@29 437 in
adamc@56 438 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@102 439 TextIO.closeOut outf;
adamc@102 440
adamc@102 441 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@102 442 print "C compilation failed\n"
adamc@102 443 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@102 444 print "C linking failed\n"
adamc@102 445 else
adamc@102 446 print "Success\n"
adamc@29 447 end
adamc@29 448
adamc@1 449 end