annotate src/compiler.sml @ 193:8a70e2919e86

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