Esempio n. 1
0
        private void AddCompileUnit(
            Dictionary <string, CompileUnit> unitMap,
            HashSet <string> objSet,
            string srcFile,
            string cflags)
        {
            string outFile = Path.Combine(OutDir, EscapePath(srcFile) + ".bc");

            objSet.Add(outFile);

            CompileUnit compUnit = new CompileUnit(
                string.Format("clang {0} -o {1} {2} -MD -c -flto " + cflags,
                              srcFile,
                              outFile,
                              OptLevel),
                WorkDir,
                OutDir,
                outFile,
                new HashSet <string> {
                srcFile
            });

            compUnit.OnOutput = Console.WriteLine;
            compUnit.OnError  = Console.Error.WriteLine;

            unitMap.Add(srcFile, compUnit);
        }
Esempio n. 2
0
        private string Optimizing(string lastOptFile, string outNamePostfix, int optCount, bool isLastText)
        {
            for (int i = 0; i < optCount; ++i)
            {
                string strExt;
                string strFlag;

                if (!isLastText || i != optCount - 1)
                {
                    strExt  = ".bc";
                    strFlag = "-c";
                }
                else
                {
                    strExt  = ".ll";
                    strFlag = "-S";
                }

                string optFile = Path.Combine(OutDir, "!opt_" + outNamePostfix + i + strExt);

                CompileUnit optUnit = new CompileUnit(
                    string.Format("clang {0} -o {1} {2} {3} -emit-llvm",
                                  lastOptFile,
                                  optFile,
                                  OptLevel,
                                  strFlag),
                    WorkDir,
                    OutDir,
                    optFile,
                    new HashSet <string> {
                    lastOptFile
                });

                optUnit.OnOutput = Console.WriteLine;
                optUnit.OnError  = Console.Error.WriteLine;

                var status = optUnit.Invoke();
                switch (status)
                {
                case ActionUnit.Status.Skipped:
                    Console.WriteLine("OptSkipped: {0}", lastOptFile);
                    break;

                case ActionUnit.Status.Completed:
                    optUnit.CompletedUpdate();
                    Console.WriteLine("OptCompleted: {0}", lastOptFile);
                    break;

                case ActionUnit.Status.Error:
                    Console.Error.WriteLine("OptError: {0}", lastOptFile);
                    return(null);
                }

                lastOptFile = optFile;
            }

            return(lastOptFile);
        }
Esempio n. 3
0
        private string Optimizing(string lastOptFile, string outNamePostfix, int optCount, bool isLastText)
        {
            var strExt  = ".ll";
            var strFlag = "-S";

            for (int i = 0; i < optCount; ++i)
            {
                // 注释掉待优化文件的 attributes 以防止破优化
                if (!Helper.PatchTextFile(lastOptFile, "attributes #", ";"))
                {
                    return(null);
                }

                string      optFile = Path.Combine(OutDir, "!opt_" + outNamePostfix + i + strExt);
                CompileUnit optUnit = new CompileUnit(
                    string.Format("clang {0} -o {1} {2} {3} -flto",
                                  lastOptFile,
                                  optFile,
                                  OptLevel,
                                  strFlag),
                    WorkDir,
                    OutDir,
                    optFile,
                    new HashSet <string> {
                    lastOptFile
                });

                optUnit.OnOutput = Console.WriteLine;
                optUnit.OnError  = Console.Error.WriteLine;

                var status = optUnit.Invoke();
                switch (status)
                {
                case ActionUnit.Status.Skipped:
                    Console.WriteLine("OptSkipped: {0}", lastOptFile);
                    break;

                case ActionUnit.Status.Completed:
                    optUnit.CompletedUpdate();
                    Console.WriteLine("OptCompleted: {0}", lastOptFile);
                    break;

                case ActionUnit.Status.Error:
                    Console.Error.WriteLine("OptError: {0}", lastOptFile);
                    return(null);
                }

                lastOptFile = optFile;
            }

            return(lastOptFile);
        }
Esempio n. 4
0
        private bool FinalLinking(string outFile, string inFile, string cflags)
        {
            CompileUnit finalCompile = new CompileUnit(
                string.Format("clang {0} -o {1} {2} {3}",
                              inFile,
                              outFile,
                              OptLevel,
                              cflags),
                WorkDir,
                OutDir,
                outFile,
                new HashSet <string> {
                inFile
            });

            finalCompile.OnOutput = Console.WriteLine;
            finalCompile.OnError  = Console.Error.WriteLine;

            var status = finalCompile.Invoke();

            switch (status)
            {
            case ActionUnit.Status.Skipped:
                Console.WriteLine("FinalSkipped: {0}", outFile);
                break;

            case ActionUnit.Status.Completed:
                finalCompile.CompletedUpdate();
                Console.WriteLine("FinalCompleted: {0}", outFile);
                break;

            case ActionUnit.Status.Error:
                Console.Error.WriteLine("FinalError: {0}", outFile);
                return(false);
            }
            return(true);
        }
Esempio n. 5
0
        private string Optimizing(string lastOptFile, string outNamePostfix, int optCount, bool isLastText)
        {
            var strExt  = ".ll";
            var strFlag = "-S";

            for (int i = 0; i < optCount; ++i)
            {
                // 注释掉待优化文件的 attributes 以防止破优化
                if (!Helper.PatchTextFile(lastOptFile, str =>
                {
                    const string cmp = "attributes #";
                    if (str.Length > 0 &&
                        str[0] == cmp[0] &&
                        str.Substring(0, cmp.Length) == cmp)
                    {
                        return(str.Replace(cmp, ";"));
                    }
                    return(str);
                }))
                {
                    return(null);
                }

                // 内部化全局变量
                if (!Helper.PatchTextFile(lastOptFile, str =>
                {
                    if (str.Length > 0 && str[0] == '@')
                    {
                        return(str.Replace(
                                   "= global",
                                   "= internal global")
                               .Replace(
                                   "= local_unnamed_addr global",
                                   "= internal local_unnamed_addr global"));
                    }
                    return(str);
                }))
                {
                    return(null);
                }

                string      optFile = Path.Combine(OutDir, "!opt_" + outNamePostfix + i + strExt);
                CompileUnit optUnit = new CompileUnit(
                    string.Format("clang {0} -o {1} {2} {3} -flto",
                                  lastOptFile,
                                  optFile,
                                  OptLevel,
                                  strFlag),
                    WorkDir,
                    OutDir,
                    optFile,
                    new HashSet <string> {
                    lastOptFile
                });

                optUnit.OnOutput = Console.WriteLine;
                optUnit.OnError  = Console.Error.WriteLine;

                var status = optUnit.Invoke();
                switch (status)
                {
                case ActionUnit.Status.Skipped:
                    Console.WriteLine("OptSkipped: {0}", lastOptFile);
                    break;

                case ActionUnit.Status.Completed:
                    optUnit.CompletedUpdate();
                    Console.WriteLine("OptCompleted: {0}", lastOptFile);
                    break;

                case ActionUnit.Status.Error:
                    Console.Error.WriteLine("OptError: {0}", lastOptFile);
                    return(null);
                }

                lastOptFile = optFile;
            }

            return(lastOptFile);
        }