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.Debug.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.Plugs"); 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(); }
public bool Execute() { try { LogMessage("Executing IL2CPU on assembly"); if (!Initialize()) { return(false); } LogTime("Engine execute started"); AssemblyLoadContext.Default.Resolving += Default_Resolving; // Gen2 // Find the kernel's entry point. We are looking for a public class Kernel, with public static void Boot() MethodBase xKernelCtor = null; if (UseGen3Kernel) { LoadBootEntries(); } else { xKernelCtor = LoadAssemblies(); if (xKernelCtor == 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 = GetAppAssembler()) { using (var xDebugInfo = new DebugInfo(xOutputFilename + ".cdb", true, false)) { xAsm.DebugInfo = xDebugInfo; xAsm.DebugEnabled = DebugEnabled; xAsm.StackCorruptionDetection = StackCorruptionDetectionEnabled; xAsm.StackCorruptionDetectionLevel = mStackCorruptionDetectionLevel; 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.LogWarning = LogWarning; CompilerHelpers.DebugEvent += LogMessage; 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!"); } } if (UseGen3Kernel) { xScanner.Execute(mBootEntries.Keys.ToArray(), mForceIncludes); } else { xScanner.QueueMethod(xKernelCtor.DeclaringType.BaseType.GetMethod(UseGen3Kernel ? "EntryPoint" : "Start")); xScanner.Execute(xKernelCtor); } //AppAssemblerRingsCheck.Execute(xScanner, xKernelCtor.DeclaringType.Assembly); using (var xOut = new StreamWriter(File.Create(OutputFilename), 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 AssemblyLoadContext.Default.GetLoadedAssemblies()) { if (xAsm.IsDynamic) { continue; } try { LogMessage(xAsm.Location); } catch { } } return(false); } }
/// <summary> /// This method validates rings. Assemblies are specific for a given ring and are able to be dependent on assemblies in /// the same ring or one ring "up" (ie, User can reference System, etc), but not other way around. /// </summary> /// <param name="scanner"></param> /// <param name="entryAssembly"></param> public static void Execute(ILScanner scanner, Assembly entryAssembly) { if (entryAssembly == null) { throw new ArgumentNullException(nameof(entryAssembly)); } RingsWriteLine("Start check"); // verify the entry assembly is in the User ring. var xRing = GetRingFromAssembly(entryAssembly); if (xRing != Ring.User) { throw new Exception($"Assembly '{entryAssembly.GetName().Name}' contains your kernel class, which means it should be in the ring {Ring.User}!"); } foreach (var xAssembly in scanner.mUsedAssemblies) { if (IsAssemblySkippedDuringRingCheck(xAssembly)) { continue; } RingsWriteLine("Assembly '{0}'", xAssembly.GetName().Name); xRing = GetRingFromAssembly(xAssembly); var xRingInt = (int)xRing; RingsWriteLine("\t\tRing = {0}", xRing); foreach (var xAsmDepRef in xAssembly.GetReferencedAssemblies()) { var xAsmDep = scanner.mUsedAssemblies.FirstOrDefault(i => i.GetName().Name == xAsmDepRef.Name); if (xAsmDep == null || IsAssemblySkippedDuringRingCheck(xAsmDep)) { continue; } RingsWriteLine("\tDependency '{0}'", xAsmDepRef.Name); var xDepRing = GetRingFromAssembly(xAsmDep); RingsWriteLine("\t\tRing = {0}", xDepRing); var xDepRingInt = (int)xDepRing; if (xDepRingInt == xRingInt) { // assembly and its dependency are in the same ring. continue; } if (xDepRingInt > xRingInt) { throw new Exception($"Assembly '{xAssembly.GetName().Name}' is in ring {xRing}({xRingInt}). It references assembly '{xAsmDepRef.Name}' which is in ring {xDepRing}({xDepRingInt}), but this is not allowed!"); } var xRingDiff = xRingInt - xDepRingInt; if (xRingDiff == 1) { // 1 level up is allowed continue; } throw new Exception($"Assembly '{xAssembly.GetName().Name}' is in ring {xRing}({xRingInt}). It references assembly '{xAsmDepRef.Name}' which is in ring {xDepRing}({xDepRingInt}), but this is not allowed!"); } // now do per-ring checks: switch (xRing) { case Ring.User: ValidateUserAssembly(xAssembly); break; case Ring.Core: ValidateCoreAssembly(xAssembly); break; case Ring.HAL: ValidateHALAssembly(xAssembly); break; case Ring.System: ValidateSystemAssembly(xAssembly); break; default: throw new NotImplementedException($"Ring {xRing} not implemented"); } } }
/// <summary> /// This method validates rings. Assemblies are specific for a given ring and are able to be dependent on assemblies in /// the same ring or one ring "up" (ie, User can reference System, etc), but not other way around. /// </summary> /// <param name="scanner"></param> public static void Execute(ILScanner scanner) { RingsWriteLine("Start check"); foreach (var xAssembly in scanner.mUsedAssemblies) { if (IsAssemblySkippedDuringRingCheck(xAssembly)) { continue; } RingsWriteLine("Assembly '{0}'", xAssembly.GetName().Name); var xRing = GetRingFromAssembly(xAssembly); var xRingInt = (int)xRing; RingsWriteLine("\t\tRing = {0}", xRing); foreach (var xAsmDepRef in xAssembly.GetReferencedAssemblies()) { var xAsmDep = scanner.mUsedAssemblies.FirstOrDefault(i => i.GetName().Name == xAsmDepRef.Name); if (xAsmDep == null || IsAssemblySkippedDuringRingCheck(xAsmDep)) { continue; } RingsWriteLine("\tDependency '{0}'", xAsmDepRef.Name); var xDepRing = GetRingFromAssembly(xAsmDep); RingsWriteLine("\t\tRing = {0}", xDepRing); var xDepRingInt = (int)xDepRing; if (xDepRingInt == xRingInt) { // assembly and its dependency are in the same ring. continue; } if (xDepRingInt > xRingInt) { throw new Exception(string.Format("Assembly '{0}' is in ring {1}({2}). It references assembly '{3}' which is in ring {4}({5}), but this is not allowed!", xAssembly.GetName().Name, xRing, xRingInt, xAsmDepRef.Name, xDepRing, xDepRingInt)); } var xRingDiff = xRingInt - xDepRingInt; if (xRingDiff == 1) { // 1 level up is allowed continue; } throw new Exception(string.Format("Assembly '{0}' is in ring {1}({2}). It references assembly '{3}' which is in ring {4}({5}), but this is not allowed!", xAssembly.GetName().Name, xRing, xRingInt, xAsmDepRef.Name, xDepRing, xDepRingInt)); } // now do per-ring checks: switch (xRing) { case Ring.User: ValidateUserAssembly(xAssembly); break; case Ring.Core: ValidateCoreAssembly(xAssembly); break; case Ring.HAL: ValidateHALAssembly(xAssembly); break; case Ring.System: ValidateSystemAssembly(xAssembly); break; default: throw new NotImplementedException(String.Format("Ring {0} not implemented", xRing)); } } }
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 = GetAppAssembler()) { using (var xDebugInfo = new DebugInfo(xOutputFilename + ".cdb", true, false)) { xAsm.DebugInfo = xDebugInfo; xAsm.DebugEnabled = DebugEnabled; xAsm.StackCorruptionDetection = StackCorruptionDetectionEnabled; xAsm.StackCorruptionDetectionLevel = mStackCorruptionDetectionLevel; 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.LogWarning = LogWarning; CompilerHelpers.DebugEvent += LogMessage; 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, xInitMethod.DeclaringType.Assembly); 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) if (xAsm.IsDynamic) { continue; } try { LogMessage(xAsm.Location); } catch { } } return false; } }
public bool Execute() { try { LogMessage("Executing IL2CPU on assembly"); LogTime("Engine execute started"); // Gen2 // Find the kernel's entry point. We are looking for a public class Kernel, with public static void Boot() MethodBase xKernelCtor = null; if (UseGen3Kernel) { LoadBootEntries(); } else { xKernelCtor = LoadAssemblies(); if (xKernelCtor == null) { return false; } } var debugCom = mSettings.DebugCom; if (!mSettings.EnableDebug) { // Default of 1 is in Cosmos.Targets. Need to change to use proj props. debugCom = 0; } using (var xAsm = GetAppAssembler(debugCom)) { var xOutputFilenameWithoutExtension = Path.ChangeExtension(mSettings.OutputFilename, null); using (var xDebugInfo = new DebugInfo(xOutputFilenameWithoutExtension + ".cdb", true, false)) { xAsm.DebugInfo = xDebugInfo; xAsm.DebugEnabled = mSettings.EnableDebug; xAsm.StackCorruptionDetection = mSettings.EnableStackCorruptionDetection; xAsm.StackCorruptionDetectionLevel = mSettings.StackCorruptionDetectionLevel; xAsm.DebugMode = mSettings.DebugMode; xAsm.TraceAssemblies = mSettings.TraceAssemblies; xAsm.IgnoreDebugStubAttribute = mSettings.IgnoreDebugStubAttribute; if (!mSettings.EnableDebug) { xAsm.ShouldOptimize = true; } bool VBEMultiboot = false; string VBEResolution; if(mSettings.CompileVBEMultiboot) { VBEMultiboot = true; } else { VBEMultiboot = false; } if(String.IsNullOrEmpty(mSettings.VBEResolution)) { VBEResolution = "800x600x32"; } else { VBEResolution=mSettings.VBEResolution; } xAsm.Assembler.Initialize(VBEMultiboot, VBEResolution); if (mSettings.DebugMode != DebugMode.IL) { xAsm.Assembler.EmitAsmLabels = false; } using (var xScanner = new ILScanner(xAsm, new TypeResolver(_assemblyLoadContext), LogException, LogWarning)) { CompilerHelpers.DebugEvent += LogMessage; if (mSettings.EnableLogging) { var xLogFile = xOutputFilenameWithoutExtension + ".log.html"; if (!xScanner.EnableLogging(xLogFile)) { // file creation not possible LogWarning("Could not create the file \"" + xLogFile + "\"! No log will be created!"); } } var plugsAssemblies = mSettings.PlugsReferences.Select( r => _assemblyLoadContext.LoadFromAssemblyPath(r)); if (UseGen3Kernel) { xScanner.Execute(mBootEntries.Keys.ToArray(), mForceIncludes, plugsAssemblies); } else { xScanner.QueueMethod(xKernelCtor.DeclaringType.BaseType.GetMethod(UseGen3Kernel ? "EntryPoint" : "Start")); xScanner.Execute(xKernelCtor, plugsAssemblies); } //AppAssemblerRingsCheck.Execute(xScanner, xKernelCtor.DeclaringType.Assembly); using (var xOut = new StreamWriter(File.Create(mSettings.OutputFilename), 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); return false; } }