Exemplo n.º 1
0
        public static LinkerMethodInfo GetMethodInfo(TypeSystem typeSystem, MosaLinker linker, UnitTestInfo unitTestInfo)
        {
            string fullMethodName = unitTestInfo.FullMethodName;

            int first  = fullMethodName.LastIndexOf(".");
            int second = fullMethodName.LastIndexOf(".", first - 1);

            var methodNamespaceName = fullMethodName.Substring(0, second);
            var methodTypeName      = fullMethodName.Substring(second + 1, first - second - 1);
            var methodName          = fullMethodName.Substring(first + 1);

            var method = FindMosaMethod(
                typeSystem,
                methodNamespaceName,
                methodTypeName,
                methodName,
                unitTestInfo.Values);

            var address = GetMethodAddress(method, linker);

            var methodInfo = new LinkerMethodInfo
            {
                MethodNamespaceName = methodNamespaceName,
                MethodTypeName      = methodTypeName,
                MethodName          = methodName,
                MosaMethod          = method,
                MosaMethodAddress   = address
            };

            return(methodInfo);
        }
Exemplo n.º 2
0
        public bool Build()
        {
            Console.WriteLine("Compile " + InputAssembly);
            Configure();

            AppLocations = new AppLocations();

            AppLocations.FindApplications();

            TestAssemblyPath = AppContext.BaseDirectory;
            Options.Paths.Add(TestAssemblyPath);
            Options.SourceFile = Path.Combine(TestAssemblyPath, InputAssembly);

            var builder = new Builder(Options, AppLocations, this);

            var start = DateTime.UtcNow;

            builder.Compile();
            Console.WriteLine((DateTime.UtcNow - start).ToString());

            Linker     = builder.Linker;
            TypeSystem = builder.TypeSystem;
            ImageFile  = Options.BootLoaderImage ?? builder.ImageFile;

            return(!builder.HasCompileError);
        }
Exemplo n.º 3
0
        public ElfLinker(MosaLinker linker, LinkerFormatType linkerFormatType)
        {
            this.linker           = linker;
            this.linkerFormatType = linkerFormatType;

            sectionHeaderStringTable.Add((byte)'\0');
            stringTable.Add((byte)'\0');

            BaseFileOffset   = 0x1000;             // required by ELF
            SectionAlignment = 0x1000;             // default 1K
        }
Exemplo n.º 4
0
        public static IntPtr GetMethodAddress(MosaMethod method, MosaLinker linker)
        {
            var symbol = linker.GetSymbol(method.FullName);

            if (symbol.VirtualAddress == 0)
            {
                Console.WriteLine(method.FullName);
            }

            return(new IntPtr((long)symbol.VirtualAddress));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of <see cref="BaseCodeEmitter" />.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="linker">The linker.</param>
        /// <param name="codeStream">The stream the machine code is written to.</param>
        public void Initialize(string methodName, MosaLinker linker, Stream codeStream)
        {
            Debug.Assert(codeStream != null);
            Debug.Assert(linker != null);

            MethodName = methodName;
            Linker     = linker;
            CodeStream = codeStream;

            Labels = new Dictionary <int, int>();

            OpcodeEncoder = new OpcodeEncoder(this);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of <see cref="CodeEmitter" />.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="linker">The linker.</param>
        /// <param name="codeStream">The stream the machine code is written to.</param>
        public CodeEmitter(string methodName, MosaLinker linker, Stream codeStream, OpcodeEncoder opcodeEncoder)
        {
            Debug.Assert(codeStream != null);
            Debug.Assert(linker != null);

            MethodName    = methodName;
            Linker        = linker;
            CodeStream    = codeStream;
            OpcodeEncoder = opcodeEncoder;

            Labels = new Dictionary <int, int>();

            opcodeEncoder.SetEmitter(this);
        }
Exemplo n.º 7
0
        public bool Compile()
        {
            LauncherOptions.Paths.Add(TestAssemblyPath);
            LauncherOptions.SourceFile = Path.Combine(TestAssemblyPath, TestSuiteFile);

            var builder = new Builder(LauncherOptions, AppLocations, this);

            builder.Compile();

            Linker     = builder.Linker;
            TypeSystem = builder.TypeSystem;
            ImageFile  = LauncherOptions.BootLoaderImage ?? builder.ImageFile;

            return(!builder.HasCompileError);
        }
Exemplo n.º 8
0
        public void Initialize()
        {
            lock (_lock)
            {
                if (Stage != CompileStage.Loaded)
                {
                    return;
                }

                Linker   = new MosaLinker(CompilerOptions.BaseAddress, CompilerOptions.Architecture.Endianness, CompilerOptions.Architecture.ElfMachineType, CompilerOptions.EmitAllSymbols, CompilerOptions.EmitStaticRelocations, CompilerOptions.LinkerFormatType, CompilerOptions.CreateExtraSections, CompilerOptions.CreateExtraProgramHeaders);
                Compiler = new Compiler(this);

                Stage = CompileStage.Initialized;
            }
        }
Exemplo n.º 9
0
        public ElfLinker(MosaLinker linker, LinkerFormatType linkerFormatType, MachineType machineType)
        {
            Linker           = linker;
            LinkerFormatType = linkerFormatType;
            MachineType      = machineType;

            sectionHeaderStringTable.Add((byte)'\0');
            stringTable.Add((byte)'\0');

            BaseFileOffset   = 0x1000;             // required by ELF
            SectionAlignment = 0x1000;             // default 1K

            // Cache for faster performance
            EmitShortSymbolName = linker.LinkerSettings.ShortSymbolNames;
        }
Exemplo n.º 10
0
        public Compiler(MosaCompiler mosaCompiler)
        {
            TypeSystem       = mosaCompiler.TypeSystem;
            TypeLayout       = mosaCompiler.TypeLayout;
            CompilerSettings = mosaCompiler.CompilerSettings;
            Architecture     = mosaCompiler.Platform;
            CompilerHooks    = mosaCompiler.CompilerHooks;
            TraceLevel       = CompilerSettings.TraceLevel;
            Statistics       = CompilerSettings.Statistics;

            Linker = new MosaLinker(this);

            ObjectHeaderSize = Architecture.NativePointerSize + 4 + 4;             // Hash Value (32-bit) + Lock & Status (32-bit) + Method Table

            StackFrame     = Operand.CreateCPURegister(TypeSystem.BuiltIn.Pointer, Architecture.StackFrameRegister);
            StackPointer   = Operand.CreateCPURegister(TypeSystem.BuiltIn.Pointer, Architecture.StackPointerRegister);
            LinkRegister   = Architecture.LinkRegister == null ? null : Operand.CreateCPURegister(TypeSystem.BuiltIn.Object, Architecture.LinkRegister);
            ProgramCounter = Architecture.ProgramCounter == null ? null : Operand.CreateCPURegister(TypeSystem.BuiltIn.Object, Architecture.ProgramCounter);

            ExceptionRegister   = Operand.CreateCPURegister(TypeSystem.BuiltIn.Object, Architecture.ExceptionRegister);
            LeaveTargetRegister = Operand.CreateCPURegister(TypeSystem.BuiltIn.Object, Architecture.LeaveTargetRegister);

            PostEvent(CompilerEvent.CompileStart);

            MethodStagePipelines = new Pipeline <BaseMethodCompilerStage> [MaxThreads];

            MethodScheduler = new MethodScheduler(this);
            MethodScanner   = new MethodScanner(this);

            foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
            {
                if (!type.IsClass)
                {
                    continue;
                }

                foreach (var method in type.GetRuntimeMethods())
                {
                    // Now get all the IntrinsicMethodAttribute attributes
                    var intrinsicMethodAttributes = (IntrinsicMethodAttribute[])method.GetCustomAttributes(typeof(IntrinsicMethodAttribute), true);

                    for (int i = 0; i < intrinsicMethodAttributes.Length; i++)
                    {
                        var d = (IntrinsicMethodDelegate)System.Delegate.CreateDelegate(typeof(IntrinsicMethodDelegate), method);

                        // Finally add the dictionary entry mapping the target name and the delegate
                        InternalIntrinsicMethods.Add(intrinsicMethodAttributes[i].Target, d);
                    }

                    // Now get all the StubMethodAttribute attributes
                    var stubMethodAttributes = (StubMethodAttribute[])method.GetCustomAttributes(typeof(StubMethodAttribute), true);

                    for (int i = 0; i < stubMethodAttributes.Length; i++)
                    {
                        var d = (StubMethodDelegate)System.Delegate.CreateDelegate(typeof(StubMethodDelegate), method);

                        // Finally add the dictionary entry mapping the target name and the delegate
                        InternalStubMethods.Add(stubMethodAttributes[i].Target, d);
                    }
                }
            }

            PlugSystem = new PlugSystem(TypeSystem);

            PlatformInternalRuntimeType = GetPlatformInternalRuntimeType();
            InternalRuntimeType         = GeInternalRuntimeType();

            // Build the default compiler pipeline
            CompilerPipeline.Add(GetDefaultCompilerPipeline(CompilerSettings, Architecture.Is64BitPlatform));

            // Call hook to allow for the extension of the pipeline
            CompilerHooks.ExtendCompilerPipeline?.Invoke(CompilerPipeline);

            Architecture.ExtendCompilerPipeline(CompilerPipeline, CompilerSettings);

            IsStopped = false;
        }
Exemplo n.º 11
0
        private static List <UnitTest> PrepareUnitTest(List <UnitTestInfo> discoveredUnitTests, TypeSystem typeSystem, MosaLinker linker)
        {
            var unitTests = new List <UnitTest>(discoveredUnitTests.Count);

            int id = 0;

            foreach (var unitTestInfo in discoveredUnitTests)
            {
                LinkerMethodInfo linkerMethodInfo;

                try
                {
                    linkerMethodInfo = Linker.GetMethodInfo(typeSystem, linker, unitTestInfo);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error while resolving method '{unitTestInfo.FullMethodName}'");

                    throw;
                }

                var unitTest = new UnitTest(unitTestInfo, linkerMethodInfo);

                unitTest.SerializedUnitTest = SerializeUnitTestMessage(unitTest);
                unitTest.UnitTestID         = id++;

                unitTests.Add(unitTest);
            }

            return(unitTests);
        }
Exemplo n.º 12
0
        private static List <UnitTest> PrepareUnitTest(List <UnitTestInfo> discoveredUnitTests, TypeSystem typeSystem, MosaLinker linker)
        {
            var unitTests = new List <UnitTest>(discoveredUnitTests.Count);

            int id = 0;

            foreach (var unitTestInfo in discoveredUnitTests)
            {
                var linkerMethodInfo = Linker.GetMethodInfo(typeSystem, linker, unitTestInfo);

                var unitTest = new UnitTest(unitTestInfo, linkerMethodInfo);

                unitTest.SerializedUnitTest = SerializeUnitTestMessage(unitTest);
                unitTest.UnitTestID         = id++;

                unitTests.Add(unitTest);
            }

            return(unitTests);
        }
Exemplo n.º 13
0
 public Starter(LauncherOptions options, AppLocations appLocations, IStarterEvent launcherEvent, MosaLinker linker)
     : base(options, appLocations)
 {
     LauncherEvent = launcherEvent;
     Linker        = linker;
 }
Exemplo n.º 14
0
 public Starter(Settings settings, CompilerHooks compilerHooks, MosaLinker linker)
     : base(settings, compilerHooks)
 {
     Linker = linker;
 }