Mercurial > projects > dang
annotate dang/compiler.d @ 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 | 381975d76baf |
children | eb5b2c719a39 |
rev | line source |
---|---|
1 | 1 module dang.compiler; |
2 | |
3 import tango.io.Stdout, | |
4 tango.core.Signal, | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
5 tango.time.StopWatch, |
1 | 6 tango.io.FilePath; |
7 | |
8 import lexer.Lexer, | |
9 parser.Parser; | |
10 | |
11 import misc.DataSource; | |
12 | |
13 import ast.Decl; | |
14 | |
15 import tools.AstPrinter, | |
16 tools.DotPrinter; | |
17 | |
51
c96cdcbdb9d6
Rearranged some stuff, and renamed LLVMGen -> CodeGen
Anders Halager <halager@gmail.com>
parents:
50
diff
changeset
|
18 import gen.CodeGen; |
1 | 19 |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
20 import sema.Visitor, |
51
c96cdcbdb9d6
Rearranged some stuff, and renamed LLVMGen -> CodeGen
Anders Halager <halager@gmail.com>
parents:
50
diff
changeset
|
21 sema.AstAction, |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
22 sema.SymbolTableBuilder, |
68
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
60
diff
changeset
|
23 sema.ImplicitCast, |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
24 sema.Declarations; |
1 | 25 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
26 import Opt = dang.OptParse; |
1 | 27 |
28 void checkFiles(char[][] *files) | |
29 { | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
30 bool non_existant_files = false; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
31 bool duplicate_files = false; |
1 | 32 |
33 char[][] validFiles; | |
34 | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
35 foreach (file; *files) |
1 | 36 { |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
37 scope path = new FilePath(file); |
1 | 38 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
39 if (!path.exists) |
1 | 40 { |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
41 Stderr.formatln("'{}' does not exist", file).newline; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
42 non_existant_files = true; |
1 | 43 |
44 continue; | |
45 } | |
46 | |
47 bool fileInStack = false; | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
48 foreach (vFile; validFiles) |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
49 if (vFile == file) |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
50 { |
1 | 51 fileInStack = true; |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
52 duplicate_files = true; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
53 } |
1 | 54 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
55 if (fileInStack) |
1 | 56 continue; |
57 | |
58 validFiles ~= file; | |
59 } | |
60 | |
61 *files = validFiles; | |
62 | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
63 if (non_existant_files) |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
64 throw new Exception("All files given must exist"); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
65 if (duplicate_files) |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
66 Stderr("warning: duplicate files ignored").newline; |
1 | 67 } |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
68 |
1 | 69 void main(char[][] args) |
70 { | |
71 char[][] filesToHandle; | |
72 | |
73 Signal!(char[][]*) preStart; | |
74 | |
75 Signal!(char[]) preLex; | |
76 Signal!(Lexer) postLex; | |
77 | |
78 Signal!(Lexer) preParse; | |
79 Signal!(Decl[], DataSource) postParse; | |
80 | |
81 preStart.attach(&checkFiles); | |
82 | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
83 auto argParse = new Opt.OptionParser(`Dang "D" compiler v0.0`); |
1 | 84 |
40
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
85 bool optimize = false; |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
86 bool inline = false; |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
87 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
88 argParse.addOption(["-h", "--help"], Opt.Action.Help) |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
89 .help("Show this help message"); |
1 | 90 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
91 argParse.addOption(["--ast-dump-dot"], |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
92 "what-to-do", Opt.Action.StoreConst, "dot") |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
93 .help("Output the AST in the dot format"); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
94 argParse.addOption(["--ast-dump-code"], |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
95 "what-to-do", Opt.Action.StoreConst, "code") |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
96 .help("Output the AST as code"); |
1 | 97 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
98 argParse.addOption(["--gen-llvm"], |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
99 "what-to-do", Opt.Action.StoreConst, "gen-llvm") |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
100 .help("Compile to LLVM code (default)"); |
40
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
101 |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
102 argParse.addOption( |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
103 ["-O","--optimize"], { |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
104 optimize = true; |
1 | 105 } |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
106 ).help("Tell LLVM to do its standard optimizations"); |
40
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
107 |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
108 argParse.addOption( |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
109 ["--inline"], { |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
110 inline = true; |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
111 } |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
112 ).help("Tell LLVM that its allowed to inline functions"); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
113 |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
114 argParse |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
115 .addOption(["--time"], Opt.Action.SetTrue, "time") |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
116 .help("Time the various operations performed."); |
1 | 117 |
118 auto options = argParse.parse(args); | |
119 | |
120 filesToHandle ~= options.args; | |
121 | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
122 // Will throw exception if some files don't exist |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
123 preStart(&filesToHandle); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
124 |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
125 struct Measurement { char[] label; double time; } |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
126 Measurement[] timings; |
1 | 127 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
128 auto what = options["what-to-do"]; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
129 if (what == "" || what == "gen-llvm") |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
130 postParse.attach( |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
131 (Decl[] decls, DataSource src) { |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
132 StopWatch w; w.start; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
133 auto llvmGen = new CodeGen(); |
40
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
134 llvmGen.gen(decls, optimize, inline); |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
135 timings ~= Measurement("Generating LLVM bytecode", w.stop); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
136 }); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
137 else if (what == "dot") |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
138 postParse.attach( |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
139 (Decl[] decls, DataSource src) { |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
140 StopWatch w; w.start; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
141 auto print = new DotPrinter(); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
142 print.print(decls); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
143 timings ~= Measurement("Generating dot output", w.stop); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
144 }); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
145 else if (what == "code") |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
146 postParse.attach( |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
147 (Decl[] decls, DataSource src) { |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
148 StopWatch w; w.start; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
149 auto print = new AstPrinter(src); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
150 print.print(decls); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
151 timings ~= Measurement("Converting AST to text", w.stop); |
40
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
152 }); |
9fb190ad81a4
Added -O and --inline args to Dang.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
153 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
154 StopWatch total; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
155 total.start; |
1 | 156 foreach(file ; filesToHandle) |
157 { | |
158 preLex(file); | |
159 | |
160 auto src = DataSource(file); | |
161 auto lexer = new Lexer(src); | |
162 postLex(lexer); | |
163 | |
164 preParse(lexer); | |
165 | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
166 StopWatch watch; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
167 watch.start; |
1 | 168 auto parser = new Parser; |
51
c96cdcbdb9d6
Rearranged some stuff, and renamed LLVMGen -> CodeGen
Anders Halager <halager@gmail.com>
parents:
50
diff
changeset
|
169 auto decls = cast(Decl[])parser.parse(lexer, new AstAction); |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
170 timings ~= Measurement("Lex + Parse", watch.stop); |
1 | 171 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
172 StopWatch watch2; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
173 watch.start; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
174 watch2.start; |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
175 (new SymbolTableBuilder).visit(decls); |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
176 auto symbol_table = watch2.stop; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
177 watch2.start; |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
178 (new Declarations).visit(decls); |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
179 auto declarations = watch2.stop; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
180 watch2.start; |
68
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
60
diff
changeset
|
181 (new ImplicitCast).visit(decls); |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
182 auto implicit_casts = watch2.stop; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
183 watch2.start; |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
184 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
185 foreach (decl; decls) |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
51
diff
changeset
|
186 decl.simplify(); |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
187 auto simplify = watch2.stop; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
188 auto extra_stuff = watch.stop; |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
189 timings ~= Measurement("Extra stuff", watch.stop); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
190 timings ~= Measurement(" - Building scopes", symbol_table); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
191 timings ~= Measurement(" - Extracting declarations", declarations); |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
192 timings ~= Measurement(" - Making casts explicit", implicit_casts); |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
51
diff
changeset
|
193 |
1 | 194 postParse(decls, src); |
195 } | |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
196 timings ~= Measurement("Total", total.stop); |
1 | 197 |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
198 if (options.flag("time")) |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
199 foreach (m; timings) |
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
200 Stderr.formatln("{,-45} {}ms", m.label, m.time*1e3); |
1 | 201 } |
87
9a35a973175a
Some improvements to the compiler program
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
202 |