Esempio n. 1
0
        /// <summary>
        /// For loop for array which will invoke Amplifier code.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="compiler">The compiler.</param>
        /// <param name="kernelName">Name of the kernel.</param>
        /// <param name="args">The arguments.</param>
        public static void AmplifyFor(this Array x, BaseCompiler compiler, string kernelName, params object[] args)
        {
            var arguments = args.ToList();

            arguments.Insert(0, x);
            compiler.Execute(kernelName, arguments.ToArray());
        }
Esempio n. 2
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 UnboxValueTypeStage(),
                (compilerOptions.EnableInlinedMethods) ? new InlineStage() : null,
                (compilerOptions.EnableSSA) ? new EdgeSplitStage() : null,
                (compilerOptions.EnableSSA) ? new EnterSSAStage() : null,
                (compilerOptions.EnableSparseConditionalConstantPropagation && compilerOptions.EnableSSA) ? new SparseConditionalConstantPropagationStage() : null,
                (compilerOptions.EnableIROptimizations) ? new IROptimizationStage() : null,
                new LowerIRStage(),

                (compilerOptions.IRLongExpansion && compiler.Architecture.NativePointerSize == 4) ? new IRLongExpansionStage() : null,

                (compilerOptions.TwoPassOptimizations && compilerOptions.EnableIROptimizations && compilerOptions.EnableSparseConditionalConstantPropagation && compilerOptions.EnableSSA) ? new SparseConditionalConstantPropagationStage() : null,
                (compilerOptions.TwoPassOptimizations && compilerOptions.EnableIROptimizations && compilerOptions.EnableSparseConditionalConstantPropagation && compilerOptions.EnableSSA) ? new IROptimizationStage() : null,
                (compilerOptions.EnableSSA) ? new LeaveSSAStage() : null,
                new IRCleanupStage(),
                (compilerOptions.EnableInlinedMethods) ? new InlineEvaluationStage() : null,
                new DevirtualizeCallStage(),
                new CallStage(),
                new PlatformStubStage(),
                new PlatformEdgeSplitStage(),
                new VirtualRegisterRenameStage(),
                new GreedyRegisterAllocatorStage(),
                new StackLayoutStage(),
                new EmptyBlockRemovalStage(),
                new BlockOrderingStage(),
                new CodeGenerationStage(),
                new PreciseGCStage(),
                new ProtectedRegionLayoutStage(),
            });
        }
Esempio n. 3
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.EnableVariablePromotion) ? new PromoteTempVariablesStage() : 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(),
            });
        }
Esempio n. 4
0
        Assembly GetAssemblyFromSource(string vpath)
        {
            vpath = UrlUtils.Combine(BaseVirtualDir, vpath);
            string realPath = MapPath(vpath, false);

            if (!File.Exists(realPath))
            {
                ThrowParseException("File " + vpath + " not found");
            }

            AddSourceDependency(vpath);

            CompilerResults    result;
            string             tmp;
            CompilerParameters parameters;
            CodeDomProvider    provider = BaseCompiler.CreateProvider(HttpContext.Current, language, out parameters, out tmp);

            if (provider == null)
            {
                throw new HttpException("Cannot find provider for language '" + language + "'.");
            }

            AssemblyBuilder abuilder = new AssemblyBuilder(provider);

            abuilder.CompilerOptions = parameters;
            abuilder.AddAssemblyReference(BuildManager.GetReferencedAssemblies() as List <Assembly>);
            abuilder.AddCodeFile(realPath);
            result = abuilder.BuildAssembly(new VirtualPath(vpath));

            if (result.NativeCompilerReturnValue != 0)
            {
                using (StreamReader reader = new StreamReader(realPath)) {
                    throw new CompilationException(realPath, result.Errors, reader.ReadToEnd());
                }
            }

            AddAssembly(result.CompiledAssembly, true);
            return(result.CompiledAssembly);
        }
Esempio n. 5
0
 void ICompilerStage.Setup(BaseCompiler compiler)
 {
     base.Setup(compiler);
     this.linker = RetrieveLinkerFromCompiler();
 }
Esempio n. 6
0
        static void Main(string[] args)
        {
            CompilerType compilerType = ReadCompilerType();
            ICompiler    compiler     = BaseCompiler.CreateCompiler(compilerType);

            Console.WriteLine("Enter file path.");
            string sourceFilePath = Console.ReadLine();
            string source         = File.ReadAllText(sourceFilePath);

            string fileName = Path.GetRandomFileName();

            if (compilerType == CompilerType.Java)
            {
                fileName = GetClassName(source);
            }
            string workingDirectory = Environment.CurrentDirectory + "\\" + Guid.NewGuid().ToString();

            Directory.CreateDirectory(workingDirectory);

            WriteSourceToFile(source, compilerType, workingDirectory, fileName);
            CompileResult compileResult = compiler.Compile(new CompilerArguments(fileName, workingDirectory, new List <string> {
                source
            }));

            if (!compileResult.IsSuccessfull)
            {
                Console.WriteLine($"Compile time error: {compileResult.Errors}");
                return;
            }

            IExecutor executor              = BaseExecutor.CreateExecutor(compilerType);
            string    compiledFileName      = Path.GetFileName(compileResult.CompiledFilePath);
            string    compiledFileDirectory = Path.GetDirectoryName(compileResult.CompiledFilePath);

            Console.WriteLine("Enter inputs to test the program. If you want to stop enter 'stop'!");
            string input = string.Empty;

            while ((input = Console.ReadLine()) != "stop")
            {
                ProcessExecutionResult processExecutionResult = executor.Execute(input, compiledFileName, compiledFileDirectory).GetAwaiter().GetResult();

                if (processExecutionResult.IsSuccesfull && processExecutionResult.Type == ProcessExecutionResultType.Success)
                {
                    Console.WriteLine("The application was run seccessfully!");
                    Console.WriteLine(processExecutionResult.ReceivedOutput);
                }
                else
                {
                    Console.WriteLine("Some errors occured!");
                    Console.WriteLine(processExecutionResult.ErrorOutput);
                }

                Console.WriteLine("Process statistics");
                Console.WriteLine($"Exit code: {processExecutionResult.ExitCode}");
                Console.WriteLine($"Memory Used: {processExecutionResult.MemoryUsed}");
                Console.WriteLine($"Processor time used: {processExecutionResult.TotalProcessorTime}");
                Console.WriteLine($"Working time: {processExecutionResult.TimeWorked}");
                Console.WriteLine($"Execution Type: {processExecutionResult.Type}");
                Console.WriteLine(new string('-', 50));
            }

            DeleteWorkingDirectory(workingDirectory);
        }
        /// <summary>
        /// Call RemoteHost.ExtractInfoFromCompiler() instead.
        /// Requires these utilities on the remote system: pwd, echo, which, touch, sort, pushd, popd, zip
        /// </summary>
        /// <param name="remote">Remote host where compiler installed</param>
        public void ExtractAdditionalInfo(RemoteHost remote)
        {
            String remoteTempFile = String.Format(Compiler.RemoteTempFile, BaseCompiler.ShortName, instanceGuid.ToString());

            if (remote.Execute("pwd || echo $PWD", out String pwd) != RemoteHost.Success)
            {
                throw new ApplicationException("could not get current directory name");
            }
            pwd = pwd.TrimEnd(RemoteHost.LineEndingChars);

            if (remote.Execute($"which {BaseCompiler.ExePath}", out String absExePath) != RemoteHost.Success)
            {
                throw new ApplicationException("could not get absolute compiler path");
            }
            absExePath = absExePath.TrimEnd(RemoteHost.LineEndingChars);

            if (remote.Execute($"{BaseCompiler.ExePath} -dumpversion", out String version) != RemoteHost.Success)
            {
                throw new ApplicationException("could not extract version from compiler");
            }
            Version = version.TrimEnd(RemoteHost.LineEndingChars);

            // create temporary .c file for auto-distinguishment between C and C++
            if (remote.Execute($"touch {remoteTempFile} || echo > {remoteTempFile}", out String _) != RemoteHost.Success)
            {
                throw new ApplicationException("could not create temporary file");
            }

            if (remote.Execute($"{BaseCompiler.ExePath} {this} -E -dM {remoteTempFile} | sort", out String defines) != RemoteHost.Success)
            {
                throw new ApplicationException("could not extract defines from compiler");
            }

            if (remote.Execute($"{BaseCompiler.ExePath} {this} -E -Wp,-v {remoteTempFile} 2>&1 1> /dev/null", out String includeDirs) != RemoteHost.Success)
            {
                throw new ApplicationException("could not extract include dirs from compiler");
            }

            AbsoluteCrosspath xpwd      = AbsoluteCrosspath.FromString(pwd);
            AbsoluteCrosspath xcompiler = AbsoluteCrosspath.FromString(absExePath);

            if (!xcompiler.ToString().Equals(BaseCompiler.ExePath.ToString()))
            {
                AbsoluteCrosspath compilerLocatedIn = new AbsoluteCrosspath(xcompiler);
                ((RelativeCrosspath)BaseCompiler.ExePath).SetWorkingDirectory(xcompiler.ToContainingDirectory());
                Logger.WriteLine(LogLevel.Info, $"compiler '{BaseCompiler.ExePath}' actually located at '{xcompiler}'");
            }

            Defines.Clear();
            Platform fallbackPlatform = Platform.x64;

            foreach (String macro in defines.Split(RemoteHost.LineEndings, StringSplitOptions.RemoveEmptyEntries))
            {
                // assuming standard format '#define MACRO some thing probably with spaces'
                String[] defArray = macro.Split(new[] { ' ' }, 3);
                // try to auto-detect the platform
                if (VSPlatform == Platform.Unknown)
                {
                    switch (defArray[1])
                    {
                    case "__x86_64__": VSPlatform = Platform.x64; break;

                    case "__i386__": VSPlatform = Platform.x86; break;

                    case "__arm__": VSPlatform = Platform.ARM; break;

                    case "__aarch64__": VSPlatform = Platform.ARM64; break;

                    case "__mips__": VSPlatform = Platform.MIPS; break;

                    case "__WORDSIZE__":     /* this seems to be standard */
                    case "__INTPTR_WIDTH__": /* this not, but use as fallback */
                        if (Int32.Parse(defArray[2]) == 32)
                        {
                            fallbackPlatform = Platform.x86;
                        }
                        break;
                    }
                }
                Defines.Add(new Define(defArray[1], defArray[2]));
            }
            if (VSPlatform == Platform.Unknown)
            {
                VSPlatform = fallbackPlatform;
            }

            IncludeDirectories.Clear();
            IncludeDirectoryType incDirType = IncludeDirectoryType.Null;

            foreach (String cppLine in includeDirs.Split(RemoteHost.LineEndings, StringSplitOptions.RemoveEmptyEntries))
            {
                // sample output:

/*
 * ignoring nonexistent directory "/opt/gcc-4.1.2-glibc-2.5-binutils-2.17-kernel-2.6.18/arm-v5te-linux-gnueabi/include"
 #include "..." search starts here:
 #include <...> search starts here:
 * /opt/gcc-4.1.2-glibc-2.5-binutils-2.17-kernel-2.6.18/bin/../lib/gcc/arm-v5te-linux-gnueabi/4.1.2/include
 * /opt/gcc-4.1.2-glibc-2.5-binutils-2.17-kernel-2.6.18/bin/../sysroot-arm-v5te-linux-gnueabi/usr/include
 * End of search list.
 */
                // regarding priority:
                // -iquote has the highest priority affecting #include "..." not only #include <...>
                // -I has lower priority than -iquote
                // -isystem has lower priority but yet higher priority than system-wide headers
                //   then follows the priority of system-wide-headers
                // -idirafter has the lowest priority possible
                // example (gcc 9):

/*
 * $ gcc -x c -c -Wp,-v -iquote /proc/1 -I /proc/10 -isystem /proc/12 -I /proc/14 -idirafter /proc/1204 - < /dev/null
 * ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
 * ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/include-fixed"
 * ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-gnu/include"
 #include "..." search starts here:
 * /proc/1
 #include <...> search starts here:
 * /proc/10
 * /proc/14
 * /proc/12
 * /usr/lib/gcc/x86_64-linux-gnu/9/include
 * /usr/local/include
 * /usr/include/x86_64-linux-gnu
 * /usr/include
 * /proc/1204
 * End of search list.
 */
                // usually gcc does not have any include directories in the #include "..." section.
                // so the reason for parsing this section is when using some compiler wrapper that puts some paths to -iquote.
                if (cppLine == "#include \"...\" search starts here:")
                {
                    incDirType = IncludeDirectoryType.Quote;
                    continue;
                }
                if (cppLine == "#include <...> search starts here:")
                {
                    incDirType = IncludeDirectoryType.System;
                    continue;
                }

                if (cppLine.Length > 0 && cppLine[0] == ' ')
                {
                    Crosspath xpath = Crosspath.FromString(cppLine.Substring(1));
                    if (xpath is RelativeCrosspath relPath)
                    {
                        relPath.SetWorkingDirectory(xpwd);
                        xpath = relPath.Absolutized();
                    }
                    IncludeDirectory incDir       = new IncludeDirectory(xpath as AbsoluteCrosspath, incDirType);
                    IncludeDirectory incDirCached = BaseCompiler.TrackIncludeDir(incDir);
                    if (incDirCached != null)
                    {
                        incDir = incDirCached;
                    }
                    IncludeDirectories.Add(incDir);

                    continue;
                }

                if (cppLine == "End of search list.")
                {
                    break;
                }
            }

            HaveAdditionalInfo = true;

            Logger.WriteLine(LogLevel.Info, $"{absExePath} {this} is {VSPlatform} compiler");
        }
Esempio n. 8
0
        void ICompilerStage.Setup(BaseCompiler compiler)
        {
            base.Setup(compiler);

            plugTypeAttribute = typeSystem.GetType("Mosa.Internal.Plug", "Mosa.Internal.Plug", "PlugTypeAttribute");
            plugMethodAttribute = typeSystem.GetType("Mosa.Internal.Plug", "Mosa.Internal.Plug", "PlugMethodAttribute");
        }
Esempio n. 9
0
 private static List<RuntimeMethod> RecompileMethods(BaseCompiler compiler, List<Token> types, RuntimeMethod method)
 {
     throw new NotImplementedException();
 }
Esempio n. 10
0
 private static List<Token> GetTokenTypesForMethod(BaseCompiler compiler, RuntimeType type, RuntimeMethod method)
 {
     throw new NotImplementedException();
 }