changeset 87:9a35a973175a new_gen

Some improvements to the compiler program
author Anders Halager <halager@gmail.com>
date Sun, 04 May 2008 12:58:02 +0200
parents 29f486ccc203
children eb5b2c719a39
files dang/compiler.d gen/CodeGen.d sema/ImplicitCast.d
diffstat 3 files changed, 93 insertions(+), 95 deletions(-) [+]
line wrap: on
line diff
--- a/dang/compiler.d	Fri May 02 21:21:18 2008 +0200
+++ b/dang/compiler.d	Sun May 04 12:58:02 2008 +0200
@@ -2,6 +2,7 @@
 
 import tango.io.Stdout,
        tango.core.Signal,
+       tango.time.StopWatch,
        tango.io.FilePath;
 
 import lexer.Lexer,
@@ -22,32 +23,36 @@
        sema.ImplicitCast,
        sema.Declarations;
 
-import dang.OptParse;
+import Opt = dang.OptParse;
 
 void checkFiles(char[][] *files)
 {
-    bool error = false;
+    bool non_existant_files = false;
+    bool duplicate_files = false;
 
     char[][] validFiles;
 
-    foreach(file ; *files)
+    foreach (file; *files)
     {
-        auto path = new FilePath(file);
+        scope path = new FilePath(file);
 
-        if(!path.exists)
+        if (!path.exists)
         {
-            Stdout("File '"~file~"' does not exists").newline;
-            error = true;
+            Stderr.formatln("'{}' does not exist", file).newline;
+            non_existant_files = true;
 
             continue;
         }
         
         bool fileInStack = false;
-        foreach(vFile ; validFiles)
-            if(vFile == file)
+        foreach (vFile; validFiles)
+            if (vFile == file)
+            {
                 fileInStack = true;
+                duplicate_files = true;
+            }
 
-        if(fileInStack)
+        if (fileInStack)
             continue;
 
         validFiles ~= file;
@@ -55,9 +60,12 @@
 
     *files = validFiles;
 
-    if(error)
-        throw new Exception("Some file(s) did not exist");
+    if (non_existant_files)
+        throw new Exception("All files given must exist");
+    if (duplicate_files)
+        Stderr("warning: duplicate files ignored").newline;
 }
+
 void main(char[][] args)
 {
     char[][] filesToHandle;
@@ -72,131 +80,123 @@
 
     preStart.attach(&checkFiles);
 
-    auto argParse = new OptionParser;
+    auto argParse = new Opt.OptionParser(`Dang "D" compiler v0.0`);
 
     bool optimize = false;
     bool inline = false;
 
-    argParse.addOption(
-            ["-h", "--help"],{
-                argParse.helpText();
-                return;
-            }
-    ).help("Show this help message");
-
-    argParse.addOption(
-            ["--ast-dump-dot"], {
-                postParse.attach(
-                    (Decl[] decls, DataSource src) {
-                        auto print = new DotPrinter();
-                        print.print(decls);
-                        exit(0);
-                    });
-            }
-    ).help("Output the AST as dot-graphicz");
+    argParse.addOption(["-h", "--help"], Opt.Action.Help)
+        .help("Show this help message");
 
-    argParse.addOption(
-            ["--ast-dump-code"], {
-                postParse.attach(
-                    (Decl[] decls, DataSource src) {
-                        auto print = new AstPrinter(src);
-                        print.print(decls);
-                        exit(0);
-                    });
-            }
-    ).help("Output the AST as dot-graphicz");
+    argParse.addOption(["--ast-dump-dot"],
+            "what-to-do", Opt.Action.StoreConst, "dot")
+        .help("Output the AST in the dot format");
+    argParse.addOption(["--ast-dump-code"],
+            "what-to-do", Opt.Action.StoreConst, "code")
+        .help("Output the AST as code");
 
-    argParse.addOption(
-            ["--gen-llvm"], {
-/*                postParse.attach(
-                    (Decl[] decls, DataSource src) {
-                        auto llvmGen = new CodeGen();
-                        llvmGen.gen(decls);
-                    }); */
-            }
-    ).help("Compile to LLVM code (default)");
+    argParse.addOption(["--gen-llvm"],
+            "what-to-do", Opt.Action.StoreConst, "gen-llvm")
+        .help("Compile to LLVM code (default)");
 
     argParse.addOption(
             ["-O","--optimize"], {
                 optimize = true;
             }
-    ).help("Optimize code when compiling to LLVM");
+    ).help("Tell LLVM to do its standard optimizations");
 
     argParse.addOption(
             ["--inline"], {
                 inline = true;
             }
-    ).help("Inline functions when compiling to LLVM");
+    ).help("Tell LLVM that its allowed to inline functions");
+
+    argParse
+        .addOption(["--time"], Opt.Action.SetTrue, "time")
+        .help("Time the various operations performed.");
 
     auto options = argParse.parse(args);
 
     filesToHandle ~= options.args;
     
-    try
-    {
-        preStart(&filesToHandle);
-    }
-    catch(Exception e)
-    {
-        return;
-    }
+    // Will throw exception if some files don't exist
+    preStart(&filesToHandle);
+
+    struct Measurement { char[] label; double time; }
+    Measurement[] timings;
 
-    postParse.attach(
-        (Decl[] decls, DataSource src) {
-            auto llvmGen = new CodeGen();
+    auto what = options["what-to-do"];
+    if (what == "" || what == "gen-llvm")
+        postParse.attach(
+            (Decl[] decls, DataSource src) {
+                StopWatch w; w.start;
+                auto llvmGen = new CodeGen();
                 llvmGen.gen(decls, optimize, inline);
+                timings ~= Measurement("Generating LLVM bytecode", w.stop);
+            });
+    else if (what == "dot")
+        postParse.attach(
+            (Decl[] decls, DataSource src) {
+                StopWatch w; w.start;
+                auto print = new DotPrinter();
+                print.print(decls);
+                timings ~= Measurement("Generating dot output", w.stop);
+            });
+    else if (what == "code")
+        postParse.attach(
+            (Decl[] decls, DataSource src) {
+                StopWatch w; w.start;
+                auto print = new AstPrinter(src);
+                print.print(decls);
+                timings ~= Measurement("Converting AST to text", w.stop);
             });
 
+    StopWatch total;
+    total.start;
     foreach(file ; filesToHandle)
     {
         preLex(file);
 
         auto src = DataSource(file);
         auto lexer = new Lexer(src);
-/*
-        auto t = lexer.next;
-        while(t.getType != "EOF")
-        {
-            Stdout(t.getType)(" : ")(t.get).newline;
-            t = lexer.next;
-        }
-        lexer = new Lexer(src);
-*/
         postLex(lexer);
 
         preParse(lexer);
 
+        StopWatch watch;
+        watch.start;
         auto parser = new Parser;
         auto decls = cast(Decl[])parser.parse(lexer, new AstAction);
+        timings ~= Measurement("Lex + Parse", watch.stop);
 
+        StopWatch watch2;
+        watch.start;
+        watch2.start;
         (new SymbolTableBuilder).visit(decls);
+        auto symbol_table = watch2.stop;
+        watch2.start;
         (new Declarations).visit(decls);
+        auto declarations = watch2.stop;
+        watch2.start;
         (new ImplicitCast).visit(decls);
+        auto implicit_casts = watch2.stop;
+        watch2.start;
 
-        foreach(decl ; decls)
+        foreach (decl; decls)
             decl.simplify();
+        auto simplify = watch2.stop;
+        auto extra_stuff = watch.stop;
+        timings ~= Measurement("Extra stuff", watch.stop);
+        timings ~= Measurement("  - Building scopes", symbol_table);
+        timings ~= Measurement("  - Extracting declarations", declarations);
+        timings ~= Measurement("  - Making casts explicit", implicit_casts);
 
         postParse(decls, src);
     }
-
-/*    if (args.length > 1 && args[1] == "lex")
-    {
-        Token t;
+    timings ~= Measurement("Total", total.stop);
 
-        t = lexer.next();
-        while(t.type != Tok.EOF)
-        {
-            Stdout(src.get(t.position, t.length)).newline;
-            t = lexer.next();
-        }
-    }
-    else
-    {
-        auto decl = parser.parse(lexer);
-        if(args.length > 1 && args[1] == "dump-ast")
-        {
-            auto buffer = new AstBuffer(src.data);
-            decl.print(buffer);
-        }
-    }*/
+    if (options.flag("time"))
+        foreach (m; timings)
+            Stderr.formatln("{,-45} {}ms", m.label, m.time*1e3);
 }
+
--- a/gen/CodeGen.d	Fri May 02 21:21:18 2008 +0200
+++ b/gen/CodeGen.d	Sun May 04 12:58:02 2008 +0200
@@ -136,7 +136,7 @@
 
         table.leaveScope;
 
-        m.verify();
+        //m.verify();
 
         if(optimize)
             m.optimize(inline);
--- a/sema/ImplicitCast.d	Fri May 02 21:21:18 2008 +0200
+++ b/sema/ImplicitCast.d	Sun May 04 12:58:02 2008 +0200
@@ -81,8 +81,6 @@
 
         if(identifierType != expType)
         {
-            Stdout(&identifierType)(identifierType).newline;
-            Stdout(&expType)(expType).newline;
             if(!expType.hasImplicitConversionTo(identifierType))
                 throw error(__LINE__, "Cannot make implicit cast between");