/// <summary>
        /// Initializes a new instance of the <see cref="BaseCallingConventionExtended"/>.
        /// </summary>
        /// <param name="architecture">The architecture of the calling convention.</param>
        public BaseCallingConventionExtended(BaseArchitecture architecture)
        {
            if (architecture == null)
                throw new ArgumentNullException(@"Architecture");

            this.architecture = architecture;
        }
        /// <summary>
        /// Calculates the stack size for parameters.
        /// </summary>
        /// <param name="typeLayout">The type layouts.</param>
        /// <param name="operands">The operands.</param>
        /// <param name="method">The method.</param>
        /// <returns></returns>
        protected static int CalculateStackSizeForParameters(MosaTypeLayout typeLayout, BaseArchitecture architecture, List<Operand> operands, MosaMethod method)
        {
            Debug.Assert((method.Signature.Parameters.Count + (method.HasThis ? 1 : 0) == operands.Count) ||
            (method.DeclaringType.IsDelegate && method.Signature.Parameters.Count == operands.Count), method.FullName);

            int offset = method.Signature.Parameters.Count - operands.Count;
            int result = 0;

            for (int index = operands.Count - 1; index >= 0; index--)
            {
                Operand operand = operands[index];

                int size, alignment;
                architecture.GetTypeRequirements(typeLayout, operand.Type, out size, out alignment);

                var param = (index + offset >= 0) ? method.Signature.Parameters[index + offset] : null;

                if (param != null && operand.IsR8 && param.ParameterType.IsR4)
                {
                    //  adjust for parameter size on stack when method parameter is R4 while the calling variable is R8
                    architecture.GetTypeRequirements(typeLayout, param.ParameterType, out size, out alignment);
                }

                result = (int)Alignment.AlignUp(result, (uint)alignment) + size;
            }

            return result;
        }
Esempio n. 3
0
        /// <summary>
        /// Compiles the specified type system.
        /// </summary>
        /// <param name="typeSystem">The type system.</param>
        /// <param name="typeLayout">The type layout.</param>
        /// <param name="compilerTrace">The compiler trace.</param>
        /// <param name="compilerOptions">The compiler options.</param>
        /// <param name="architecture">The architecture.</param>
        /// <param name="simAdapter">The sim adapter.</param>
        /// <param name="linker">The linker.</param>
        /// <returns></returns>
        public static SimCompiler Compile(TypeSystem typeSystem, MosaTypeLayout typeLayout, CompilerTrace compilerTrace, CompilerOptions compilerOptions, BaseArchitecture architecture, ISimAdapter simAdapter, BaseLinker linker)
        {
            var compiler = new SimCompiler(architecture, typeSystem, typeLayout, linker, compilerOptions, compilerTrace, simAdapter);

            compiler.Compile();

            return compiler;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultCallingConvention"/>.
 /// </summary>
 /// <param name="architecture">The architecture of the calling convention.</param>
 public DefaultCallingConvention(BaseArchitecture architecture)
     : base(architecture)
 {
     scratchRegister = GeneralPurposeRegister.EDX;
     return32BitRegister = GeneralPurposeRegister.EAX;
     return64BitRegister = GeneralPurposeRegister.EDX;
     returnFloatingPointRegister = SSE2Register.XMM0;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseCallingConventionExtended"/>.
        /// </summary>
        /// <param name="architecture">The architecture of the calling convention.</param>
        public BaseCallingConventionExtended(BaseArchitecture architecture)
        {
            if (architecture == null)
            {
                throw new ArgumentNullException(@"Architecture");
            }

            this.architecture = architecture;
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="DefaultCallingConvention"/>.
		/// </summary>
		/// <param name="architecture">The architecture of the calling convention.</param>
		public DefaultCallingConvention(BaseArchitecture architecture)
			: base(architecture)
		{
			// FIXME:
			scratchRegister = GeneralPurposeRegister.R12;
			return32BitRegister = GeneralPurposeRegister.R1;
			return64BitRegister = GeneralPurposeRegister.R2;
			returnFloatingPointRegister = GeneralPurposeRegister.R1;
		}
Esempio n. 7
0
        /// <summary>
        /// Compiles the specified type system.
        /// </summary>
        /// <param name="typeSystem">The type system.</param>
        /// <param name="typeLayout">The type layout.</param>
        /// <param name="internalTrace">The internal trace.</param>
        /// <param name="enabledSSA">if set to <c>true</c> [enabled ssa].</param>
        /// <param name="architecture">The architecture.</param>
        /// <param name="simAdapter">The sim adapter.</param>
        /// <param name="linker">The linker.</param>
        /// <returns></returns>
        public static SimCompiler Compile(TypeSystem typeSystem, MosaTypeLayout typeLayout, IInternalTrace internalTrace, bool enabledSSA, BaseArchitecture architecture, ISimAdapter simAdapter, ILinker linker)
        {
            CompilerOptions compilerOptions = new CompilerOptions();
            compilerOptions.EnableSSA = enabledSSA;
            compilerOptions.EnableSSAOptimizations = enabledSSA;

            SimCompiler compiler = new SimCompiler(architecture, typeSystem, typeLayout, linker, compilerOptions, internalTrace, simAdapter);

            compiler.Compile();

            return compiler;
        }
Esempio n. 8
0
        /// <summary>
        /// Prevents a default instance of the <see cref="ExplorerCompiler" /> class from being created.
        /// </summary>
        /// <param name="architecture">The compiler target architecture.</param>
        /// <param name="typeSystem">The type system.</param>
        /// <param name="typeLayout">The type layout.</param>
        /// <param name="compilerTrace">The internal trace.</param>
        /// <param name="compilerOptions">The compiler options.</param>
        public ExplorerCompiler(BaseArchitecture architecture, TypeSystem typeSystem, MosaTypeLayout typeLayout, CompilerTrace compilerTrace, CompilerOptions compilerOptions, bool emitBinary)
            : base(architecture, typeSystem, typeLayout, new CompilationScheduler(typeSystem, true), compilerTrace, new ExplorerLinker(), compilerOptions)
        {
            this.emitBinary = emitBinary;

            // Build the assembly compiler pipeline
            Pipeline.Add(new ICompilerStage[] {
                new PlugStage(),
                new MethodCompilerSchedulerStage(),
                new TypeInitializerSchedulerStage(),
                new MethodLookupTableStage(),
                new MethodExceptionLookupTableStage(),
                new MetadataStage(),
            });

            architecture.ExtendCompilerPipeline(Pipeline);
        }
Esempio n. 9
0
        /// <summary>
        /// Prevents a default instance of the <see cref="SimCompiler" /> class from being created.
        /// </summary>
        /// <param name="architecture">The compiler target architecture.</param>
        /// <param name="typeSystem">The type system.</param>
        /// <param name="typeLayout">The type layout.</param>
        /// <param name="linker">The linker.</param>
        /// <param name="compilerOptions">The compiler options.</param>
        /// <param name="internalTrace">The internal trace.</param>
        /// <param name="simAdapter">The sim adapter.</param>
        public SimCompiler(BaseArchitecture architecture, TypeSystem typeSystem, MosaTypeLayout typeLayout, ILinker linker, CompilerOptions compilerOptions, IInternalTrace internalTrace, ISimAdapter simAdapter)
            : base(architecture, typeSystem, typeLayout, new CompilationScheduler(typeSystem, true), internalTrace, linker, compilerOptions)
        {
            this.simAdapter = simAdapter;

            // Build the assembly compiler pipeline
            Pipeline.Add(new ICompilerStage[] {
                new PlugStage(),
                new MethodCompilerSchedulerStage(),
                new TypeInitializerSchedulerStage(),
                new SimPowerUpStage(),
                new TypeLayoutStage(),
                new MetadataStage(),
                new LinkerFinalizationStage(),
            });

            architecture.ExtendCompilerPipeline(Pipeline);
        }
Esempio n. 10
0
        /// <summary>
        /// Initializes a new compiler instance.
        /// </summary>
        /// <param name="architecture">The compiler target architecture.</param>
        /// <param name="typeSystem">The type system.</param>
        /// <param name="typeLayout">The type layout.</param>
        /// <param name="compilationScheduler">The compilation scheduler.</param>
        /// <param name="compilerTrace">The compiler trace.</param>
        /// <param name="linker">The linker.</param>
        /// <param name="compilerOptions">The compiler options.</param>
        /// <exception cref="System.ArgumentNullException">@Architecture</exception>
        protected BaseCompiler(BaseArchitecture architecture, TypeSystem typeSystem, MosaTypeLayout typeLayout, ICompilationScheduler compilationScheduler, CompilerTrace compilerTrace, BaseLinker linker, CompilerOptions compilerOptions)
        {
            if (architecture == null)
                throw new ArgumentNullException(@"Architecture");

            Pipeline = new CompilerPipeline();
            Architecture = architecture;
            TypeSystem = typeSystem;
            TypeLayout = typeLayout;
            CompilerTrace = compilerTrace;
            CompilerOptions = compilerOptions;
            Counters = new Counters();
            CompilationScheduler = compilationScheduler;
            PlugSystem = new PlugSystem();
            Linker = linker;

            if (Linker == null)
            {
                Linker = compilerOptions.LinkerFactory();
                Linker.Initialize(compilerOptions.BaseAddress, architecture.Endianness, architecture.ElfMachineType);
            }

            // Create new dictionary
            IntrinsicTypes = new Dictionary<string, Type>();

            // Get all the classes that implement the IIntrinsicInternalMethod interface
            IEnumerable<Type> types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => typeof(IIntrinsicInternalMethod).IsAssignableFrom(p) && p.IsClass);

            // Iterate through all the found types
            foreach (var t in types)
            {
                // Now get all the ReplacementTarget attributes
                var attributes = (ReplacementTargetAttribute[])t.GetCustomAttributes(typeof(ReplacementTargetAttribute), true);
                for (int i = 0; i < attributes.Length; i++)
                {
                    // Finally add the dictionary entry mapping the target string and the type
                    IntrinsicTypes.Add(attributes[i].Target, t);
                }
            }

            PlatformInternalRuntimeType = GetPlatformInternalRuntimeType();
        }
Esempio n. 11
0
        /// <summary>
        /// Initializes a new compiler instance.
        /// </summary>
        /// <param name="architecture">The compiler target architecture.</param>
        /// <param name="typeSystem">The type system.</param>
        /// <param name="typeLayout">The type layout.</param>
        /// <param name="compilationScheduler">The compilation scheduler.</param>
        /// <param name="internalTrace">The internal trace.</param>
        /// <param name="compilerOptions">The compiler options.</param>
        protected BaseCompiler(BaseArchitecture architecture, TypeSystem typeSystem, MosaTypeLayout typeLayout, ICompilationScheduler compilationScheduler, IInternalTrace internalTrace, ILinker linker, CompilerOptions compilerOptions)
        {
            if (architecture == null)
                throw new ArgumentNullException(@"Architecture");

            Pipeline = new CompilerPipeline();
            Architecture = architecture;
            TypeSystem = typeSystem;
            TypeLayout = typeLayout;
            InternalTrace = internalTrace;
            CompilerOptions = compilerOptions;
            Counters = new Counters();
            CompilationScheduler = compilationScheduler;
            PlugSystem = new PlugSystem();
            Linker = linker;

            if (Linker == null)
            {
                Linker = compilerOptions.LinkerFactory();
                Linker.Initialize(compilerOptions.OutputFile, architecture.Endianness, architecture.ElfMachineType);
            }
        }
Esempio n. 12
0
        public void StartSimulator(string platform)
        {
            if (TypeSystem == null)
                return;

            Status = "Compiling...";

            Architecture = GetArchitecture(platform);
            var simAdapter = GetSimAdaptor(platform);
            Linker = new SimLinker(simAdapter);

            compileStartTime = DateTime.Now;

            var compilerOptions = new CompilerOptions();

            compilerOptions.EnableSSA = true;
            compilerOptions.EnableOptimizations = true;
            compilerOptions.EnablePromoteTemporaryVariablesOptimization = true;
            compilerOptions.EnableSparseConditionalConstantPropagation = true;

            SimCompiler.Compile(TypeSystem, TypeLayout, InternalTrace, compilerOptions, Architecture, simAdapter, Linker);

            SimCPU = simAdapter.SimCPU;

            SimCPU.Monitor.BreakAtTick = 1;
            SimCPU.Monitor.BreakOnException = true;
            SimCPU.Monitor.OnStateUpdate = UpdateSimState;
            SimCPU.Reset();

            Display32 = SimCPU.GetState().NativeRegisterSize == 32;

            SimCPU.Monitor.OnExecutionStepCompleted(true);

            Status = "Compiled.";

            symbolView.CreateEntries();
        }
        /// <summary>
        /// Calculates the stack size for parameters.
        /// </summary>
        /// <param name="typeLayout">The type layouts.</param>
        /// <param name="operands">The operands.</param>
        /// <param name="method">The method.</param>
        /// <returns></returns>
        protected static int CalculateStackSizeForParameters(MosaTypeLayout typeLayout, BaseArchitecture architecture, List <Operand> operands, MosaMethod method)
        {
            Debug.Assert((method.Signature.Parameters.Count + (method.HasThis ? 1 : 0) == operands.Count) ||
                         (method.DeclaringType.IsDelegate && method.Signature.Parameters.Count == operands.Count), method.FullName);

            int offset = method.Signature.Parameters.Count - operands.Count;
            int result = 0;

            for (int index = operands.Count - 1; index >= 0; index--)
            {
                Operand operand = operands[index];

                int size, alignment;
                architecture.GetTypeRequirements(typeLayout, operand.Type, out size, out alignment);

                var param = (index + offset >= 0) ? method.Signature.Parameters[index + offset] : null;

                if (param != null && operand.IsR8 && param.ParameterType.IsR4)
                {
                    //  adjust for parameter size on stack when method parameter is R4 while the calling variable is R8
                    architecture.GetTypeRequirements(typeLayout, param.ParameterType, out size, out alignment);
                }

                result = (int)Alignment.AlignUp(result, (uint)alignment) + size;
            }

            return(result);
        }
Esempio n. 14
0
        protected static int CalculateStackSizeForParameters(MosaTypeLayout typeLayout, BaseArchitecture architecture, List <Operand> operands)
        {
            // first operand is the call location
            int result = 0;

            foreach (var operand in operands)
            {
                int size, alignment;
                architecture.GetTypeRequirements(typeLayout, operand.Type, out size, out alignment);

                result = Alignment.AlignUp(result, alignment) + size;
            }

            return(result);
        }
        protected static int CalculateStackSizeForParameters(MosaTypeLayout typeLayout, BaseArchitecture architecture, List<Operand> operands)
        {
            // first operand is the call location
            int result = 0;

            foreach (var operand in operands)
            {
                int size, alignment;
                architecture.GetTypeRequirements(typeLayout, operand.Type, out size, out alignment);

                result = Alignment.AlignUp(result, alignment) + size;
            }

            return result;
        }
Esempio n. 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AotCompiler" /> class.
 /// </summary>
 /// <param name="architecture">The architecture.</param>
 /// <param name="typeSystem">The type system.</param>
 /// <param name="typeLayout">The type layout.</param>
 /// <param name="internalTrace">The internal trace.</param>
 /// <param name="compilerOptions">The compiler options.</param>
 public AotCompiler(BaseArchitecture architecture, TypeSystem typeSystem, MosaTypeLayout typeLayout, IInternalTrace internalTrace, CompilerOptions compilerOptions)
     : base(architecture, typeSystem, typeLayout, new CompilationScheduler(typeSystem, true), internalTrace, null, compilerOptions)
 {
 }
Esempio n. 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StackLayout"/> class.
 /// </summary>
 /// <param name="architecture">The architecture.</param>
 /// <param name="parameters">The parameters.</param>
 public StackLayout(BaseArchitecture architecture, int parameters)
 {
     this.architecture = architecture;
     this.parameters   = new Operand[parameters];
 }
Esempio n. 18
0
 public static void Add(BaseArchitecture platform)
 {
     Registry.Add(platform.PlatformName, platform);
 }
Esempio n. 19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StackLayout"/> class.
 /// </summary>
 /// <param name="architecture">The architecture.</param>
 /// <param name="parameters">The parameters.</param>
 public StackLayout(BaseArchitecture architecture, int parameters)
 {
     this.architecture = architecture;
     Parameters        = new Operand[parameters];
     LocalStack        = new List <Operand>();
 }
Esempio n. 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StackLayout"/> class.
 /// </summary>
 /// <param name="architecture">The architecture.</param>
 /// <param name="parameters">The parameters.</param>
 public StackLayout(BaseArchitecture architecture, int parameters)
 {
     this.architecture = architecture;
     Parameters = new Operand[parameters];
     LocalStack = new List<Operand>();
 }
Esempio n. 21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VirtualRegisters"/> class.
 /// </summary>
 public VirtualRegisters(BaseArchitecture architecture)
 {
 }
Esempio n. 22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StackLayout"/> class.
 /// </summary>
 /// <param name="architecture">The architecture.</param>
 /// <param name="parameters">The parameters.</param>
 public StackLayout(BaseArchitecture architecture, int parameters)
 {
     this.architecture = architecture;
     this.parameters = new Operand[parameters];
 }
        /// <summary>
        /// Calculates the stack size for parameters.
        /// </summary>
        /// <param name="typeLayout">The type layouts.</param>
        /// <param name="operands">The operands.</param>
        /// <param name="method">The method.</param>
        /// <returns></returns>
        protected static int CalculateStackSizeForParameters(MosaTypeLayout typeLayout, BaseArchitecture architecture, List<Operand> operands, MosaMethod method)
        {
            Debug.Assert((method.Signature.Parameters.Count + (method.HasThis ? 1 : 0) == operands.Count) ||
            (method.DeclaringType.IsDelegate && method.Signature.Parameters.Count == operands.Count));

            int offset = method.Signature.Parameters.Count - operands.Count;
            int result = 0;

            for (int index = operands.Count - 1; index >= 0; index--)
            {
                Operand operand = operands[index];

                int size, alignment;
                architecture.GetTypeRequirements(typeLayout, operand.Type, out size, out alignment);

                var param = (index + offset >= 0) ? method.Signature.Parameters[index + offset] : null;

                if (param != null && operand.IsR8 && param.Type.IsR4)
                    architecture.GetTypeRequirements(typeLayout, param.Type, out size, out alignment);

                if (size < alignment)
                    size = alignment;

                result += size;
            }

            return result;
        }