Beispiel #1
0
        private ScriptCode /*!*/ CompileRubySource(SourceUnit /*!*/ sourceUnit, LoadFlags flags)
        {
            Assert.NotNull(sourceUnit);

            // TODO: check file timestamp
            string fullPath = Platform.GetFullPath(sourceUnit.Path);

#if FEATURE_FILESYSTEM
            CompiledFile compiledFile;
            if (TryGetCompiledFile(fullPath, out compiledFile))
            {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");

                return(compiledFile.CompiledCode);
            }

            Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");
#endif

            RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions)
            {
                FactoryKind = (flags & LoadFlags.LoadIsolated) != 0 ? TopScopeFactoryKind.WrappedFile : TopScopeFactoryKind.File
            };

            ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);

#if FEATURE_FILESYSTEM
            AddCompiledFile(fullPath, compiledCode);
#endif
            return(compiledCode);
        }
Beispiel #2
0
        private void ExecuteRubySourceUnit(SourceUnit /*!*/ sourceUnit, Scope /*!*/ globalScope, LoadFlags flags)
        {
            Assert.NotNull(sourceUnit, globalScope);

            // TODO: check file timestamp
            string       fullPath = Platform.GetFullPath(sourceUnit.Path);
            CompiledFile compiledFile;

            if (TryGetCompiledFile(fullPath, out compiledFile))
            {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");
                compiledFile.CompiledCode.Run(globalScope);
            }
            else
            {
                Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");

                RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions)
                {
                    IsIncluded = true,
                    IsWrapped  = (flags & LoadFlags.LoadIsolated) != 0,
                };

                long       ts1          = Stopwatch.GetTimestamp();
                ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);
                long       ts2          = Stopwatch.GetTimestamp();
                Interlocked.Add(ref _ScriptCodeGenerationTimeTicks, ts2 - ts1);

                AddCompiledFile(fullPath, compiledCode);

                CompileAndRun(globalScope, compiledCode, _context.Options.InterpretedMode);
            }
        }
Beispiel #3
0
 public TraceAstGenerator(Dictionary <MSA.DynamicExpression, SourceSpan> /*!*/ sites,
                          RubyContext /*!*/ context, RubyCompilerOptions /*!*/ options, SourceUnit /*!*/ sourceUnit, Encoding /*!*/ encoding)
     : base((RubyBinder)context.Binder, options, sourceUnit, encoding, false,
            context.DomainManager.Configuration.DebugMode, false, false, false)
 {
     _sites = sites;
 }
Beispiel #4
0
        private ScriptCode /*!*/ CompileRubySource(SourceUnit /*!*/ sourceUnit, LoadFlags flags)
        {
            Assert.NotNull(sourceUnit);

            // TODO: check file timestamp
            string       fullPath = Platform.GetFullPath(sourceUnit.Path);
            CompiledFile compiledFile;

            if (TryGetCompiledFile(fullPath, out compiledFile))
            {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");

                return(compiledFile.CompiledCode);
            }
            else
            {
                Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");

                RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions)
                {
                    FactoryKind = (flags & LoadFlags.LoadIsolated) != 0 ? TopScopeFactoryKind.WrappedFile : TopScopeFactoryKind.File
                };

                long       ts1          = Stopwatch.GetTimestamp();
                ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);
                long       ts2          = Stopwatch.GetTimestamp();
                Interlocked.Add(ref _ScriptCodeGenerationTimeTicks, ts2 - ts1);

                AddCompiledFile(fullPath, compiledCode);

                return(compiledCode);
            }
        }
Beispiel #5
0
        private SourceUnitTree MakeParseTree(TextContentProvider /*!*/ content, ErrorSink /*!*/ errorSink)
        {
            var source  = new SourceUnit(HostingHelpers.GetLanguageContext(_engine), content, null, SourceCodeKind.File);
            var options = new RubyCompilerOptions();
            var parser  = new Parser();

            try {
                int attempts = 10;
                while (true)
                {
                    try {
                        return(parser.Parse(source, options, errorSink));
                    } catch (IOException) {
                        // file being copied, try again...
                        if (attempts > 0)
                        {
                            Thread.Sleep(100);
                            attempts--;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            } catch (Exception e) {
                Debug.Assert(false, String.Format("Failure in IronRuby parser: {0}", e.ToString()));
                return(null);
            }
        }
Beispiel #6
0
        public static MSA.Expression <T> /*!*/ Transform <T>(SourceUnitTree /*!*/ ast, SourceUnit /*!*/ sourceUnit,
                                                             RubyCompilerOptions /*!*/ options, int sourceId)
        {
            var context   = (RubyContext)sourceUnit.LanguageContext;
            var siteNodes = new Dictionary <MSA.DynamicExpression, SourceSpan>();
            var generator = new TraceAstGenerator(siteNodes, context, options, sourceUnit, ast.Encoding);
            var lambda    = ast.Transform <T>(generator);

            return((MSA.Expression <T>) new CallSiteTraceInjector(siteNodes, sourceId).Visit(lambda));
        }
		public SourceUnitTree CreateAst(string fileName, ITextBuffer fileContent)
		{
			if (scriptEngine == null) {
				scriptEngine = Ruby.CreateEngine();
			}
			
			RubyContext rubyContext = HostingHelpers.GetLanguageContext(scriptEngine) as RubyContext;
			sourceUnit = rubyContext.CreateFileUnit(fileName, fileContent.Text);
			RubyCompilerSink sink = new RubyCompilerSink();
			RubyCompilerOptions compilerOptions = new RubyCompilerOptions((RubyOptions)rubyContext.Options);
			Parser parser = new Parser();
			return parser.Parse(sourceUnit, compilerOptions, sink);
		}
Beispiel #8
0
        public static MSA.Expression <T> /*!*/ Transform <T>(SourceUnitTree /*!*/ ast, SourceUnit /*!*/ sourceUnit,
                                                             RubyCompilerOptions /*!*/ options, int sourceId)
        {
            var siteNodes = new Dictionary <MSA.DynamicExpression, SourceSpan>();
            var context   = (RubyContext)sourceUnit.LanguageContext;

            context.CallSiteCreated = (expression, callSite) => siteNodes.Add(callSite, expression.Location);

            var generator = new AstGenerator(context, options, sourceUnit.Document, ast.Encoding, false);
            var lambda    = ast.Transform <T>(generator);

            return((MSA.Expression <T>) new CallSiteTraceInjector(siteNodes, sourceId).Visit(lambda));
        }
Beispiel #9
0
 internal AstGenerator(RubyBinder /*!*/ binder, RubyCompilerOptions /*!*/ options, SourceUnit /*!*/ sourceUnit, Encoding /*!*/ encoding,
                       bool debugCompiler, bool debugMode, bool traceEnabled, bool profilerEnabled, bool savingToDisk)
 {
     Assert.NotNull(binder, options, encoding);
     _binder          = binder;
     _compilerOptions = options;
     _debugCompiler   = debugCompiler;
     _debugMode       = debugMode;
     _traceEnabled    = traceEnabled;
     _sourceUnit      = sourceUnit;
     _document        = sourceUnit.Document;
     _encoding        = encoding;
     _profiler        = profilerEnabled ? Profiler.Instance : null;
     _savingToDisk    = savingToDisk;
 }
Beispiel #10
0
        internal AstGenerator(RubyCompilerOptions/*!*/ options, SourceUnit/*!*/ sourceUnit, RubyEncoding/*!*/ encoding,
            bool debugCompiler, bool debugMode, bool traceEnabled, bool profilerEnabled, bool savingToDisk) {

            Assert.NotNull(options, encoding, sourceUnit);
            _context = (RubyContext)sourceUnit.LanguageContext;
            _compilerOptions = options;
            _debugCompiler = debugCompiler;
            _debugMode = debugMode;
            _traceEnabled = traceEnabled;
            _sourceUnit = sourceUnit;
            _document = sourceUnit.Document;
            _encoding = encoding;
            _profiler = profilerEnabled ? Profiler.Instance : null;
            _savingToDisk = savingToDisk;
        }
Beispiel #11
0
        private MSA.Expression _sourcePathConstant; // lazy

        internal AstGenerator(RubyContext/*!*/ context, RubyCompilerOptions/*!*/ options, MSA.SymbolDocumentInfo document, RubyEncoding/*!*/ encoding,
            bool printInteractiveResult) {

            Assert.NotNull(context, options, encoding);
            _context = context;
            _compilerOptions = options;
            _debugCompiler = Snippets.Shared.SaveSnippets;
            _debugMode = context.DomainManager.Configuration.DebugMode;
            _traceEnabled = context.RubyOptions.EnableTracing;
            _document = document;
            _encoding = encoding;
            _profiler = context.RubyOptions.Profile ? Profiler.Instance : null;
            _savingToDisk = context.RubyOptions.SavePath != null;
            _printInteractiveResult = printInteractiveResult;
        }
Beispiel #12
0
        private void AstLocations1()
        {
            // DumpExpression uses private reflection:
            if (_driver.PartialTrust)
            {
                return;
            }

            var sourceUnit = Context.CreateSnippet(@"
def add a,b
  a + b
end

add 1, 1
add 'foo', 'bar'
", SourceCodeKind.Expression);

            var options = new RubyCompilerOptions();
            var parser  = new Parser();
            var tokens  = new List <KeyValuePair <SourceSpan, Tokens> >();

            parser.TokenSink = (token, span) => { tokens.Add(new KeyValuePair <SourceSpan, Tokens>(span, token)); };
            var ast = parser.Parse(sourceUnit, options, Context.RuntimeErrorSink);

            const int Id = 0x12345678;

            var lambda = CallSiteTracer.Transform <DlrMainCallTarget>(ast, sourceUnit, options, Id);
            var code   = new ScriptCode(lambda, sourceUnit);

            var locations = new List <int>();

            CallSiteTracer.Register((context, args, result, id, location) => {
                locations.Add(location);
                Debug.Assert(id == Id);
                Debug.Assert(location > 0);

                //Console.WriteLine("-- {0} ---------", location);
                //Console.WriteLine(this);
                //Console.WriteLine(AstUtils.DumpExpression(result.Restrictions.ToExpression()));
                //Console.WriteLine();
                //Console.WriteLine(AstUtils.DumpExpression(result.Expression));
                //Console.WriteLine("----------------");
            });

            code.Run();

            Debug.Assert(locations.Count == 4 && locations[0] == 31 && locations[1] == 19 && locations[2] == 41 && locations[3] == 19);
        }
Beispiel #13
0
        internal AstGenerator(RubyContext /*!*/ context, RubyCompilerOptions /*!*/ options, MSA.SymbolDocumentInfo document, RubyEncoding /*!*/ encoding,
                              bool printInteractiveResult)
        {
            Assert.NotNull(context, options, encoding);
            _context                = context;
            _compilerOptions        = options;
            _debugMode              = context.DomainManager.Configuration.DebugMode;
            _traceEnabled           = context.RubyOptions.EnableTracing;
            _document               = document;
            _sequencePointClearance = (document != null) ? Ast.ClearDebugInfo(document) : null;
            _encoding               = encoding;
            _encodingConstant       = Ast.Constant(encoding);
            _profiler               = context.RubyOptions.Profile ? Profiler.Instance : null;
            _savingToDisk           = context.RubyOptions.SavePath != null;
            _printInteractiveResult = printInteractiveResult;
#if !SILVERLIGHT
            _debugCompiler = Snippets.Shared.SaveSnippets;
#endif
        }
Beispiel #14
0
        private void ParserLoggingTest()
        {
#if DEBUG
            string source     = "def foo(a); end";
            var    sourceUnit = Context.CreateSnippet(source, SourceCodeKind.Statements);
            var    options    = new RubyCompilerOptions();

            string temp = Path.Combine(Path.GetTempPath(), "RubyParser");
            Console.WriteLine("> see {0}", temp);
            Directory.CreateDirectory(temp);

            Parser parser = new Parser();
            using (TextWriter writer = File.CreateText(Path.Combine(temp, "default.log"))) {
                DefaultParserLogger.Attach(parser, writer);
                parser.Parse(sourceUnit, options, ErrorSink.Null);
            }

            using (TextWriter writer = File.CreateText(Path.Combine(temp, "tables.csv"))) {
                parser.DumpTables(writer);
            }

            using (TextWriter writer = File.CreateText(Path.Combine(temp, "productions.txt"))) {
                for (int i = 0; i < parser.RuleLhsNonTerminals.Length; i++)
                {
                    writer.WriteLine("{0}\t{1}", i, parser.RuleToString(i));
                }
            }

            parser = new Parser();
            using (TextWriter writer = File.CreateText(Path.Combine(temp, "productions.txt"))) {
                for (int i = 0; i < parser.RuleLhsNonTerminals.Length; i++)
                {
                    writer.WriteLine("{0}\t{1}", i, parser.RuleToString(i));
                }
            }

            using (TextWriter writer = File.CreateText(Path.Combine(temp, "second_order.log"))) {
                parser.EnableLogging(new CoverageParserLogger(parser, writer));
                parser.Parse(sourceUnit, options, ErrorSink.Null);
            }
#endif
        }
Beispiel #15
0
        internal AstGenerator(RubyContext/*!*/ context, RubyCompilerOptions/*!*/ options, MSA.SymbolDocumentInfo document, RubyEncoding/*!*/ encoding,
            bool printInteractiveResult) {

            Assert.NotNull(context, options, encoding);
            _context = context;
            _compilerOptions = options;
            _debugMode = context.DomainManager.Configuration.DebugMode;
            _traceEnabled = context.RubyOptions.EnableTracing;
            _document = document;
            _sequencePointClearance = (document != null) ? Ast.ClearDebugInfo(document) : null;
            _encoding = encoding;
            _encodingConstant = Ast.Constant(encoding);
            _profiler = context.RubyOptions.Profile ? Profiler.Instance : null;
            _savingToDisk = context.RubyOptions.SavePath != null;
            _printInteractiveResult = printInteractiveResult;
#if SILVERLIGHT
            _debugCompiler = false;
#else
            _debugCompiler = Snippets.Shared.SaveSnippets;
#endif
        }
Beispiel #16
0
        private void Benchmark(List <string> /*!*/ files)
        {
            var       sources   = new List <SourceUnit>();
            Stopwatch readTime  = new Stopwatch();
            long      totalSize = 0;

            readTime.Start();
            foreach (string path in files)
            {
                try {
                    byte[] data = File.ReadAllBytes(path);
                    sources.Add(_context.CreateSourceUnit(new BinaryContentProvider(data), path, Encoding.Default, SourceCodeKind.File));
                    totalSize += data.Length;
                } catch (Exception) {
                    Console.WriteLine("Error: {0}", path);
                }
            }
            readTime.Stop();

            Console.WriteLine("Read: {0} kB in {1}", totalSize / 1024, readTime.Elapsed);

#if F
            Stopwatch tokenizeTime = new Stopwatch();
            tokenizeTime.Start();
            foreach (var source in sources)
            {
                try {
                    var tokenizer = new Tokenizer();
                    tokenizer.Initialize(source);

                    Tokens token;
                    do
                    {
                        token = tokenizer.GetNextToken();
                    } while (token != Tokens.EndOfFile);
                } catch (Exception) {
                    Console.WriteLine("Tokenization error: {0}", source.Path);
                    break;
                }
            }
            tokenizeTime.Stop();
#endif
            //var stackSizes = new Dictionary<int, int>();

            var       options       = new RubyCompilerOptions();
            Stopwatch parseTime     = new Stopwatch();
            Stopwatch transformTime = new Stopwatch();
            foreach (var source in sources)
            {
                try {
                    parseTime.Start();
                    var parser = new Parser();
                    parser.Parse(source, options, ErrorSink.Null);
                    //int mt;
                    //stackSizes[parser.StackMaxTop] = stackSizes.TryGetValue(parser.StackMaxTop, out mt) ? mt + 1 : 1;
                    parseTime.Stop();
#if F
                    if (rubyTree != null)
                    {
                        transformTime.Start();
                        var lambda = _context.TransformTree <DlrMainCallTarget>(rubyTree, source, options);
                        transformTime.Stop();
                    }
                    else
                    {
                        Console.WriteLine("SyntaxError: {0}", source.Path);
                    }
#endif
                } catch (Exception e) {
                    Console.WriteLine("{0}: {1}: {2}", e.GetType().Name, source.Path, e.Message);
                    break;
                }
            }

            //  Console.WriteLine("Tokenize:        {0}", tokenizeTime.Elapsed);
            Console.WriteLine("Parse:           {0}", parseTime.Elapsed);
            //Console.WriteLine("Idf/Kwd/Loc: {0}/{1}/{2}", Tokenizer.IdfLength, Tokenizer.KwdLength, Tokenizer.LocLength);
            // Console.WriteLine("Transform:       {0}", transformTime.Elapsed);

            //PerfTrack.DumpHistogram(Parser.Reductions);
            //PerfTrack.DumpHistogram(stackSizes);
        }