@@ -103,16 +103,21 @@ public void Compile(string moduleName)
103
103
// O0
104
104
if ( Options . Optimization >= OptimizationLevel . O0 )
105
105
{
106
+ // Function passes.
106
107
LLVM . AddPromoteMemoryToRegisterPass ( mFunctionPassManager ) ;
107
108
LLVM . AddConstantPropagationPass ( mFunctionPassManager ) ;
108
109
LLVM . AddReassociatePass ( mFunctionPassManager ) ;
109
110
LLVM . AddInstructionCombiningPass ( mFunctionPassManager ) ;
110
- LLVM . AddMemCpyOptPass ( mFunctionPassManager ) ;
111
+
112
+ // Module passes.
113
+ LLVM . AddStripDeadPrototypesPass ( mPassManager ) ;
114
+ LLVM . AddStripSymbolsPass ( mPassManager ) ;
111
115
}
112
116
113
117
// O1
114
118
if ( Options . Optimization >= OptimizationLevel . O1 )
115
119
{
120
+ // Function passes.
116
121
LLVM . AddLowerExpectIntrinsicPass ( mFunctionPassManager ) ;
117
122
LLVM . AddEarlyCSEPass ( mFunctionPassManager ) ;
118
123
LLVM . AddLoopRotatePass ( mFunctionPassManager ) ;
@@ -124,25 +129,9 @@ public void Compile(string moduleName)
124
129
LLVM . AddDeadStoreEliminationPass ( mFunctionPassManager ) ;
125
130
LLVM . AddJumpThreadingPass ( mFunctionPassManager ) ;
126
131
LLVM . AddCFGSimplificationPass ( mFunctionPassManager ) ;
127
- }
128
-
129
- // O2
130
- if ( Options . Optimization >= OptimizationLevel . O2 )
131
- {
132
- LLVM . AddLoopVectorizePass ( mFunctionPassManager ) ;
133
- LLVM . AddSLPVectorizePass ( mFunctionPassManager ) ;
134
- }
135
-
136
- // O0
137
- if ( Options . Optimization >= OptimizationLevel . O0 )
138
- {
139
- LLVM . AddStripDeadPrototypesPass ( mPassManager ) ;
140
- LLVM . AddStripSymbolsPass ( mPassManager ) ;
141
- }
132
+ LLVM . AddMemCpyOptPass ( mFunctionPassManager ) ;
142
133
143
- // O1
144
- if ( Options . Optimization >= OptimizationLevel . O1 )
145
- {
134
+ // Module passes.
146
135
LLVM . AddAlwaysInlinerPass ( mPassManager ) ;
147
136
LLVM . AddDeadArgEliminationPass ( mPassManager ) ;
148
137
LLVM . AddAggressiveDCEPass ( mFunctionPassManager ) ;
@@ -151,11 +140,16 @@ public void Compile(string moduleName)
151
140
// O2
152
141
if ( Options . Optimization >= OptimizationLevel . O2 )
153
142
{
143
+ // Function passes.
144
+ LLVM . AddLoopVectorizePass ( mFunctionPassManager ) ;
145
+ LLVM . AddSLPVectorizePass ( mFunctionPassManager ) ;
146
+
147
+ // Module passes.
154
148
LLVM . AddFunctionInliningPass ( mPassManager ) ;
155
149
LLVM . AddConstantMergePass ( mPassManager ) ;
156
150
LLVM . AddArgumentPromotionPass ( mPassManager ) ;
157
151
}
158
-
152
+
159
153
// Initialize types and runtime.
160
154
string dataLayout = LLVM . GetDataLayout ( Module ) ;
161
155
TargetData = LLVM . CreateTargetData ( dataLayout ) ;
@@ -173,7 +167,9 @@ public void Compile(string moduleName)
173
167
Console . ForegroundColor = ConsoleColor . Gray ;
174
168
175
169
// Debug: print LLVM assembly code.
176
- //Console.WriteLine(LLVM.PrintModuleToString(mModule));
170
+ #if DEBUG
171
+ Console . WriteLine ( LLVM . PrintModuleToString ( mModule ) ) ;
172
+ #endif
177
173
178
174
// Verify and throw exception on error.
179
175
Console . ForegroundColor = ConsoleColor . DarkGray ;
@@ -197,7 +193,7 @@ public void Compile(string moduleName)
197
193
TargetMachineRef machine = LLVM . CreateTargetMachine ( target , triplet , "generic" , "" , CodeGenOptLevel . CodeGenLevelDefault , RelocMode . RelocDefault , CodeModel . CodeModelDefault ) ;
198
194
LLVM . SetModuleDataLayout ( mModule , LLVM . CreateTargetDataLayout ( machine ) ) ;
199
195
CodeGenFileType type = ( Options . OutputAssembly ) ? CodeGenFileType . AssemblyFile : CodeGenFileType . ObjectFile ;
200
-
196
+
201
197
if ( LLVM . TargetMachineEmitToFile ( machine , mModule , Options . OutputFile , type , out error ) )
202
198
{
203
199
throw new InvalidOperationException ( error ) ;
@@ -284,6 +280,8 @@ private int sortTypes(TypeDefinition left, TypeDefinition right)
284
280
private void compileModule ( ModuleDefinition moduleDef )
285
281
{
286
282
List < MethodDefinition > methods = new List < MethodDefinition > ( ) ;
283
+ List < MethodDefinition > ctors = new List < MethodDefinition > ( ) ;
284
+
287
285
Collection < TypeDefinition > types = moduleDef . Types ;
288
286
List < TypeDefinition > sortedTypes = new List < TypeDefinition > ( ) ;
289
287
@@ -300,7 +298,13 @@ private void compileModule(ModuleDefinition moduleDef)
300
298
// Compiles types and adds methods.
301
299
foreach ( TypeDefinition type in sortedTypes )
302
300
{
303
- compileType ( type , methods ) ;
301
+ compileType ( type , methods , ctors ) ;
302
+ }
303
+
304
+ // COmpile .ctors.
305
+ foreach ( MethodDefinition ctor in ctors )
306
+ {
307
+ compileMethod ( ctor ) ;
304
308
}
305
309
306
310
// Compile methods.
@@ -322,20 +326,30 @@ private void compileModule(ModuleDefinition moduleDef)
322
326
/// Compiles a type.
323
327
/// </summary>
324
328
/// <param name="type">The type definition.</param>
325
- private void compileType ( TypeDefinition type , List < MethodDefinition > methods )
329
+ /// <param name="methods">The list of methods to add ours to.</param>
330
+ /// <param name="ctors">The list of ctors to add our to.</param>
331
+ private void compileType ( TypeDefinition type , List < MethodDefinition > methods , List < MethodDefinition > ctors )
326
332
{
327
333
// Nested types.
328
334
foreach ( TypeDefinition inner in type . NestedTypes )
329
335
{
330
- compileType ( inner , methods ) ;
336
+ compileType ( inner , methods , ctors ) ;
331
337
}
332
338
333
339
mTypeCompiler . Compile ( type ) ;
334
340
335
341
// Note: First, we need all types to generate before we can generate methods,
336
342
// because methods may refer to types that are not yet generated.
337
343
if ( ! type . IsInterface )
338
- methods . AddRange ( type . Methods ) ;
344
+ {
345
+ foreach ( MethodDefinition method in type . Methods )
346
+ {
347
+ if ( method . Name == ".ctor" )
348
+ ctors . Add ( method ) ;
349
+ else
350
+ methods . Add ( method ) ;
351
+ }
352
+ }
339
353
}
340
354
341
355
/// <summary>
0 commit comments