Beispiel #1
0
 internal LLVMBackendEventArgs(
     CompileUnit compileUnit,
     LLVMModuleRef moduleRef)
 {
     CompileUnit = compileUnit;
     ModuleRef   = moduleRef;
 }
Beispiel #2
0
        public void RunTestCase(string testFile)
        {
            try
            {
                string        testFilePath         = this.ResolvePath(testFile);
                CompileUnit[] array                = CSharpAssertModule.Convert(new FileInput(testFilePath));
                CompileUnit   resultingCSharpNode  = array[0];
                CompileUnit   resultingBooNode     = array[1];
                string[]      expectedOutput       = this.GetExpectedOutput(testFilePath);
                string        expectedCSharpOutput = expectedOutput[0];
                string        expectedBooOutput    = expectedOutput[1];
                string        inputJSCode          = expectedOutput[2];
                Debug.WriteLine("JavaScript:\n" + inputJSCode);

                var output = new StringWriter();
                resultingCSharpNode.Accept(new CSharpPrinter(output));
                Debug.WriteLine("Generated C#:\n" + output.ToString().TrimEnd());
                Debug.WriteLine("Generated Boo:\n" + resultingBooNode.ToCodeString());

                CSharpAssertModule.AssertCSharpCode(expectedCSharpOutput, resultingCSharpNode);
                StringsModule.AssertAreEqualIgnoringNewLineDifferences(expectedBooOutput, resultingBooNode.ToCodeString());
            }
            catch (CompilationErrorsException x)
            {
                Assert.Fail(x.Errors.ToString(true));
            }
        }
        /// <summary>
        /// Constructs a default LLVM-based ABI specification.
        /// </summary>
        /// <param name="unit">The compile unit used for ABI generation.</param>
        public DefaultLLVMABI(CompileUnit unit)
            : base(unit)
        {
            var backend = unit.Backend as LLVMBackend;

            if (backend == null)
            {
                throw new NotSupportedException(ErrorMessages.NotSupportedBackend);
            }
            LLVMTargetData = CreateTargetDataLayout(backend.LLVMTargetMachine);
            foreach (var managedAlignment in ManagedAlignments)
            {
                var managedType = managedAlignment.Key;
                var llvmType    = unit.GetType(managedType);
                var alignment   = ABIAlignmentOfType(LLVMTargetData, llvmType);
                // We need a special case for the builtin mapping of 64bit floats
                // to 32bit floats since this mapping changes the alignment logic.
                if (unit.Force32BitFloats && managedType == typeof(double))
                {
                    managedType = typeof(float);
                }
                if (ManagedAlignments[managedType] != alignment)
                {
                    throw new NotSupportedException(string.Format(
                                                        ErrorMessages.CustomABIImplementationRequired, managedAlignment.Key));
                }
                Alignments.Add(managedAlignment.Key, alignment);
            }
            AddNonBlittableTypes();
            AddPtrAlignment(ABIAlignmentOfType(
                                LLVMTargetData,
                                unit.LLVMContext.VoidPtrType));
        }
        /// <summary>
        /// Builds a warp-shuffle mask.
        /// </summary>
        /// <param name="unit">The current unit.</param>
        /// <param name="builder">The current builder.</param>
        /// <param name="width">The width that was passed by the user.</param>
        /// <param name="addOrMask">True, to add an or mask consisting of (WarpSize - 1).</param>
        /// <returns>A value that represents the desired warp-shuffle mask.</returns>
        private LLVMValueRef BuildWarpShuffleMask(
            CompileUnit unit,
            LLVMBuilderRef builder,
            LLVMValueRef width,
            bool addOrMask)
        {
            var warpSize = MakeWarpSize(builder);
            var warpDiff = BuildSub(builder, warpSize, width, string.Empty);
            var result   = BuildShl(
                builder,
                warpDiff,
                ConstInt(unit.GetType(BasicValueType.Int32), 8, false),
                string.Empty);

            if (addOrMask)
            {
                var orMask = BuildSub(
                    builder,
                    warpSize,
                    ConstInt(unit.GetType(BasicValueType.Int32), 1, false),
                    string.Empty);
                result = BuildOr(
                    builder,
                    result,
                    orMask,
                    string.Empty);
            }
            return(result);
        }
        static CompileUnit SyntaxTreeFor(IFile file)
        {
            var compileUnit = new CompileUnit();

            UnityScriptParser.ParseReader(file.OpenText(), "", new CompilerContext(), compileUnit);
            return(compileUnit);
        }
Beispiel #6
0
        /// <summary>
        /// Creates an <see cref="Index3"/> in the LLVM world containing the current group-thread indices.
        /// </summary>
        /// <param name="unit">The target unit.</param>
        /// <param name="entryPoint">The entry point.</param>
        /// <param name="builder">The LLVM builder.</param>
        /// <param name="cudaDeviceFunctions">A reference to the cuda device functions.</param>
        /// <returns>An <see cref="Index3"/> in the LLVM world containg the current group-thread indices.</returns>
        private static LLVMValueRef CreateGroupIndexValue(
            CompileUnit unit,
            EntryPoint entryPoint,
            LLVMBuilderRef builder,
            PTXDeviceFunctions cudaDeviceFunctions)
        {
            var indexType        = unit.GetType(entryPoint.UngroupedIndexType);
            var threadIndexValue = GetUndef(indexType);

            Debug.Assert(entryPoint.Type >= IndexType.Index1D);

            var isGroupedIndex = entryPoint.IsGroupedIndexEntry;

            threadIndexValue = BuildInsertValue(builder, threadIndexValue, BuildCall(
                                                    builder, cudaDeviceFunctions.GetThreadIdxX.Value), 0, "TIdx1");

            if (entryPoint.Type >= IndexType.Index2D && !isGroupedIndex || entryPoint.Type >= IndexType.GroupedIndex2D)
            {
                threadIndexValue = BuildInsertValue(builder, threadIndexValue, BuildCall(
                                                        builder, cudaDeviceFunctions.GetThreadIdxY.Value), 1, "TIdx2");
            }
            if (entryPoint.Type >= IndexType.Index3D && !isGroupedIndex || entryPoint.Type >= IndexType.GroupedIndex3D)
            {
                threadIndexValue = BuildInsertValue(builder, threadIndexValue, BuildCall(
                                                        builder, cudaDeviceFunctions.GetThreadIdxZ.Value), 2, "TIdx3");
            }

            return(threadIndexValue);
        }
Beispiel #7
0
        /// <summary>
        /// Creates a comparison of the current global index to the custom desired number of user threads.
        /// </summary>
        /// <param name="unit">The target unit.</param>
        /// <param name="entryPoint">The entry point.</param>
        /// <param name="builder">The LLVM builder.</param>
        /// <param name="cudaDeviceFunctions">A reference to the cuda device functions.</param>
        /// <param name="globalIndexValue">The current global index values.</param>
        /// <param name="userIndexRange">The user given custom launcher range.</param>
        /// <returns>An instance of an <see cref="IGroupedIndex{TIndex}"/> in the LLVM world.</returns>
        private static LLVMValueRef CreateGlobalIndexRangeComparison(
            CompileUnit unit,
            EntryPoint entryPoint,
            LLVMBuilderRef builder,
            PTXDeviceFunctions cudaDeviceFunctions,
            LLVMValueRef globalIndexValue,
            LLVMValueRef userIndexRange)
        {
            Debug.Assert(entryPoint.Type >= IndexType.Index1D && entryPoint.Type < IndexType.GroupedIndex1D);

            LLVMValueRef comparisonValue = ConstInt(unit.LLVMContext.Int1Type, 1, false);

            for (int i = 0, e = (int)entryPoint.Type; i <= e; ++i)
            {
                var compareResult = BuildICmp(
                    builder,
                    LLVMIntPredicate.LLVMIntSLT,
                    BuildExtractValue(builder, globalIndexValue, 0, "GlobalIdx_" + i),
                    BuildExtractValue(builder, userIndexRange, 0, "UserRange_" + i),
                    "InRange_" + i);
                comparisonValue = BuildAnd(
                    builder,
                    comparisonValue,
                    compareResult,
                    "RangeOr_" + i);
            }

            return(comparisonValue);
        }
Beispiel #8
0
        /// <summary>
        /// Creates a signature for the actual kernel entry point.
        /// </summary>
        /// <param name="unit">The target unit.</param>
        /// <param name="entryPoint">The target entry point.</param>
        /// <param name="parameterOffset">The parameter offset for the actual kernel parameters.</param>
        /// <returns>A signature for the actual kernel entry point.</returns>
        private LLVMTypeRef CreatePTXKernelFunctionType(
            CompileUnit unit,
            EntryPoint entryPoint,
            out int parameterOffset)
        {
            parameterOffset = entryPoint.IsGroupedIndexEntry ? 0 : 1;
            var numUniformVariables = entryPoint.NumUniformVariables;
            var argTypes            = new LLVMTypeRef[parameterOffset + numUniformVariables + entryPoint.NumDynamicallySizedSharedMemoryVariables];

            // Custom dispatch-size information for implicitly grouped kernels
            if (parameterOffset > 0)
            {
                argTypes[0] = unit.GetType(entryPoint.UngroupedIndexType);
            }

            Debug.Assert(parameterOffset >= 0 && parameterOffset < 2);

            for (int i = 0, e = numUniformVariables; i < e; ++i)
            {
                argTypes[i + parameterOffset] = unit.GetType(entryPoint.UniformVariables[i].VariableType);
            }

            // Attach length information to dynamically sized variables using runtime information
            for (int i = 0, e = entryPoint.NumDynamicallySizedSharedMemoryVariables; i < e; ++i)
            {
                argTypes[i + parameterOffset + numUniformVariables] = unit.GetType(typeof(int));
            }

            return(FunctionType(Context.LLVMContext.VoidType, argTypes));
        }
Beispiel #9
0
        /// <summary>
        /// Creates an <see cref="Index3"/> in the LLVM world containing the current grid indices.
        /// </summary>
        /// <param name="unit">The target unit.</param>
        /// <param name="entryPoint">The entry point.</param>
        /// <param name="builder">The LLVM builder.</param>
        /// <param name="cudaDeviceFunctions">A reference to the cuda device functions.</param>
        /// <returns>An <see cref="Index3"/> in the LLVM world containg the current grid indices.</returns>
        private static LLVMValueRef CreateIndexValue(
            CompileUnit unit,
            EntryPoint entryPoint,
            LLVMBuilderRef builder,
            PTXDeviceFunctions cudaDeviceFunctions)
        {
            var indexType  = unit.GetType(entryPoint.UngroupedIndexType);
            var indexValue = GetUndef(indexType);

            Debug.Assert(entryPoint.Type >= IndexType.Index1D);

            indexValue = BuildInsertValue(builder, indexValue, BuildCall(
                                              builder, cudaDeviceFunctions.GetBlockIdxX.Value), 0, "Idx1");

            if (entryPoint.Type >= IndexType.Index2D && entryPoint.Type <= IndexType.Index3D ||
                entryPoint.Type >= IndexType.GroupedIndex2D)
            {
                indexValue = BuildInsertValue(builder, indexValue, BuildCall(
                                                  builder, cudaDeviceFunctions.GetBlockIdxY.Value), 1, "Idx2");
            }
            if (entryPoint.Type == IndexType.Index3D || entryPoint.Type == IndexType.GroupedIndex3D)
            {
                indexValue = BuildInsertValue(builder, indexValue, BuildCall(
                                                  builder, cudaDeviceFunctions.GetBlockIdxZ.Value), 2, "Idx3");
            }

            return(indexValue);
        }
Beispiel #10
0
        public static CompileUnit ParseReader(string readerName, TextReader reader)
        {
            CompileUnit cu = new CompileUnit();

            cu.Modules.Add(ParseModule(readerName, reader, null));
            return(cu);
        }
Beispiel #11
0
        /// <summary cref="Backend.TargetUnit(CompileUnit)"/>
        internal override void TargetUnit(CompileUnit unit)
        {
            var module       = unit.LLVMModule;
            var dataLayout   = GetLLVMLayout(Platform);
            var targetTriple = GetLLVMTriple(Platform);

            SetDataLayout(module, dataLayout);
            SetTarget(module, targetTriple);

            if (CreateMemoryBufferWithContentsOfFile(LibDevicePath, out LLVMMemoryBufferRef libDeviceBuffer, out IntPtr errorMessage))
            {
                throw new InvalidOperationException(string.Format(
                                                        ErrorMessages.CouldNotReadLibDevice, Marshal.PtrToStringAnsi(errorMessage)));
            }
            if (GetBitcodeModuleInContext(unit.LLVMContext, libDeviceBuffer, out LLVMModuleRef libDeviceModule, out errorMessage))
            {
                throw new InvalidOperationException(string.Format(
                                                        ErrorMessages.CouldNotLoadLibDevice, Marshal.PtrToStringAnsi(errorMessage)));
            }
            SetDataLayout(libDeviceModule, dataLayout);
            SetTarget(libDeviceModule, targetTriple);
            LinkModules2(module, libDeviceModule);

            var functions = new PTXDeviceFunctions(unit);

            ptxDeviceFunctions.Add(unit, functions);
            unit.RegisterDeviceFunctions(functions);
        }
Beispiel #12
0
        public void Run()
        {
            CompileUnit cu = _context.CompileUnit;

            new XmlSerializer(cu.GetType()).Serialize(Console.Out, cu);
            Console.WriteLine();
        }
Beispiel #13
0
 public CompilerContext(CompilerParameters parameters, CompileUnit compileUnit)
 {
     Parameters        = parameters;
     CompileUnit       = compileUnit;
     Errors            = new SortedSet <CompilerError>();
     NamespaceResolver = new NamespaceResolver(this);
 }
        /// <summary>
        /// Constructs a new code generator that targets the given unit.
        /// </summary>
        /// <param name="unit">The target unit.</param>
        /// <param name="method">The source method for code generation.</param>
        /// <param name="disassembledMethod">The disassembled method for code generation.</param>
        public CodeGenerator(
            CompileUnit unit,
            Method method,
            DisassembledMethod disassembledMethod = null)
        {
            Debug.Assert(unit != null, "Invalid unit");
            Debug.Assert(method != null, "Invalid method");

            Unit   = unit;
            Method = method;

            disassembledMethod = disassembledMethod ??
                                 DisassembledMethod.Disassemble(method.MethodBase, CompilationContext.NotSupportedILInstructionHandler);
            if (disassembledMethod.Method.GetMethodBody().ExceptionHandlingClauses.Count > 0)
            {
                throw CompilationContext.GetNotSupportedException(
                          ErrorMessages.CustomExceptionSemantics, method.Name);
            }
            Debug.Assert(
                method.MethodBase == disassembledMethod.Method,
                "The provided disassembled method does not match the given method for code generation");

            this.disassembledMethod = disassembledMethod;
            Builder = CreateBuilderInContext(unit.LLVMContext);

            InitCFG();
            InitArgsAndLocals();
        }
Beispiel #15
0
 public override void OnCompileUnit(CompileUnit node)
 {
     foreach (Module m in node.Modules)
     {
         m.Accept(this);
     }
 }
Beispiel #16
0
        /// <summary>
        /// Creates instance of <see cref="CompilerContext"/>.
        /// </summary>
        /// <param name="parameters">Compiler parameters of the session</param>
        /// <param name="compileUnit">Compiler unit of the session.</param>

        public CompilerContext(CompilerParameters parameters, CompileUnit compileUnit)
        {
            Parameters  = parameters;
            CompileUnit = compileUnit;
            Properties  = new Hashtable();
            Errors      = new SortedSet <CompilerError>();
        }
Beispiel #17
0
        public static CompileUnit ParseReader(int tabSize, string readerName, TextReader reader)
        {
            CompileUnit cu = new CompileUnit();

            ParseModule(tabSize, cu, readerName, reader, null);
            return(cu);
        }
Beispiel #18
0
        override public void Run()
        {
            CompileUnit cu = Context.CompileUnit;

            new XmlSerializer(cu.GetType()).Serialize(OutputWriter, cu);
            Console.WriteLine();
        }
Beispiel #19
0
        public void La()
        {
            string templateStr = @"
#<laconf>
  compiler
  {
     base-class-name=""NFX.NUnit.Templatization.TeztTemplate""
     namespace=""TestWebApp.Templates""
     summary=""Test master page""
    
    using {ns=""NFX.Web"" }
    using {ns=""NFX.RecordModel"" }
    using {ns=""BusinessLogic"" }

    attribute {decl=""BusinessLogic.SultanPermission(4)"" }
   
   }   
#</laconf>";

            TemplateStringContentSource templateSrc = new TemplateStringContentSource(templateStr);

            TextCSTemplateCompiler compiler = new TextCSTemplateCompiler(templateSrc);

            compiler.Compile();

            Assert.AreEqual(1, compiler.Count());

            CompileUnit unit = compiler.First();

            Assert.IsNull(unit.CompilationException);
            Assert.IsNull(unit.CompiledTemplateType);
            Assert.IsNotNullOrEmpty(unit.CompiledTemplateTypeName);
            Assert.AreSame(templateSrc, unit.TemplateSource);
            Assert.AreEqual(templateStr, templateSrc.Content);
        }
Beispiel #20
0
        protected IEnumerator BuildCompileUnits_Internal(BuildTarget target, DirectoryInfo sourceDir)
        {
            ClearCompileUnits();

            var c = kernels.Count;
            var s = 1f / Mathf.Max(1, c - 1);

            for (var i = 0; i < c; i++)
            {
                var kernel = kernels[i];

                progress.SetNormalizedProgress(s * i, "Building compile units {0:D3} / {1:D3}", i + 1, c);

                var compileOptions = ShaderAnalysisUtils.DefaultCompileOptions(kernel.defines, kernel.name, sourceDir);
                compileOptions.defines.Add(ShaderAnalysisUtils.DefineCompute);


                compileOptions.defines.Add(ShaderAnalysisUtils.DefineCompute);

                var unit = new CompileUnit
                {
                    sourceCodeFile = sourceCodeFile,
                    compileOptions = compileOptions,
                    compileProfile = ShaderProfile.ComputeProgram,
                    compileTarget  = ShaderTarget.CS_5,
                    compiledFile   = ShaderAnalysisUtils.GetTemporaryProgramCompiledFile(sourceCodeFile, temporaryDirectory, kernel.name)
                };

                AddCompileUnit(unit);

                yield return(null);
            }
        }
Beispiel #21
0
        public void NotAbstract()
        {
            const string CLASS_NAMESPACE     = "NFX.NUnit.Templatization";
            const string BASE_CLASS_FULLNAME = "NFX.NUnit.Templatization.TeztTemplate";

            string templateStr = @"
  #<conf><compiler 
  base-class-name=""{0}"" 
  namespace=""{1}""
  abstract=""false""
  summary=""Test master page""/>
  #</conf>".Args(BASE_CLASS_FULLNAME, CLASS_NAMESPACE);

            TemplateStringContentSource src = new TemplateStringContentSource(templateStr);

            TextCSTemplateCompiler compiler = new TextCSTemplateCompiler(src)
            {
                CompileCode = true
            };

            compiler.ReferenceAssembly(NFX_NUNIT_DLL);

            compiler.Compile();

            CompileUnit cu = compiler.First();

            Assert.IsFalse(cu.CompiledTemplateType.IsAbstract);
        }
Beispiel #22
0
        public void AutoGeneratedNamespace()
        {
            const string CLASS_NAMESPACE     = "";
            const string BASE_CLASS_FULLNAME = "NFX.NUnit.Templatization.TeztTemplate";

            string templateSrc = @"
#<conf><compiler 
base-class-name=""{0}"" 
namespace=""{1}""
abstract=""true""
summary=""Test master page""/>
#</conf>".Args(BASE_CLASS_FULLNAME, CLASS_NAMESPACE);

            TemplateStringContentSource src = new TemplateStringContentSource(templateSrc);

            TextCSTemplateCompiler compiler = new TextCSTemplateCompiler(src)
            {
                CompileCode = true
            };

            compiler.ReferenceAssembly(NFX_NUNIT_DLL);

            compiler.Compile();

            CompileUnit cu = compiler.First();

            compiler.CodeCompilerErrors.ForEach(e => Console.WriteLine(e.ToMessageWithType()));

            Assert.IsNotNullOrEmpty(cu.CompiledTemplateType.Namespace);
        }
Beispiel #23
0
        public void TestForContinue()
        {
            string compileTest = @"
module Program
{
    function main(array : int[]) : int
    {
        var val : int = 0;
        for(i in array)
        {
            if(i == 1) continue;
            val = val + i;
        }
        return val;
    }
}
";

            var lexResults   = lexer.Lex(new Document(compileTest, "")).GetAwaiter().GetResult();
            var parseResults = parser.Parse(lexResults).GetAwaiter().GetResult();

            var compileUnit    = new CompileUnit(new [] { parseResults });
            var compileResults = compileUnit.Compile();

            System.Console.WriteLine();

            foreach (var err in compileResults.Errors)
            {
                System.Console.WriteLine($"ERROR: {err.Message}");
            }

            Assert.IsTrue(compileResults.Errors.Length == 0);

            byte[] image = compileResults.Output.Serialize();
            System.Console.WriteLine($"Compiled Cozi IL image - {image.Length} byte(s)");

            ILContext context = new ILContext(image);
            CoziVM    vm      = new CoziVM(context);

            foreach (var m in context.Modules)
            {
                System.Console.WriteLine(m.ToString());
            }

            var intType     = context.GlobalTypes.GetType("int");
            var intArrayRef = vm.NewArray(intType, 6);

            vm.Pin(intArrayRef.SrcPointer);

            vm.SetElement <int>(intArrayRef, 0, 24);
            vm.SetElement <int>(intArrayRef, 1, 1);
            vm.SetElement <int>(intArrayRef, 2, 49);
            vm.SetElement <int>(intArrayRef, 3, 1);
            vm.SetElement <int>(intArrayRef, 4, 7);
            vm.SetElement <int>(intArrayRef, 5, 1);

            Assert.IsTrue(vm.BeginInvoke("Program", "main", out var ctx));
            ctx.PushArg <VMSlice>(intArrayRef, 0);
            Assert.IsTrue(ctx.Invoke <int>() == (24 + 49 + 7));
        }
Beispiel #24
0
        protected CompileUnit CompileUnitFor(TypeDefinition node)
        {
            CompileUnit unit = new CompileUnit();

            GetModuleFor(unit, node);
            return(unit);
        }
 public override void OnCompileUnit(CompileUnit node)
 {
     this._codeDomUnit = new CodeCompileUnit();
     this._defaultNS   = new CodeNamespace();
     this._codeDomUnit.Namespaces.Add(this._defaultNS);
     base.OnCompileUnit(node);
 }
Beispiel #26
0
        /// <summary>
        /// Checks the given call target for compatibility.
        /// </summary>
        /// <param name="unit">The current compilation unit.</param>
        /// <param name="target">The call target to test for compatiblity.</param>
        /// <param name="entryPoint">The entry point.</param>
        private static void CheckCall(
            CompileUnit unit,
            MethodBase target,
            EntryPoint entryPoint)
        {
            var compilationContext = unit.CompilationContext;

            if (target.IsAbstract)
            {
                throw compilationContext.GetNotSupportedException(
                          ErrorMessages.NotSupportedVirtualMethodCall, target.Name);
            }
            CodeGenerator.VerifyAccessToMethodInImplicitlyGroupedKernel(
                compilationContext,
                target,
                entryPoint);
            var declaringType = target.DeclaringType;

            if (declaringType.IsGenericType)
            {
                declaringType = declaringType.GetGenericTypeDefinition();
            }
            if (IntrinsicTypes.Contains(declaringType))
            {
                return;
            }
            if (VerifyActivatorCall(unit, target))
            {
                return;
            }
            CodeGenerator.VerifyNotRuntimeMethod(
                compilationContext,
                target);
            CheckMethod(unit, target, entryPoint);
        }
Beispiel #27
0
        public static Assembly compile(CompileUnit unit, params Assembly[] references)
        {
            CompilerContext result = compile_(unit, references);

            AssertNoErrors(result);
            return(result.GeneratedAssembly);
        }
Beispiel #28
0
        public void RunStringTestCase(string message, string expected, string actual)
        {
            CompileUnit cu = RunCompiler(new StringInput("<actual>", actual));

            cu.Modules[0].Name = BooParser.CreateModuleName("<expected>");
            AssertEquals(message, ParseString("<expected>", expected), cu);
        }
Beispiel #29
0
        public void RunTestCase(string expectedFile, string actualFile)
        {
            CompileUnit cu = RunCompiler(new FileInput(GetTestCasePath(actualFile)));

            cu.Modules[0].Name = BooParser.CreateModuleName(expectedFile);
            AssertEquals("[required]", ParseTestCase(expectedFile), cu);
        }
Beispiel #30
0
        public static CompileUnit ParseReader(ParserSettings settings, string readerName, TextReader reader)
        {
            var cu = new CompileUnit();

            ParseModule(settings, cu, readerName, reader);
            return(cu);
        }
Beispiel #31
0
        protected override void DoCompileTemplateSource(CompileUnit unit)
        {
            var text = unit.TemplateSource.GetSourceContent().ToString().Trim();
             var icname = unit.TemplateSource.InferClassName();

             Configuration conf = new MemoryConfiguration();

             var confLineCount = 0;
             if (text.StartsWith(CONFIG_START))
             {
               var i = text.IndexOf(CONFIG_END);
               if (i<CONFIG_START.Length) throw new TemplateParseException(StringConsts.TEMPLATE_CS_COMPILER_CONFIG_CLOSE_TAG_ERROR);

               var confText = text.Substring(CONFIG_START.Length, i - CONFIG_START.Length);

               confLineCount = confText.Count(c=>c=='\n');

               //cut configuration out of template
               text = text.Substring(i + CONFIG_END.Length);

               try
               {
                 conf = XMLConfiguration.CreateFromXML("<config>"+confText+"</config>");
               }
               catch(Exception error)
               {
                 throw new TemplateParseException(StringConsts.TEMPLATE_CS_COMPILER_CONFIG_ERROR + error.Message, error);
               }
             }else//20140103 DKh add Laconic support
             if (text.StartsWith(LACONFIG_START))
             {
               var i = text.IndexOf(LACONFIG_END);
               if (i<LACONFIG_START.Length) throw new TemplateParseException(StringConsts.TEMPLATE_CS_COMPILER_CONFIG_CLOSE_TAG_ERROR);

               var confText = text.Substring(LACONFIG_START.Length, i - LACONFIG_START.Length);

               confLineCount = confText.Count(c=>c=='\n');

               //cut configuration out of template
               text = text.Substring(i + LACONFIG_END.Length);

               try
               {
                 conf = LaconicConfiguration.CreateFromString("config{"+confText+"}");
               }
               catch(Exception error)
               {
                 throw new TemplateParseException(StringConsts.TEMPLATE_CS_COMPILER_CONFIG_ERROR + error.Message, error);
               }
             }

             var compilerNode = conf.Root[CONFIG_COMPILER_SECTION];

             //add referenced assemblies
             foreach(var anode in compilerNode.Children.Where(cn=> cn.IsSameName(CONFIG_REF_ASSEMBLY_SECTION)))
               this.ReferenceAssembly(anode.AttrByName(CONFIG_REF_ASSEMBLY_NAME_ATTR).Value);

             //add usings
             var usings = new HashSet<string>();

             RegisterDefaultUsings(usings);

             foreach(var unode in compilerNode.Children.Where(cn=> cn.IsSameName(CONFIG_USING_SECTION)))
               usings.Add(unode.AttrByName(CONFIG_USING_NS_ATTR).Value);

             //add attributes
             var attributes = new List<string>();
              foreach(var anode in compilerNode.Children.Where(cn=> cn.IsSameName(CONFIG_ATTRIBUTE_SECTION)))
               attributes.Add(anode.AttrByName(CONFIG_ATTRIBUTE_DECL_ATTR).Value);

             unit.CompiledSource = new FSM(){Compiler = this,
                                             Unit = unit,
                                             InferredClassName = icname,
                                             ConfigNode = conf.Root,
                                             Source = text,
                                             Usings = usings,
                                             Attributes = attributes,
                                             LineNo = confLineCount+1}.Build().ToString();
        }