17 using System.Diagnostics;
18 using System.Collections.Generic;
23 using ErrorManagement;
24 using Inference.src.Core;
27 using Semantic.SSAAlgorithm;
29 using TargetPlatforms;
31 namespace Inference.Core
40 private List<SourceFile> astList;
46 private static List<entryPointInfo> entryPointList =
new List<entryPointInfo>();
48 #region struct entryPointInfo
50 private struct entryPointInfo
59 get {
return this.location; }
72 public entryPointInfo(
Location location)
74 this.location = location;
84 #region SetEntryPointFound()
94 entryPointList.Add(
new entryPointInfo(location));
106 this.astList =
new List<SourceFile>();
121 if (Directory.Exists(f.FullName))
123 string[] files = Directory.GetFileSystemEntries(f.FullName);
124 for (
int i = 0; i < files.Length; i++)
126 LoadFile(
new FileInfo(files[i]), directories);
127 directories[files[i]] = f.FullName;
130 if (f.Extension.Equals(
".stadyn")||f.Extension.Equals(
".cs")) {
131 parseFile(f,
new FileStream(f.FullName, FileMode.Open, FileAccess.Read));
132 directories[f.Name] = f.Directory.FullName;
150 TokenStreamHiddenTokenFilter filter;
159 antlrLexer.Selector = selector;
160 antlrLexer.setFilename(f.Name);
163 preproLexer.Selector = selector;
165 hooverLexer.Selector = selector;
169 antlrLexer.setTabSize(1);
171 preproLexer.setTabSize(1);
173 hooverLexer.setTabSize(1);
176 selector.addInputStream(antlrLexer,
"codeLexer");
177 selector.addInputStream(preproLexer,
"directivesLexer");
178 selector.addInputStream(hooverLexer,
"hooverLexer");
179 selector.select(
"codeLexer");
183 filter =
new TokenStreamHiddenTokenFilter(lexer);
184 filter.hide(CSharpTokenTypes.WHITESPACE);
185 filter.hide(CSharpTokenTypes.NEWLINE);
186 filter.hide(CSharpTokenTypes.ML_COMMENT);
187 filter.hide(CSharpTokenTypes.SL_COMMENT);
193 parser.setFilename(f.FullName);
197 long startTime = DateTime.Now.Ticks;
199 this.astList.Add(parser.compilationUnit());
202 double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0;
203 ConsoleColor previousColor = Console.ForegroundColor;
204 Console.ForegroundColor = ConsoleColor.DarkGray;
205 System.Console.Out.WriteLine(
"Parsed {0} in: {1} seconds.", f.Name, elapsedTime);
206 Console.ForegroundColor = previousColor;
211 ErrorManager.Instance.NotifyError(
new ParserError(
new Location(e.fileName, e.line, e.column), e.Message));
229 public void Run(IDictionary<string, string> directories,
string outputFileName,
230 string debugFilePath,
string ilasmFileName,
string typeTableFileName,
TargetPlatform targetPlatform,
bool run,
bool serverOptimization)
232 int previousNumberOfErrors = ErrorManager.Instance.ErrorCount;
234 if (entryPointList.Count == 0)
238 if (entryPointList.Count != 1)
240 for (
int i = 0; i < entryPointList.Count; i++)
247 for (
int i = 0; i < this.astList.Count; i++)
249 this.astList[i].Accept(
new VisitorSSA(), null);
252 for (
int i = 0; i < this.astList.Count; i++)
257 for (
int i = 0; i < this.astList.Count; i++)
262 for (
int i = 0; i < this.astList.Count; i++)
268 for (
int i = 0; i < this.astList.Count; i++)
270 this.astList[i].Accept(visitorTypeInference, null);
277 outputFileName != null)
279 if (entryPointList.Count == 1) {
281 string ilFileName = Path.ChangeExtension(outputFileName,
".il");
282 VisitorCodeGenerationBase visitorCodeGeneration = createVisitorCodeGeneration(ilFileName, outputFileName, targetPlatform);
283 for (
int i = 0; i < this.astList.Count; i++)
285 this.astList[i].Accept(visitorCodeGeneration, null);
287 visitorCodeGeneration.AddExceptionCode();
288 visitorCodeGeneration.Close();
290 if (serverOptimization)
292 ConsoleColor previousColor = Console.ForegroundColor;
293 Console.ForegroundColor = ConsoleColor.DarkGray;
294 System.Console.Out.WriteLine(
"Server option founded, optimizing code...");
295 ServerOptimizer.OptimizeIlFile(ilFileName);
296 System.Console.Out.WriteLine(
"Code optimized.");
297 Console.ForegroundColor = previousColor;
302 switch (targetPlatform)
304 case TargetPlatform.CLR:
305 assembleAndRun(ilFileName, outputFileName, ilasmFileName, run);
307 case TargetPlatform.RRotor:
311 System.Diagnostics.Debug.Assert(
false,
"Unknown target platform.");
329 #region ClearMemory()
337 TypeSystem.TypeTable.Instance.Clear();
339 Program.entryPointList.Clear();
344 #region createVisitorCodeGeneration()
355 case TargetPlatform.CLR:
356 return new VisitorCLRCodeGeneration<CLRCodeGenerator>(Path.GetFileNameWithoutExtension(outputFileName),
363 System.Diagnostics.Debug.Assert(
false,
"Wrong target platform");
370 #region assembleAndRun()
371 private void assembleAndRun(
string ilFileName,
string outputFileName,
string ilasmFileName,
bool run)
380 Process process =
new Process();
381 process.StartInfo.UseShellExecute =
false;
382 process.StartInfo.CreateNoWindow =
true;
383 process.StartInfo.RedirectStandardOutput =
false;
384 process.StartInfo.RedirectStandardError =
false;
386 process.StartInfo.FileName = ilasmFileName;
387 process.StartInfo.Arguments =
"\"" + ilFileName +
"\"" +
" /output=" +
"\"" + outputFileName +
"\" /optimize";
389 process.WaitForExit();
391 if (process.ExitCode != 0)
393 ErrorManager.Instance.NotifyError(
new AssemblerError(ilFileName));
394 process.StartInfo.RedirectStandardOutput =
false;
395 process.StartInfo.RedirectStandardError =
false;
397 process.WaitForExit();
402 process.StartInfo.UseShellExecute =
false;
403 process.StartInfo.RedirectStandardOutput =
false;
404 process.StartInfo.RedirectStandardError =
false;
405 process.StartInfo.FileName = outputFileName;
407 process.WaitForExit();
408 if (process.ExitCode != 0)
409 ErrorManager.Instance.NotifyError(
new ExecutionError(outputFileName));
415 private void debug(
string debugFilePath,
string typeTableFileName) {
423 for (
int i = 0; i < this.astList.Count; i++)
425 string debugFile = Path.Combine(debugFilePath, Path.ChangeExtension(this.astList[i].Location.FileName,
".out.txt"));
426 using (StreamWriter sw =
new StreamWriter(debugFile.ToString()))
434 using (StreamWriter sw2 =
new StreamWriter(typeTableFileName))
436 sw2.Write(TypeSystem.TypeTable.Instance.ToString());
Represents a error produced when source code has not any entry point.
This class visits the AST to store the type of each declaration. Includes methods, fields and inheritance.
static void SetEntryPointFound(Location location)
Sets the information of entry point.
Class to allow the management of all different error types happened.
Represents the error occurred when compiled application has been executed.
This class encapsulates a location in a specific file. Implements an Inmutable pattern. So it can be used in any context, that is his internal fields never change.
This class walks the AST to to obtain intermediate code Thisis a layer adapter class and is the base ...
Program()
Constructor of Program
static void ClearMemory()
Frees information in memory
System.IO.FileInfo FileInfo
This class encapsulates the IL CLR code generator IL.
antlr.TokenStream TokenStream
static ErrorManager Instance
Gets the unique instance of ErrorManager
Represents the error occurred while the source code is parsing.
void Run(IDictionary< string, string > directories, string outputFileName, string debugFilePath, string ilasmFileName, string typeTableFileName, TargetPlatform targetPlatform, bool run, bool serverOptimization)
Runs the application A filename : directoryname mapThe output file name. A null value means no execut...
antlr.RecognitionException RecognitionException
Represents a error produced when source code has more the one entry point defined.
This class shows the information of the abstract syntax tree.
This class visits the AST to make the type inference of all expressiones in the source code...
This class makes the static single assignment algorithm in which every variable is assigned exactly o...
antlr.TokenStreamSelector TokenStreamSelector
string LoadFile(FileInfo f, IDictionary< string, string > directories)
Starts the parsing process
Represents the error occurred when the assembler is not capable of assembling an IL file...
This class visits the AST to store the type of each defined class or inteface.
This class visits the AST to assign symbol information to identifier expression.
int ErrorCount
Returns the number of errors