Ejemplo n.º 1
0
        private CompilerResult ProcessQueue(int threadID = 0)
        {
            CompilerResult method = default;

            try
            {
                method = MethodScheduler.GetMethodToCompile();

                if (method == null)
                {
                    return(null);
                }

                method.CompiledMethod = CompileMethod(method.Method, threadID);
                method.Result         = "OK";
                return(method);
            }
            catch (Exception e)
            {
                method.Result = e.Message;
                MethodScheduler.AddToQueue(method);
            }

            return(method);
        }
Ejemplo n.º 2
0
        public Compiler(MosaCompiler mosaCompiler)
        {
            TypeSystem      = mosaCompiler.TypeSystem;
            TypeLayout      = mosaCompiler.TypeLayout;
            CompilerOptions = mosaCompiler.CompilerOptions;
            Linker          = mosaCompiler.Linker;
            CompilerTrace   = mosaCompiler.CompilerTrace;
            Architecture    = CompilerOptions.Architecture;

            PostCompilerTraceEvent(CompilerEvent.CompilerStart);

            CompilerExtensions.AddRange(mosaCompiler.CompilerExtensions);

            MethodStagePipelines = new Pipeline <BaseMethodCompilerStage> [mosaCompiler.MaxThreads + 1];

            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 attributes = (IntrinsicMethodAttribute[])method.GetCustomAttributes(typeof(IntrinsicMethodAttribute), true);

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

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

            PlugSystem = new PlugSystem(TypeSystem);

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

            // Build the default compiler pipeline
            CompilerPipeline.Add(GetDefaultCompilerPipeline(CompilerOptions));

            foreach (var extension in CompilerExtensions)
            {
                extension.ExtendCompilerPipeline(CompilerPipeline);
            }

            Architecture.ExtendCompilerPipeline(CompilerPipeline, CompilerOptions);

            IsStopped = false;
            WorkCount = 0;
        }
Ejemplo n.º 3
0
        public void CompileMethod(MosaMethod method)
        {
            if (!MethodScheduler.IsCompilable(method))
            {
                return;
            }

            Debug.Assert(!method.HasOpenGenericParams);

            CompileMethod(method, 0);
        }
Ejemplo n.º 4
0
        private MosaMethod ProcessQueue(int threadID = 0)
        {
            var method = MethodScheduler.GetMethodToCompile();

            if (method == null)
            {
                return(null);
            }

            return(CompileMethod(method, threadID));
        }
Ejemplo n.º 5
0
        private MosaMethod ProcessQueue(int threadID = 0)
        {
            try
            {
                var methodData = MethodScheduler.GetMethodToCompile();

                if (methodData == null)
                {
                    return(null);
                }

                CompileMethod(methodData.Method, threadID);

                return(methodData.Method);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Ejemplo n.º 6
0
        private MosaMethod ProcessQueue(int threadID = 0)
        {
            try
            {
                WorkIncrement();

                var method = MethodScheduler.GetMethodToCompile();

                if (method == null)
                {
                    return(null);
                }

                return(CompileMethod(method, threadID));
            }
            finally
            {
                WorkDecrement();
            }
        }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
0
        private void ExecuteThreadedCompilePass(int maxThreads)
        {
            //maxThreads = 512;

            int threadLaunched = 0;

            var threadIDs = new Stack <int>();

            for (var i = 1; i <= maxThreads; i++)
            {
                threadIDs.Push(i);
            }

            while (true)
            {
                int launched;

                lock (threadIDs)
                {
                    launched = threadLaunched;
                }

                // are available threads?
                if (launched < maxThreads)
                {
                    // Yes - is there a method to compile
                    var method = MethodScheduler.GetMethodToCompile();

                    if (method != null)
                    {
                        int threadID;

                        lock (threadIDs)
                        {
                            threadID = threadIDs.Pop();
                            threadLaunched++;
                        }

                        ThreadPool.QueueUserWorkItem((state) =>
                        {
                            CompileMethod(method, threadID);

                            // lets check for another one
                            while (true)
                            {
                                var method2 = MethodScheduler.GetMethodToCompile();

                                if (method2 == null)
                                {
                                    break;
                                }

                                CompileMethod(method2, threadID);
                            }

                            lock (threadIDs)
                            {
                                threadIDs.Push(threadID);
                                threadLaunched--;
                            }
                        });
                    }
                    else
                    {
                        if (launched == 0)
                        {
                            return;                             // all done
                        }
                    }
                }
                else
                {
                    Thread.Yield();
                }
            }
        }