public bool CheckOption(CompilerOption option, bool defaultValue, XSharpParserRuleContext context, IList <PragmaOption> options)
        {
            bool result = defaultValue;

            if (context != null && options != null && options.Count > 0)
            {
                int line = context.Start.Line;
                foreach (var pragmaoption in options)
                {
                    if (pragmaoption.Line > line)
                    {
                        break;
                    }
                    if (pragmaoption.Option == option || pragmaoption.Option == CompilerOption.All)
                    {
                        switch (pragmaoption.State)
                        {
                        case Pragmastate.On:
                            result = true;
                            break;

                        case Pragmastate.Off:
                            result = false;
                            break;

                        case Pragmastate.Default:
                            result = defaultValue;
                            break;
                        }
                    }
                }
            }
            return(result);
        }
Example #2
0
        public void Compile(
            CompilerOutputType outputType,
            IEnumerable <string> libraries,
            IEnumerable <string> sourceFilePaths,
            string targetFilePath,
            CompilerOption options,
            IDictionary <string, string> defineConstants)
        {
            Parameter.ThrowIfNull(libraries, nameof(libraries));
            Parameter.ThrowIfNull(sourceFilePaths, nameof(sourceFilePaths));
            Parameter.ThrowIfNullOrWhiteSpace(targetFilePath, nameof(targetFilePath));
            Parameter.ThrowIfNull(defineConstants, nameof(defineConstants));

            bool result = this.CompileCore(outputType,
                                           this.Settings.DebuggeeArchitecture,
                                           libraries,
                                           sourceFilePaths,
                                           targetFilePath,
                                           options,
                                           defineConstants);

            Assert.True(result, "The compilation failed.");

            // If the output file was not created, then error
            Assert.True(File.Exists(targetFilePath), "The compiler did not create the expected output. " + targetFilePath);
        }
Example #3
0
 protected abstract bool CompileCore(
     CompilerOutputType outputType,
     SupportedArchitecture architecture,
     IEnumerable <string> libraries,
     IEnumerable <string> sourceFilePaths,
     string targetFilePath,
     CompilerOption options,
     IDictionary <string, string> defineConstants);
        internal bool HasOption(CompilerOption option, SyntaxNode syntax)
        {
            switch (option)
            {
            case CompilerOption.ArrayZero:
                return(CheckOption(option, ArrayZero, syntax));

            case CompilerOption.LateBinding:
                return(CheckOption(option, LateBinding, syntax));

            case CompilerOption.MemVars:
                return(CheckOption(option, MemVars, syntax));

            case CompilerOption.UndeclaredMemVars:
                return(CheckOption(option, UndeclaredMemVars, syntax));

            case CompilerOption.NullStrings:        // vo2
                return(CheckOption(option, VONullStrings, syntax));

            case CompilerOption.SignedUnsignedConversion:     // vo4
                return(CheckOption(option, VOSignedUnsignedConversion, syntax));

            case CompilerOption.ResolveTypedFunctionPointersToPtr:     // vo6
                return(CheckOption(option, VOResolveTypedFunctionPointersToPtr, syntax));

            case CompilerOption.ImplicitCastsAndConversions:     // vo7
                return(CheckOption(option, VOImplicitCastsAndConversions, syntax));

            case CompilerOption.CompatibleIIF:      // vo10
                return(CheckOption(option, VOCompatibleIIF, syntax));

            case CompilerOption.ArithmeticConversions:     // vo11
                return(CheckOption(option, VOArithmeticConversions, syntax));

            case CompilerOption.StringComparisons:     // vo13
                return(CheckOption(option, VOStringComparisons, syntax));

            // other options are not handled or only handled during parsing
            case CompilerOption.ClipperCallingConvention: // Vo5
            case CompilerOption.AllowMissingReturns:      // Vo9
            case CompilerOption.ClipperIntegerDivisions:  // Vo12
            case CompilerOption.FloatConstants:           // vo14
                return(false);
            }
            return(false);
        }
        public bool HasOption(CompilerOption option, XSharpParserRuleContext context, IList <PragmaOption> options)
        {
            switch (option)
            {
            case CompilerOption.ArrayZero:     // az
                return(CheckOption(option, ArrayZero, context, options));

            case CompilerOption.InitLocals:     // initlocals
                return(CheckOption(option, InitLocals, context, options));

            case CompilerOption.MemVars:     // memvar
                return(CheckOption(option, MemVars, context, options));

            case CompilerOption.UndeclaredMemVars:     // undeclared
                return(CheckOption(option, UndeclaredMemVars, context, options));

            case CompilerOption.NullStrings:     // vo2
                return(CheckOption(option, VONullStrings, context, options));

            case CompilerOption.ClipperCallingConvention:     // vo5
                return(CheckOption(option, VOClipperCallingConvention, context, options));

            case CompilerOption.AllowMissingReturns:     // vo9
                return(CheckOption(option, VOAllowMissingReturns, context, options));

            case CompilerOption.ClipperIntegerDivisions:     // vo12
                return(CheckOption(option, VOClipperIntegerDivisions, context, options));

            case CompilerOption.FloatConstants:     // vo14
                return(CheckOption(option, VOFloatConstants, context, options));

            case CompilerOption.LateBinding:                       // lb is handled in cde generation
            case CompilerOption.SignedUnsignedConversion:          // vo4
            case CompilerOption.ResolveTypedFunctionPointersToPtr: // vo6
            case CompilerOption.ImplicitCastsAndConversions:       // vo7
            case CompilerOption.CompatibleIIF:                     // vo10
            case CompilerOption.ArithmeticConversions:             // vo11
            case CompilerOption.StringComparisons:                 // vo13
                return(false);                                     // not handled during parsing
            }
            return(false);
        }
        private bool CheckOption(CompilerOption option, bool defaultValue, SyntaxNode node)
        {
            bool result = defaultValue;

            if (node is CSharpSyntaxNode csn)
            {
                var unit = csn.SyntaxTree.GetRoot() as CompilationUnitSyntax;
                if (unit != null && unit.PragmaOptions != null)
                {
                    var context = csn.XNode as XSharpParserRuleContext;
                    int line    = context.Start.Line;
                    foreach (var pragmaoption in unit.PragmaOptions)
                    {
                        if (pragmaoption.Line > line)
                        {
                            break;
                        }
                        if (pragmaoption.Option == option || pragmaoption.Option == CompilerOption.All)
                        {
                            switch (pragmaoption.State)
                            {
                            case Pragmastate.On:
                                result = true;
                                break;

                            case Pragmastate.Off:
                                result = false;
                                break;

                            case Pragmastate.Default:
                                result = defaultValue;
                                break;
                            }
                        }
                    }
                }
            }
            return(result);
        }
Example #7
0
        protected override bool CompileCore(
            CompilerOutputType outputType,
            SupportedArchitecture architecture,
            IEnumerable <string> libraries,
            IEnumerable <string> sourceFilePaths,
            string targetFilePath,
            CompilerOption options,
            IDictionary <string, string> defineConstants)
        {
            ArgumentBuilder builder = new ArgumentBuilder("-", String.Empty);

            if (options.HasFlag(CompilerOption.GenerateSymbols))
            {
                builder.AppendNamedArgument("g", null);
            }

            if (options.HasFlag(CompilerOption.OptimizeLevel1))
            {
                builder.AppendNamedArgument("O1", null);
            }
            else if (options.HasFlag(CompilerOption.OptimizeLevel2))
            {
                builder.AppendNamedArgument("O2", null);
            }
            else if (options.HasFlag(CompilerOption.OptimizeLevel3))
            {
                builder.AppendNamedArgument("O3", null);
            }
            else
            {
                builder.AppendNamedArgument("O0", null);
            }

            // Enable pthreads
            if (options.HasFlag(CompilerOption.SupportThreading))
            {
                builder.AppendNamedArgument("pthread", null);
            }

            // Just use C++ 11
            builder.AppendNamedArgument("std", "c++11", "=");

            switch (outputType)
            {
            case CompilerOutputType.SharedLibrary:
                builder.AppendNamedArgument("shared", null);
                builder.AppendNamedArgument("fpic", null);
                break;

            case CompilerOutputType.ObjectFile:
                builder.AppendNamedArgument("c", null);
                builder.AppendNamedArgument("fpic", null);
                break;

            case CompilerOutputType.Unspecified:
            // Treat Unspecified as Executable, since executable is the default
            case CompilerOutputType.Executable:
                // Compiling an executable does not have a command line option, it's the default
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(outputType), "Unhandled output type: " + outputType);
            }

            switch (architecture)
            {
            case SupportedArchitecture.x64:
                builder.AppendNamedArgument("m", "64");
                DefineConstant(builder, "DEBUGGEE_ARCH", "64");
                break;

            case SupportedArchitecture.x86:
                builder.AppendNamedArgument("m", "32");
                DefineConstant(builder, "DEBUGGEE_ARCH", "32");
                break;

            case SupportedArchitecture.arm:
                builder.AppendNamedArgument("m", "arm");
                DefineConstant(builder, "DEBUGGEE_ARCH", "ARM");
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(architecture), "Unhandled target architecture: " + architecture);
            }

            // Define a constant for the platform name.
            DefineConstant(builder, "DEBUGGEE_PLATFORM", GetPlatformName());

            this.SetAdditionalArguments(builder);

            builder.AppendNamedArgument("o", targetFilePath);

            foreach (string sourceFilePath in sourceFilePaths)
            {
                builder.AppendArgument(sourceFilePath);
            }

            foreach (string library in libraries)
            {
                builder.AppendNamedArgument("l", library);
            }

            foreach (var defineConstant in defineConstants)
            {
                DefineConstant(builder, defineConstant.Key, defineConstant.Value);
            }

            return(0 == this.RunCompiler(builder.ToString(), targetFilePath));
        }
Example #8
0
 public void SetOption(CompilerOption option, uint value)
 {
     spvc_compiler_options_set_uint(Handle, option, value);
     IsDirty = true;
 }
Example #9
0
 public void SetOption(CompilerOption option, bool value)
 {
     spvc_compiler_options_set_bool(Handle, option, value);
     IsDirty = true;
 }
Example #10
0
 public static extern Result spvc_compiler_options_set_uint(spvc_compiler_options options, CompilerOption option, uint value);
Example #11
0
 public static extern Result spvc_compiler_options_set_bool(spvc_compiler_options options, CompilerOption option, [MarshalAs(UnmanagedType.I1)] bool value);
Example #12
0
 public PragmaOption(ParserRuleContext context, Pragmastate state, CompilerOption option) : base(context, state)
 {
     Option = option;
 }
        protected override bool CompileCore(
            CompilerOutputType outputType,
            SupportedArchitecture architecture,
            IEnumerable <string> libraries,
            IEnumerable <string> sourceFilePaths,
            string targetFilePath,
            CompilerOption options,
            IDictionary <string, string> defineConstants)
        {
            ArgumentBuilder clBuilder   = new ArgumentBuilder("/", ":");
            ArgumentBuilder linkBuilder = new ArgumentBuilder("/", ":");

            foreach (var defineConstant in defineConstants)
            {
                DefineConstant(clBuilder, defineConstant.Key, defineConstant.Value);
            }
            DefineConstant(clBuilder, "_WIN32");

            // Suppresses error C4996 for 'getenv' (in debuggees/kitchensink/src/environment.cpp)
            DefineConstant(clBuilder, "_CRT_SECURE_NO_WARNINGS");

            if (options.HasFlag(CompilerOption.GenerateSymbols))
            {
                clBuilder.AppendNamedArgument("ZI", null);
                clBuilder.AppendNamedArgument("Debug", null);
            }

            if (!options.HasFlag(CompilerOption.OptimizeLevel1) &&
                !options.HasFlag(CompilerOption.OptimizeLevel2) &&
                !options.HasFlag(CompilerOption.OptimizeLevel3))
            {
                // Disable optimization
                clBuilder.AppendNamedArgument("Od", null);
            }

            // Add options that are set by default in VS
            AddDefaultOptions(clBuilder);

            if (this.Settings.Properties != null)
            {
                // Get the include folders from the compiler properties
                string rawIncludes;
                if (!this.Settings.Properties.TryGetValue("Include", out rawIncludes))
                {
                    rawIncludes = String.Empty;
                }

                IEnumerable <string> includes = rawIncludes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim());
                foreach (string include in includes)
                {
                    clBuilder.AppendNamedArgumentQuoted("I", include, string.Empty);
                }

                // Get the lib folders from the compiler properties
                string rawLibs;
                if (!this.Settings.Properties.TryGetValue("Lib", out rawLibs))
                {
                    rawLibs = String.Empty;
                }

                IEnumerable <string> libs = rawLibs.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim());
                foreach (string lib in libs)
                {
                    linkBuilder.AppendNamedArgumentQuoted("LIBPATH", lib);
                }
            }

            switch (outputType)
            {
            case CompilerOutputType.SharedLibrary:
                // Create.DLL
                clBuilder.AppendNamedArgument("LD", null);
                clBuilder.AppendNamedArgument("Fo", Path.GetDirectoryName(targetFilePath) + Path.DirectorySeparatorChar);
                clBuilder.AppendNamedArgument("Fe", targetFilePath);
                break;

            case CompilerOutputType.ObjectFile:
                // Name obj
                clBuilder.AppendNamedArgument("Fo", targetFilePath);
                break;

            case CompilerOutputType.Unspecified:
            // Treat Unspecified as Executable, since executable is the default
            case CompilerOutputType.Executable:
                // Compiling an executable does not have a command line option, it's the default
                // Name exe
                clBuilder.AppendNamedArgument("Fo", Path.GetDirectoryName(targetFilePath) + Path.DirectorySeparatorChar);
                clBuilder.AppendNamedArgument("Fe", targetFilePath);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(outputType), "Unhandled output type: " + outputType);
            }

            // Specify the PDB name
            clBuilder.AppendNamedArgument("Fd", Path.ChangeExtension(targetFilePath, "pdb"));

            // Define a constant for the platform name.
            DefineConstant(clBuilder, "DEBUGGEE_PLATFORM", GetPlatformName());
            DefineConstant(clBuilder, "DEBUGGEE_COMPILER", "Visual C++");

            foreach (string sourceFilePath in sourceFilePaths)
            {
                clBuilder.AppendArgument(sourceFilePath);
            }

            foreach (string library in libraries)
            {
                clBuilder.AppendArgument(library);
            }

            switch (architecture)
            {
            case SupportedArchitecture.x64:
                DefineConstant(clBuilder, "DEBUGGEE_ARCH", "64");
                linkBuilder.AppendNamedArgument("MACHINE", "X64");
                break;

            case SupportedArchitecture.x86:
                DefineConstant(clBuilder, "DEBUGGEE_ARCH", "32");
                linkBuilder.AppendNamedArgument("MACHINE", "X86");
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(architecture), "Unhandled target architecture: " + architecture);
            }

            // Pass the linker arguments (Note, the link parameter doesn't use the ":" suffix)
            clBuilder.AppendNamedArgument("link", linkBuilder.ToString(), overrideSuffix: " ");

            return(0 == this.RunCompiler(clBuilder.ToString(), targetFilePath));
        }