Beispiel #1
0
    private static void DoScan()
    {

        var xSW = new Stopwatch();
        xSW.Start();
        string MDFFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.mdf";
        if (File.Exists(MDFFile))
            File.Delete(MDFFile);

        var outFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.out";
        if (File.Exists(outFile))
            File.Delete(outFile);

        var logFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.log";
        if (File.Exists(logFile))
            File.Delete(logFile);

        var xAsmblr = new AppAssembler(1, "Cosmos.Assembler.Log");
        using (var xScanner = new ILScanner(xAsmblr))
        {
            xScanner.LogException = (Exception e) =>
            {
                Console.WriteLine("ILScanner exception : " + e.Message);
            };
            using (var xDebugInfo = new DebugInfo(MDFFile, true, true))
            {
                xAsmblr.DebugInfo = xDebugInfo;
                xAsmblr.DebugEnabled = true;
                xAsmblr.DebugMode = DebugMode.Source;
                xAsmblr.TraceAssemblies = TraceAssemblies.All;
                xAsmblr.IgnoreDebugStubAttribute = false;

                xAsmblr.Assembler.Initialize();
                //TODO: Add plugs into the scanning equation to profile scanning them too
                //System.Reflection.MethodInfo[] name = typeof(SSchockeTest.Kernel).GetMethods();
                Type xFoundType = typeof(FakeKernel);
                var xCtor = xFoundType.GetConstructor(Type.EmptyTypes);
                typeof(Cosmos.System.Plugs.System.ConsoleImpl).IsSubclassOf(typeof(object));
                var xEntryPoint = typeof(Kernel).GetMethod("Start", BindingFlags.Public | BindingFlags.Instance);
                //var xEntryPoint = typeof(Program).GetMethod("ScannerEntryPoint", BindingFlags.NonPublic | BindingFlags.Static);
                //EnableLogging(pathToLogFile)
                xScanner.EnableLogging(logFile);
                //xScanner.TempDebug += new Action<string>(xScanner_TempDebug);
                //xScanner.
                xScanner.QueueMethod(xEntryPoint);
                xScanner.Execute(xCtor);
                using (var xOut = new StreamWriter(outFile, false))
                {
                    //if (EmitDebugSymbols) {
                    xAsmblr.Assembler.FlushText(xOut);
                    xAsmblr.FinalizeDebugInfo();
                }
                xSW.Stop();
                Console.WriteLine("Total time : {0}", xSW.Elapsed);
                Console.WriteLine("Method count: {0}", xScanner.MethodCount);
                //Console.WriteLine("Instruction count: {0}", xScanner.InstructionCount);
            }
        }
    }
Beispiel #2
0
        public void OnValidate()
        {
            MyValue = 5;
            var scanner = new ILScanner();

            MyValueIsAccessed = ILScanner.IsFieldAccessed(GetType(), nameof(MyValue));

            FieldNameToScanIsAccessed = ILScanner.IsFieldAccessed(FieldNameOwner?.GetType(), FieldNameToScan);
        }
Beispiel #3
0
        private void PrintMethods()
        {
            if (!FieldNameOwner)
            {
                return;
            }
            var methods = ILScanner.GetMethods(FieldNameOwner);

            foreach (var m in methods)
            {
                Debug.Log(m.Name);
            }
        }
Beispiel #4
0
        public ILImporter(ILScanner compilation, MethodDesc method, MethodIL methodIL = null)
        {
            if (methodIL == null)
            {
                methodIL = compilation.GetMethodIL(method);
            }
            else
            {
                _isFallbackBodyCompilation = true;
            }

            // This is e.g. an "extern" method in C# without a DllImport or InternalCall.
            if (methodIL == null)
            {
                ThrowHelper.ThrowInvalidProgramException(ExceptionStringID.InvalidProgramSpecific, method);
            }

            _compilation = compilation;
            _factory     = (ILScanNodeFactory)compilation.NodeFactory;

            _ilBytes = methodIL.GetILBytes();

            _canonMethodIL = methodIL;

            // Get the runtime determined method IL so that this works right in shared code
            // and tokens in shared code resolve to runtime determined types.
            MethodIL uninstantiatiedMethodIL = methodIL.GetMethodILDefinition();

            if (methodIL != uninstantiatiedMethodIL)
            {
                MethodDesc sharedMethod = method.GetSharedRuntimeFormMethodTarget();
                _methodIL = new InstantiatedMethodIL(sharedMethod, uninstantiatiedMethodIL);
            }
            else
            {
                _methodIL = methodIL;
            }

            _canonMethod = method;

            var ilExceptionRegions = methodIL.GetExceptionRegions();

            _exceptionRegions = new ExceptionRegion[ilExceptionRegions.Length];
            for (int i = 0; i < ilExceptionRegions.Length; i++)
            {
                _exceptionRegions[i] = new ExceptionRegion()
                {
                    ILRegion = ilExceptionRegions[i]
                };
            }
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
            StreamWriter strw = new StreamWriter("log.txt");

            s.Start();
            System.Reflection.Assembly.Load("Cosmos.Build.Common");
            System.Reflection.Assembly.Load("Cosmos.Compiler.Assembler");
            System.Reflection.Assembly.Load("Cosmos.Compiler.Assembler.X86");
            System.Reflection.Assembly.Load("Cosmos.Core_Plugs");
            System.Reflection.Assembly.Load("Cosmos.Debug.Common");
            System.Reflection.Assembly.Load("Cosmos.Core.DebugStub");
            System.Reflection.Assembly.Load("Cosmos.Debug.Kernel");
            System.Reflection.Assembly.Load("Cosmos.Debug.Kernel.Plugs");
            System.Reflection.Assembly.Load("Cosmos.IL2CPU");
            System.Reflection.Assembly.Load("Cosmos.IL2CPU.API");
            System.Reflection.Assembly.Load("Cosmos.IL2CPU.X86");
            System.Reflection.Assembly.Load("Cosmos.System");
            System.Reflection.Assembly.Load("Cosmos.System_Plugs");
            var xInitMethod     = typeof(StructTest.Kernel).GetConstructor(Type.EmptyTypes);
            var xOutputFilename = System.Windows.Forms.Application.StartupPath + "\\" + "StructTest";
            var xAsm            = new AppAssemblerNasm(0);

            using (var xDebugInfo = new DebugInfo())
            {
                xDebugInfo.CreateCPDB(xOutputFilename + ".cpdb");
                xAsm.DebugInfo       = xDebugInfo;
                xAsm.DebugMode       = DebugMode.None;
                xAsm.TraceAssemblies = TraceAssemblies.All;
                xAsm.ShouldOptimize  = true;

                var xNasmAsm = (AssemblerNasm)xAsm.Assembler;
                xAsm.Assembler.Initialize();
                using (var xScanner = new ILScanner(xAsm))
                {
                    // TODO: shouldn't be here?
                    xScanner.QueueMethod(xInitMethod.DeclaringType.BaseType.GetMethod("Start"));
                    xScanner.Execute(xInitMethod);

                    using (var xOut = new StreamWriter(xOutputFilename + ".asm", false))
                    {
                        xAsm.Assembler.FlushText(xOut);
                    }
                }
            }
            s.Stop();
            strw.Write("Total Time to Run: " + s.ElapsedMilliseconds.ToString() + "ms");
            strw.Close();
        }
Beispiel #6
0
        public bool Execute()
        {
            try
            {
                LogMessage("Executing IL2CPU on assembly");
                if (!Initialize())
                {
                    return(false);
                }
                LogTime("Engine execute started");

                // Find the kernel's entry point. We are looking for a public class Kernel, with public static void Boot()
                var xInitMethod = LoadAssemblies();
                if (xInitMethod == null)
                {
                    return(false);
                }

                var xOutputFilename = Path.Combine(Path.GetDirectoryName(OutputFilename), Path.GetFileNameWithoutExtension(OutputFilename));
                if (!DebugEnabled)
                {
                    // Default of 1 is in Cosmos.Targets. Need to change to use proj props.
                    DebugCom = 0;
                }

                using (var xAsm = new AppAssembler(DebugCom))
                {
                    using (var xDebugInfo = new DebugInfo(xOutputFilename + ".cdb", true))
                    {
                        xAsm.DebugInfo                = xDebugInfo;
                        xAsm.DebugEnabled             = DebugEnabled;
                        xAsm.StackCorruptionDetection = StackCorruptionDetectionEnabled;
                        xAsm.DebugMode                = mDebugMode;
                        xAsm.TraceAssemblies          = mTraceAssemblies;
                        xAsm.IgnoreDebugStubAttribute = IgnoreDebugStubAttribute;
                        if (DebugEnabled == false)
                        {
                            xAsm.ShouldOptimize = true;
                        }

                        xAsm.Assembler.Initialize();
                        using (var xScanner = new ILScanner(xAsm))
                        {
                            xScanner.LogException = LogException;
                            xScanner.TempDebug   += x => LogMessage(x);
                            if (EnableLogging)
                            {
                                var xLogFile = xOutputFilename + ".log.html";
                                if (false == xScanner.EnableLogging(xLogFile))
                                {
                                    // file creation not possible
                                    EnableLogging = false;
                                    LogWarning("Could not create the file \"" + xLogFile + "\"! No log will be created!");
                                }
                            }
                            xScanner.QueueMethod(xInitMethod.DeclaringType.BaseType.GetMethod("Start"));
                            xScanner.Execute(xInitMethod);

                            AppAssemblerRingsCheck.Execute(xScanner);

                            using (var xOut = new StreamWriter(OutputFilename, false, Encoding.ASCII, 128 * 1024))
                            {
                                //if (EmitDebugSymbols) {
                                xAsm.Assembler.FlushText(xOut);
                                xAsm.FinalizeDebugInfo();
                                //// for now: write debug info to console
                                //Console.WriteLine("Wrote {0} instructions and {1} datamembers", xAsm.Assembler.Instructions.Count, xAsm.Assembler.DataMembers.Count);
                                //var dict = new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase);
                                //foreach (var instr in xAsm.Assembler.Instructions)
                                //{
                                //    var mn = instr.Mnemonic ?? "";
                                //    if (dict.ContainsKey(mn))
                                //    {
                                //        dict[mn] = dict[mn] + 1;
                                //    }
                                //    else
                                //    {
                                //        dict[mn] = 1;
                                //    }
                                //}
                                //foreach (var entry in dict)
                                //{
                                //    Console.WriteLine("{0}|{1}", entry.Key, entry.Value);
                                //}
                            }
                        }
                        // If you want to uncomment this line make sure to enable PERSISTANCE_PROFILING symbol in
                        // DebugInfo.cs file.
                        //LogMessage(string.Format("DebugInfo flatening {0} seconds, persistance : {1} seconds",
                        //    (int)xDebugInfo.FlateningDuration.TotalSeconds,
                        //    (int)xDebugInfo.PersistanceDuration.TotalSeconds));
                    }
                }
                LogTime("Engine execute finished");
                return(true);
            }
            catch (Exception ex)
            {
                LogException(ex);
                LogMessage("Loaded assemblies: ");
                foreach (var xAsm in AppDomain.CurrentDomain.GetAssemblies())
                {
                    // HACK: find another way to skip dynamic assemblies (which belong to dynamic methods)
                    try
                    {
                        LogMessage(xAsm.Location);
                    }
                    catch
                    {
                    }
                }
                return(false);
            }
        }
Beispiel #7
0
        private static void DoScan()
        {
            var xSW = new Stopwatch();

            xSW.Start();
            string MDFFile = AppContext.BaseDirectory + "TestKernel.mdf";

            if (File.Exists(MDFFile))
            {
                File.Delete(MDFFile);
            }

            var outFile = AppContext.BaseDirectory + "TestKernel.out";

            if (File.Exists(outFile))
            {
                File.Delete(outFile);
            }

            var logFile = AppContext.BaseDirectory + "TestKernel.log";

            if (File.Exists(logFile))
            {
                File.Delete(logFile);
            }

            var xAsmblr = new AppAssembler(1, "Cosmos.Assembler.Log");

            using (var xScanner = new ILScanner(xAsmblr))
            {
                xScanner.LogException = (Exception e) =>
                {
                    Console.WriteLine("ILScanner exception : " + e.Message);
                };
                using (var xDebugInfo = new DebugInfo(MDFFile, true, true))
                {
                    xAsmblr.DebugInfo                = xDebugInfo;
                    xAsmblr.DebugEnabled             = true;
                    xAsmblr.DebugMode                = DebugMode.Source;
                    xAsmblr.TraceAssemblies          = TraceAssemblies.All;
                    xAsmblr.IgnoreDebugStubAttribute = false;

                    xAsmblr.Assembler.Initialize();
                    //TODO: Add plugs into the scanning equation to profile scanning them too
                    Type xFoundType  = typeof(FakeKernel);
                    var  xCtor       = xFoundType.GetConstructor(Type.EmptyTypes);
                    var  xEntryPoint = typeof(Kernel).GetMethod("Start", BindingFlags.Public | BindingFlags.Instance);
                    xScanner.EnableLogging(logFile);
                    xScanner.QueueMethod(xEntryPoint);
                    xScanner.Execute(xCtor);
                    using (var xOut = new StreamWriter(File.OpenWrite(outFile)))
                    {
                        xAsmblr.Assembler.FlushText(xOut);
                        xAsmblr.FinalizeDebugInfo();
                    }
                    xSW.Stop();
                    Console.WriteLine("Total time : {0}", xSW.Elapsed);
                    Console.WriteLine("Method count: {0}", xScanner.MethodCount);
                    //Console.WriteLine("Instruction count: {0}", xScanner.InstructionCount);
                }
            }
        }
Beispiel #8
0
        private static void DoScan()
        {
            var xSW = new Stopwatch();

            xSW.Start();
            string MDFFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.mdf";

            if (File.Exists(MDFFile))
            {
                File.Delete(MDFFile);
            }

            var outFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.out";

            if (File.Exists(outFile))
            {
                File.Delete(outFile);
            }

            var logFile = AppDomain.CurrentDomain.BaseDirectory + "TestKernel.log";

            if (File.Exists(logFile))
            {
                File.Delete(logFile);
            }

            var xAsmblr = new AppAssembler(1, "Cosmos.Assembler.Log");

            using (var xScanner = new ILScanner(xAsmblr))
            {
                xScanner.LogException = (Exception e) =>
                {
                    Console.WriteLine("ILScanner exception : " + e.Message);
                };
                using (var xDebugInfo = new DebugInfo(MDFFile, true))
                {
                    xAsmblr.DebugInfo                = xDebugInfo;
                    xAsmblr.DebugEnabled             = true;
                    xAsmblr.DebugMode                = DebugMode.Source;
                    xAsmblr.TraceAssemblies          = TraceAssemblies.All;
                    xAsmblr.IgnoreDebugStubAttribute = false;

                    xAsmblr.Assembler.Initialize();
                    //TODO: Add plugs into the scanning equation to profile scanning them too
                    //System.Reflection.MethodInfo[] name = typeof(SSchockeTest.Kernel).GetMethods();
                    Type xFoundType = typeof(FakeKernel);
                    var  xCtor      = xFoundType.GetConstructor(Type.EmptyTypes);
                    typeof(Cosmos.System.Plugs.System.ConsoleImpl).IsSubclassOf(typeof(object));
                    var xEntryPoint = typeof(Kernel).GetMethod("Start", BindingFlags.Public | BindingFlags.Instance);
                    //var xEntryPoint = typeof(Program).GetMethod("ScannerEntryPoint", BindingFlags.NonPublic | BindingFlags.Static);
                    //EnableLogging(pathToLogFile)
                    xScanner.EnableLogging(logFile);
                    //xScanner.TempDebug += new Action<string>(xScanner_TempDebug);
                    //xScanner.
                    xScanner.QueueMethod(xEntryPoint);
                    xScanner.Execute(xCtor);
                    using (var xOut = new StreamWriter(outFile, false))
                    {
                        //if (EmitDebugSymbols) {
                        xAsmblr.Assembler.FlushText(xOut);
                        xAsmblr.FinalizeDebugInfo();
                    }
                    xSW.Stop();
                    Console.WriteLine("Total time : {0}", xSW.Elapsed);
                    Console.WriteLine("Method count: {0}", xScanner.MethodCount);
                    //Console.WriteLine("Instruction count: {0}", xScanner.InstructionCount);
                }
            }
        }