Skip to content

Commit a8fc68b

Browse files
shargoncschuchardt88
authored andcommitted
Allow to choose the kind of optimization (neo-project#964)
* Avoid NoX argument * Fix * Use all * fix ut
1 parent dcf550b commit a8fc68b

File tree

7 files changed

+33
-15
lines changed

7 files changed

+33
-15
lines changed

src/Neo.Compiler.CSharp/CompilationContext.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ internal void Compile()
132132
RemoveEmptyInitialize();
133133
Instruction[] instructions = GetInstructions().ToArray();
134134
instructions.RebuildOffsets();
135-
if (!Options.NoOptimize) Optimizer.CompressJumps(instructions);
135+
if (Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Basic))
136+
{
137+
Optimizer.CompressJumps(instructions);
138+
}
136139
instructions.RebuildOperands();
137140
}
138141
}
@@ -143,7 +146,7 @@ internal void Compile()
143146
ContractManifest manifest = CreateManifest();
144147
JObject debugInfo = CreateDebugInformation(folder);
145148

146-
if (!Options.NoOptimize)
149+
if (Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Experimental))
147150
{
148151
try
149152
{

src/Neo.Compiler.CSharp/CompilationOptions.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@
1010

1111
using Microsoft.CodeAnalysis;
1212
using Microsoft.CodeAnalysis.CSharp;
13+
using System;
1314
using System.Collections.Generic;
1415

1516
namespace Neo.Compiler
1617
{
1718
public class CompilationOptions
1819
{
20+
[Flags]
21+
public enum OptimizationType : byte
22+
{
23+
None = 0,
24+
Basic = 1,
25+
Experimental = 2,
26+
27+
All = Basic | Experimental
28+
}
29+
1930
public NullableContextOptions Nullable { get; set; }
2031
public bool Debug { get; set; }
21-
public bool NoOptimize { get; set; }
32+
public OptimizationType Optimize { get; set; } = OptimizationType.Basic;
2233
public bool Checked { get; set; }
2334
public bool NoInline { get; set; }
2435
public byte AddressVersion { get; set; }

src/Neo.Compiler.CSharp/MethodConvert/MethodConvert.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ private byte AddAnonymousVariable()
103103

104104
private void RemoveAnonymousVariable(byte index)
105105
{
106-
if (!_context.Options.NoOptimize)
106+
if (_context.Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Basic))
107107
_anonymousVariables.Remove(index);
108108
}
109109

110110
private void RemoveLocalVariable(ILocalSymbol symbol)
111111
{
112-
if (!_context.Options.NoOptimize)
112+
if (_context.Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Basic))
113113
_localVariables.Remove(symbol);
114114
}
115115

@@ -226,7 +226,7 @@ public void Convert(SemanticModel model)
226226
// it comes from modifier clean up
227227
AddInstruction(OpCode.RET);
228228
}
229-
if (!_context.Options.NoOptimize)
229+
if (_context.Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Basic))
230230
Optimizer.RemoveNops(_instructions);
231231
_startTarget.Instruction = _instructions[0];
232232
}

src/Neo.Compiler.CSharp/Options.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88
// Redistribution and use in source and binary forms with or without
99
// modifications are permitted.
1010

11+
using System;
12+
1113
namespace Neo.Compiler
1214
{
1315
public class Options : CompilationOptions
1416
{
15-
public enum GenerateArtifactsKind
17+
[Flags]
18+
public enum GenerateArtifactsKind : byte
1619
{
17-
None,
18-
Source,
19-
Library,
20-
All
20+
None = 0,
21+
Source = 1,
22+
Library = 2,
23+
24+
All = Source | Library
2125
}
2226

2327
public string? Output { get; set; }

src/Neo.Compiler.CSharp/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,14 @@ private static int ProcessOutput(Options options, string folder, CompilationCont
201201
{
202202
var artifact = manifest.GetArtifactsSource(baseName, nef);
203203

204-
if (options.GenerateArtifacts == Options.GenerateArtifactsKind.All || options.GenerateArtifacts == Options.GenerateArtifactsKind.Source)
204+
if (options.GenerateArtifacts.HasFlag(Options.GenerateArtifactsKind.Source))
205205
{
206206
path = Path.Combine(outputFolder, $"{baseName}.artifacts.cs");
207207
File.WriteAllText(path, artifact);
208208
Console.WriteLine($"Created {path}");
209209
}
210210

211-
if (options.GenerateArtifacts == Options.GenerateArtifactsKind.All || options.GenerateArtifacts == Options.GenerateArtifactsKind.Library)
211+
if (options.GenerateArtifacts.HasFlag(Options.GenerateArtifactsKind.Library))
212212
{
213213
try
214214
{

tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void EnsureArtifactsUpToDate()
3333
var result = new CompilationEngine(new CompilationOptions()
3434
{
3535
Debug = true,
36-
NoOptimize = false,
36+
Optimize = CompilationOptions.OptimizationType.All,
3737
Nullable = Microsoft.CodeAnalysis.NullableContextOptions.Disable
3838
})
3939
.CompileSources(

tests/Neo.SmartContract.TestEngine/TestEngine.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public List<CompilationContext> AddEntryScripts(bool optimize = true, bool debug
7878
{
7979
AddressVersion = ProtocolSettings.Default.AddressVersion,
8080
Debug = debug,
81-
NoOptimize = !optimize
81+
Optimize = optimize ? Compiler.CompilationOptions.OptimizationType.All : Compiler.CompilationOptions.OptimizationType.None
8282
}).Compile(files, references);
8383

8484
if (contexts == null || contexts.Count == 0)

0 commit comments

Comments
 (0)