Base class for just-in-time and ahead-of-time compilers, which use the Mosa.Compiler.Framework framework.
Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AotMethodCompiler" /> class.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        /// <param name="method">The method.</param>
        /// <param name="basicBlocks">The basic blocks.</param>
        /// <param name="instructionSet">The instruction set.</param>
        public AotMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBlocks basicBlocks, InstructionSet instructionSet)
            : base(compiler, method, basicBlocks, instructionSet)
        {
            var compilerOptions = compiler.CompilerOptions;

            Pipeline.Add(new IMethodCompilerStage[] {
                new CILDecodingStage(),
                new BasicBlockBuilderStage(),
                new StackSetupStage(),
                new ExceptionPrologueStage(),
                new OperandAssignmentStage(),
                new StaticAllocationResolutionStage(),
                new CILTransformationStage(),
                new ConvertCompoundMoveStage(),
                (compilerOptions.EnableSSA) ? new PromoteLocalVariablesStage() : null,
                (compilerOptions.EnableSSA) ? new EdgeSplitStage() : null,
                (compilerOptions.EnableSSA) ? new PhiPlacementStage() : null,
                (compilerOptions.EnableSSA) ? new EnterSSAStage() : null,
                (compilerOptions.EnableSSA && compilerOptions.EnableSSAOptimizations) ? new SSAOptimizations() : null,
                (compilerOptions.EnableSSA) ? new LeaveSSA() : null,
                (compilerOptions.EnableSSA) ? new ConvertCompoundMoveStage() : null,
                new PlatformStubStage(),
                new	PlatformEdgeSplitStage(),
                new GreedyRegisterAllocatorStage(),
                new StackLayoutStage(),
                new EmptyBlockRemovalStage(),
                new BlockOrderingStage(),
                new CodeGenerationStage(),
            });
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AotMethodCompiler"/> class.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        /// <param name="method">The method.</param>
        public AotMethodCompiler(BaseCompiler compiler, RuntimeMethod method)
            : base(compiler, method, null)
        {
            var compilerOptions = compiler.CompilerOptions;

            Pipeline.AddRange(new IMethodCompilerStage[] {
                new CILDecodingStage(),
                new BasicBlockBuilderStage(),
                new ExceptionPrologueStage(),
                new OperandAssignmentStage(),
                new StaticAllocationResolutionStage(),
                new CILTransformationStage(),

                new IRCheckStage(),

                (compilerOptions.EnableSSA && compilerOptions.EnableSSAOptimizations) ? new LocalVariablePromotionStage() : null,
                (compilerOptions.EnableSSA) ? new EdgeSplitStage() : null,
                (compilerOptions.EnableSSA) ? new DominanceCalculationStage() : null,
                (compilerOptions.EnableSSA) ? new PhiPlacementStage() : null,
                (compilerOptions.EnableSSA) ? new EnterSSAStage() : null,
                (compilerOptions.EnableSSA && compilerOptions.EnableSSAOptimizations) ? new SSAOptimizations() : null,
                (compilerOptions.EnableSSA) ? new LeaveSSA() : null,

                new StackLayoutStage(),
                new PlatformIntrinsicTransformationStage(),
                new PlatformStubStage(),
                new LoopAwareBlockOrderStage(),
                //new SimpleTraceBlockOrderStage(),
                //new ReverseBlockOrderStage(),
                //new LocalCSE(),
                new CodeGenerationStage(),
                //new RegisterUsageAnalyzerStage(),
            });
        }
 public void Setup(BaseCompiler compiler)
 {
     this.compiler = compiler;
     architecture = compiler.Architecture;
     typeSystem = compiler.TypeSystem;
     typeLayout = compiler.TypeLayout;
 }
Example #4
0
        public CompilerData(BaseCompiler compiler)
        {
            if (compiler == null)
                throw new ArgumentNullException("compiler");

            Compiler = compiler;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseMethodCompiler" /> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler.</param>
        /// <param name="method">The method to compile by this instance.</param>
        /// <param name="basicBlocks">The basic blocks.</param>
        /// <param name="threadID">The thread identifier.</param>
        protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBlocks basicBlocks, int threadID)
        {
            Compiler          = compiler;
            Method            = method;
            Type              = method.DeclaringType;
            Scheduler         = compiler.CompilationScheduler;
            Architecture      = compiler.Architecture;
            TypeSystem        = compiler.TypeSystem;
            TypeLayout        = compiler.TypeLayout;
            Trace             = compiler.CompilerTrace;
            Linker            = compiler.Linker;
            BasicBlocks       = basicBlocks ?? new BasicBlocks();
            Pipeline          = new CompilerPipeline();
            StackLayout       = new StackLayout(Architecture, method.Signature.Parameters.Count + (method.HasThis || method.HasExplicitThis ? 1 : 0));
            VirtualRegisters  = new VirtualRegisters(Architecture);
            LocalVariables    = emptyOperandList;
            ThreadID          = threadID;
            DominanceAnalysis = new Dominance(Compiler.CompilerOptions.DominanceAnalysisFactory, BasicBlocks);
            PluggedMethod     = compiler.PlugSystem.GetPlugMethod(Method);
            stop              = false;

            MethodData = compiler.CompilerData.GetCompilerMethodData(Method);
            MethodData.Counters.Clear();

            EvaluateParameterOperands();
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseMethodCompiler" /> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler.</param>
        /// <param name="method">The method to compile by this instance.</param>
        /// <param name="basicBlocks">The basic blocks.</param>
        /// <param name="threadID">The thread identifier.</param>
        protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBlocks basicBlocks, int threadID)
        {
            this.Compiler          = compiler;
            this.Method            = method;
            this.Type              = method.DeclaringType;
            this.Scheduler         = compiler.CompilationScheduler;
            this.Architecture      = compiler.Architecture;
            this.TypeSystem        = compiler.TypeSystem;
            this.TypeLayout        = Compiler.TypeLayout;
            this.Trace             = Compiler.CompilerTrace;
            this.Linker            = compiler.Linker;
            this.BasicBlocks       = basicBlocks ?? new BasicBlocks();
            this.Pipeline          = new CompilerPipeline();
            this.StackLayout       = new StackLayout(Architecture, method.Signature.Parameters.Count + (method.HasThis || method.HasExplicitThis ? 1 : 0));
            this.VirtualRegisters  = new VirtualRegisters(Architecture);
            this.LocalVariables    = emptyOperandList;
            this.ThreadID          = threadID;
            this.DominanceAnalysis = new Dominance(Compiler.CompilerOptions.DominanceAnalysisFactory, this.BasicBlocks);

            EvaluateParameterOperands();

            this.stop = false;

            Debug.Assert(this.Linker != null);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseMethodCompiler" /> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler.</param>
        /// <param name="method">The method to compile by this instance.</param>
        /// <param name="basicBlocks">The basic blocks.</param>
        /// <param name="threadID">The thread identifier.</param>
        protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBlocks basicBlocks, int threadID)
        {
            Compiler     = compiler;
            Method       = method;
            Type         = method.DeclaringType;
            Scheduler    = compiler.CompilationScheduler;
            Architecture = compiler.Architecture;
            TypeSystem   = compiler.TypeSystem;
            TypeLayout   = compiler.TypeLayout;
            Trace        = compiler.CompilerTrace;
            Linker       = compiler.Linker;
            BasicBlocks  = basicBlocks ?? new BasicBlocks();
            Pipeline     = new CompilerPipeline();
            LocalStack   = new List <Operand>();

            ConstantZero     = Operand.CreateConstant(0, TypeSystem);
            StackFrame       = Operand.CreateCPURegister(TypeSystem.BuiltIn.Pointer, Architecture.StackFrameRegister);
            StackPointer     = Operand.CreateCPURegister(TypeSystem.BuiltIn.Pointer, Architecture.StackPointerRegister);
            Parameters       = new Operand[method.Signature.Parameters.Count + (method.HasThis || method.HasExplicitThis ? 1 : 0)];
            VirtualRegisters = new VirtualRegisters();
            LocalVariables   = emptyOperandList;
            ThreadID         = threadID;
            PluggedMethod    = compiler.PlugSystem.GetPlugMethod(Method);
            stop             = false;

            MethodData = compiler.CompilerData.GetCompilerMethodData(Method);
            MethodData.Counters.Clear();

            EvaluateParameterOperands();

            CalculateMethodParameterSize();
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseMethodCompiler" /> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler.</param>
        /// <param name="method">The method to compile by this instance.</param>
        /// <param name="basicBlocks">The basic blocks.</param>
        /// <param name="instructionSet">The instruction set.</param>
        protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBlocks basicBlocks, InstructionSet instructionSet)
        {
            this.Compiler = compiler;
            this.Method = method;
            this.Type = method.DeclaringType;
            this.Scheduler = compiler.CompilationScheduler;
            this.Architecture = compiler.Architecture;
            this.TypeSystem = compiler.TypeSystem;
            this.TypeLayout = Compiler.TypeLayout;
            this.InternalTrace = Compiler.InternalTrace;
            this.Linker = compiler.Linker;
            this.BasicBlocks = basicBlocks ?? new BasicBlocks();
            this.InstructionSet = instructionSet ?? new InstructionSet(256);
            this.Pipeline = new CompilerPipeline();
            this.StackLayout = new StackLayout(Architecture, method.Signature.Parameters.Count + (method.HasThis || method.HasExplicitThis ? 1 : 0));
            this.VirtualRegisters = new VirtualRegisters(Architecture);
            this.LocalVariables = emptyOperandList;
            this.DominanceAnalysis = new DominanceAnalysis(Compiler.CompilerOptions.DominanceAnalysisFactory, this.BasicBlocks);

            EvaluateParameterOperands();

            this.stop = false;

            Debug.Assert(this.Linker != null);
        }
        /// <summary>
        /// Link time code generator used to compile dynamically created methods during link time.
        /// </summary>
        /// <param name="compiler">The assembly compiler used to compile this method.</param>
        /// <param name="methodName">The name of the created method.</param>
        /// <param name="instructionSet">The instruction set.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="compiler"/>, <paramref name="methodName"/> or <paramref name="instructionSet"/> is null.</exception>
        /// <exception cref="System.ArgumentException"><paramref name="methodName"/> is invalid.</exception>
        public static LinkerGeneratedMethod Compile(BaseCompiler compiler, string methodName, InstructionSet instructionSet, ITypeSystem typeSystem)
        {
            if (compiler == null)
                throw new ArgumentNullException(@"compiler");
            if (methodName == null)
                throw new ArgumentNullException(@"methodName");
            if (methodName.Length == 0)
                throw new ArgumentException(@"Invalid method name.");

            LinkerGeneratedType compilerGeneratedType = typeSystem.InternalTypeModule.GetType(@"Mosa.Tools.Compiler", @"LinkerGenerated") as LinkerGeneratedType;

            // Create the type if we need to.
            if (compilerGeneratedType == null)
            {
                compilerGeneratedType = new LinkerGeneratedType(typeSystem.InternalTypeModule, @"Mosa.Tools.Compiler", @"LinkerGenerated", null);
                typeSystem.AddInternalType(compilerGeneratedType);

                //compiler.Scheduler.TrackTypeAllocated(compilerGeneratedType);
            }

            MethodSignature signature = new MethodSignature(BuiltInSigType.Void, new SigType[0]);

            // Create the method
            // HACK: <$> prevents the method from being called from CIL
            LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeSystem.InternalTypeModule, "<$>" + methodName, compilerGeneratedType, signature);
            compilerGeneratedType.AddMethod(method);

            //compiler.Scheduler.TrackMethodInvoked(method);

            LinkerMethodCompiler methodCompiler = new LinkerMethodCompiler(compiler, method, instructionSet);
            methodCompiler.Compile();

            return method;
        }
Example #10
0
 public void Setup(BaseCompiler compiler)
 {
     this.OutputFile = compiler.CompilerOptions.OutputFile;
     this.FileAlignment = compiler.CompilerOptions.Elf32.FileAlignment;
     this.IsLittleEndian = compiler.Architecture.IsLittleEndian;
     this.Machine = (MachineType)compiler.Architecture.ElfMachineType;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseMethodCompiler" /> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler.</param>
        /// <param name="method">The method to compile by this instance.</param>
        /// <param name="basicBlocks">The basic blocks.</param>
        /// <param name="threadID">The thread identifier.</param>
        protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBlocks basicBlocks, int threadID)
        {
            Compiler = compiler;
            Method = method;
            Type = method.DeclaringType;
            Scheduler = compiler.CompilationScheduler;
            Architecture = compiler.Architecture;
            TypeSystem = compiler.TypeSystem;
            TypeLayout = compiler.TypeLayout;
            Trace = compiler.CompilerTrace;
            Linker = compiler.Linker;
            BasicBlocks = basicBlocks ?? new BasicBlocks();
            Pipeline = new CompilerPipeline();
            StackLayout = new StackLayout(Architecture, method.Signature.Parameters.Count + (method.HasThis || method.HasExplicitThis ? 1 : 0));
            VirtualRegisters = new VirtualRegisters(Architecture);
            LocalVariables = emptyOperandList;
            ThreadID = threadID;
            DominanceAnalysis = new Dominance(Compiler.CompilerOptions.DominanceAnalysisFactory, BasicBlocks);
            PluggedMethod = compiler.PlugSystem.GetPlugMethod(Method);
            stop = false;

            MethodData = compiler.CompilerData.GetCompilerMethodData(Method);
            MethodData.Counters.Clear();

            EvaluateParameterOperands();
        }
        void ICompilerStage.Initialize(BaseCompiler compiler)
        {
            Debug.Assert(compiler != null);

            Compiler = compiler;

            Setup();
        }
Example #13
0
 public void Execute(int threads)
 {
     Initialize();
     PreCompile();
     ScheduleAll();
     BaseCompiler.ExecuteThreadedCompile(threads);
     PostCompile();
 }
Example #14
0
        public void Initialize()
        {
            Linker = new BaseLinker(CompilerOptions.BaseAddress, CompilerOptions.Architecture.Endianness, CompilerOptions.Architecture.MachineType, CompilerOptions.EmitSymbols, CompilerOptions.LinkerFormatType);

            BaseCompiler = CompilerFactory();

            BaseCompiler.Initialize(this);
        }
Example #15
0
        public CompilerData(BaseCompiler compiler)
        {
            if (compiler == null)
            {
                throw new ArgumentNullException("compiler");
            }

            Compiler = compiler;
        }
Example #16
0
        public void Initialize()
        {
            Linker = CompilerOptions.LinkerFactory();
            Linker.Initialize(CompilerOptions.BaseAddress, CompilerOptions.Architecture.Endianness, CompilerOptions.Architecture.ElfMachineType);

            BaseCompiler = CompilerFactory();

            BaseCompiler.Initialize(this);
        }
        public void Setup(BaseCompiler compiler)
        {
            this.OutputFile = compiler.CompilerOptions.OutputFile;
            this.IsLittleEndian = compiler.Architecture.IsLittleEndian;

            if (compiler.CompilerOptions.PortableExecutable.FileAlignment.HasValue)
                this.FileAlignment = compiler.CompilerOptions.PortableExecutable.FileAlignment.Value;

            if (compiler.CompilerOptions.PortableExecutable.SectionAlignment.HasValue)
                this.SectionAlignment = compiler.CompilerOptions.PortableExecutable.SectionAlignment.Value;

            if (compiler.CompilerOptions.PortableExecutable.SetChecksum.HasValue)
                this.SetChecksum = compiler.CompilerOptions.PortableExecutable.SetChecksum.Value;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkerMethodCompiler"/> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler executing this method compiler.</param>
        /// <param name="method">The metadata of the method to compile.</param>
        /// <param name="instructionSet">The instruction set.</param>
        public LinkerMethodCompiler(BaseCompiler compiler, RuntimeMethod method, InstructionSet instructionSet)
            : base(compiler, method, instructionSet)
        {
            BasicBlocks.CreateBlock(BasicBlock.PrologueLabel, 0);
            BasicBlocks.AddHeaderBlock(BasicBlocks.PrologueBlock);

            this.Pipeline.AddRange(new IMethodCompilerStage[] {
                new LoopAwareBlockOrderStage(),
                new PlatformStubStage(),
                new CodeGenerationStage(),
            });

            compiler.Architecture.ExtendMethodCompilerPipeline(this.Pipeline);
        }
Example #19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AotMethodCompiler" /> class.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        /// <param name="method">The method.</param>
        /// <param name="basicBlocks">The basic blocks.</param>
        /// <param name="threadID">The thread identifier.</param>
        public AotMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBlocks basicBlocks, int threadID)
            : base(compiler, method, basicBlocks, threadID)
        {
            var compilerOptions = compiler.CompilerOptions;

            // Populate the pipeline
            Pipeline.Add(new IMethodCompilerStage[] {
                new CILDecodingStage(),
                new ExceptionPrologueStage(),
                new OperandAssignmentStage(),
                new StackSetupStage(),
                new CILProtectedRegionStage(),
                new ExceptionStage(),
                new StaticAllocationResolutionStage(),
                new CILTransformationStage(),

                //new ConvertCompoundStage(),
                new UnboxValueTypeStage(),
                (compilerOptions.EnableInlinedMethods) ? new InlineStage() : null,
                (compilerOptions.EnableSSA) ? new EdgeSplitStage() : null,
                (compilerOptions.EnableSSA) ? new PhiPlacementStage() : null,
                (compilerOptions.EnableSSA) ? new EnterSSAStage() : null,
                (compilerOptions.EnableOptimizations && compilerOptions.EnableSparseConditionalConstantPropagation && compilerOptions.EnableSSA) ? new SparseConditionalConstantPropagationStage() : null,
                (compilerOptions.EnableOptimizations) ? new IROptimizationStage() : null,

                (compilerOptions.TwoPassOptimizationStages && compilerOptions.EnableOptimizations && compilerOptions.EnableSparseConditionalConstantPropagation && compilerOptions.EnableSSA) ? new SparseConditionalConstantPropagationStage() : null,
                (compilerOptions.TwoPassOptimizationStages && compilerOptions.EnableOptimizations && compilerOptions.EnableSparseConditionalConstantPropagation && compilerOptions.EnableSSA) ? new IROptimizationStage() : null,

                (compilerOptions.EnableSSA) ? new LeaveSSA() : null,
                new IRCleanupStage(),
                (compilerOptions.EnableInlinedMethods) ? new InlineEvaluationStage() : null,
                new PlatformStubStage(),
                new PlatformEdgeSplitStage(),
                new VirtualRegisterRenameStage(),
                new GreedyRegisterAllocatorStage(),
                new StackLayoutStage(),
                new EmptyBlockRemovalStage(),
                new BlockOrderingStage(),
                new CodeGenerationStage(),
                new ProtectedRegionLayoutStage(),
            });
        }
Example #20
0
 public void Compile()
 {
     BaseCompiler.ExecuteCompile();
 }
Example #21
0
        void ICompilerStage.Initialize(BaseCompiler compiler)
        {
            this.Compiler = compiler;

            Setup();
        }
Example #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DelegatePatcher"/> class.
 /// </summary>
 /// <param name="compiler">The compiler.</param>
 public DelegatePatcher(BaseCompiler compiler)
 {
     Compiler = compiler;
 }
 /// <summary>
 /// Link time code generator used to compile dynamically created methods during link time.
 /// </summary>
 /// <param name="compiler">The assembly compiler used to compile this method.</param>
 /// <param name="methodName">The name of the created method.</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException"><paramref name="compiler"/> or <paramref name="methodName"/>  is null.</exception>
 /// <exception cref="System.ArgumentException"><paramref name="methodName"/> is invalid.</exception>
 public static LinkerGeneratedMethod Compile(BaseCompiler compiler, string methodName, ITypeSystem typeSystem)
 {
     return Compile(compiler, methodName, null, typeSystem);
 }
        void ICompilerStage.Initialize(BaseCompiler compiler)
        {
            Debug.Assert(compiler != null);

            this.Compiler = compiler;

            Setup();
        }
        void ICompilerStage.Setup(BaseCompiler compiler)
        {
            base.Setup(compiler);
            linker = RetrieveLinkerFromCompiler();

            if (compiler.CompilerOptions.Multiboot.VideoDepth.HasValue)
                this.VideoDepth = compiler.CompilerOptions.Multiboot.VideoDepth.Value;
            if (compiler.CompilerOptions.Multiboot.VideoHeight.HasValue)
                this.VideoHeight = compiler.CompilerOptions.Multiboot.VideoHeight.Value;
            if (compiler.CompilerOptions.Multiboot.VideoMode.HasValue)
                this.VideoMode = compiler.CompilerOptions.Multiboot.VideoMode.Value;
            if (compiler.CompilerOptions.Multiboot.VideoWidth.HasValue)
                this.VideoWidth = compiler.CompilerOptions.Multiboot.VideoWidth.Value;
        }
 void ICompilerStage.Setup(BaseCompiler compiler)
 {
     base.Setup(compiler);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseMethodCompiler"/> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler.</param>
        /// <param name="method">The method to compile by this instance.</param>
        /// <param name="instructionSet">The instruction set.</param>
        protected BaseMethodCompiler(BaseCompiler compiler, RuntimeMethod method, InstructionSet instructionSet)
        {
            this.compiler = compiler;
            this.method = method;
            this.type = method.DeclaringType;
            this.compilationScheduler = compiler.Scheduler;
            this.moduleTypeSystem = method.Module;
            this.architecture = compiler.Architecture;
            this.typeSystem = compiler.TypeSystem;
            this.typeLayout = Compiler.TypeLayout;
            this.internalTrace = Compiler.InternalTrace;
            this.linker = compiler.Linker;

            this.basicBlocks = new BasicBlocks();

            this.instructionSet = instructionSet ?? new InstructionSet(256);

            this.pipeline = new CompilerPipeline();

            this.stackLayout = new StackLayout(architecture, method.Parameters.Count + (method.Signature.HasThis || method.Signature.HasExplicitThis ? 1 : 0));

            this.virtualRegisterLayout = new VirtualRegisterLayout(architecture, stackLayout);

            EvaluateParameterOperands();

            this.stopMethodCompiler = false;
        }
Example #28
0
 public void PreCompile()
 {
     BaseCompiler.PreCompile();
 }
        void ICompilerStage.Setup(BaseCompiler compiler)
        {
            base.Setup(compiler);

            this.MethodPipelineExportDirectory = compiler.CompilerOptions.MethodPipelineExportDirectory;
        }
 void ICompilerStage.Setup(BaseCompiler compiler)
 {
     base.Setup(compiler);
     linker = RetrieveLinkerFromCompiler();
 }
Example #31
0
 public void PostCompile()
 {
     BaseCompiler.PostCompile();
 }