Пример #1
0
        public CompilerState
            (PFunction source, Engine targetEngine, ILGenerator il, CompilerPass pass,
                FunctionLinking linking)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (targetEngine == null)
                throw new ArgumentNullException("targetEngine");
            if (il == null)
                throw new ArgumentNullException("il");

            _source = source;
            _linking = linking;
            _pass = pass;
            _targetEngine = targetEngine;
            _il = il;
            _indexMap = new Dictionary<int, string>();
            _instructionLabels = new Label[Source.Code.Count + 1];
            for (var i = 0; i < InstructionLabels.Length; i++)
                InstructionLabels[i] = il.DefineLabel();
            _returnLabel = il.DefineLabel();
            _symbols = new SymbolTable<CilSymbol>();
            _tryBlocks = new Stack<CompiledTryCatchFinallyBlock>();

            MetaEntry cilHints;
            _foreachHints = new List<ForeachHint>();
            _cilExtensionOffsets = new Queue<int>();
            if (source.Meta.TryGetValue(Loader.CilHintsKey, out cilHints))
            {
                SortedSet<int> cilExtensionOffsets = null;
                foreach (var entry in cilHints.List)
                {
                    var hint = entry.List;
                    if (hint.Length < 1)
                        continue;
                    switch (hint[0].Text)
                    {
                        case CilExtensionHint.Key:
                            if (cilExtensionOffsets == null)
                                cilExtensionOffsets = new SortedSet<int>();
                            var cilExt = CilExtensionHint.FromMetaEntry(hint);
                            foreach (var offset in cilExt.Offsets)
                                cilExtensionOffsets.Add(offset);
                            break;
                        case ForeachHint.Key:
                            _ForeachHints.Add(ForeachHint.FromMetaEntry(hint));
                            break;
                    }
                }
                if (cilExtensionOffsets != null)
                {
                    foreach (var offset in cilExtensionOffsets)
                        _cilExtensionOffsets.Enqueue(offset);
                }
            }

            _seh = new StructuredExceptionHandling(this);
            _stackSize = new int[source.Code.Count];
        }
Пример #2
0
        private static void _compile
            (PFunction source, ILGenerator il, Engine targetEngine, CompilerPass pass,
                FunctionLinking linking)
        {
            var state = new CompilerState(source, targetEngine, il, pass, linking);

            //Every cil implementation needs to instantiate a CilFunctionContext and assign PValue.Null to the result.
            _emitCilImplementationHeader(state);

            //Reads the functions metadata about parameters, local variables and shared variables.
            //initializes shared variables.
            _buildSymbolTable(state);

            //CODE ANALYSIS
            //  - determine number of temporary variables
            //  - find variable references (alters the symbol table)
            //  - determine stack size at all offsets
            _analysisAndPreparation(state);

            //Create and initialize local variables for parameters
            _parseParameters(state);

            //Shared variables and parameters have already been initialized
            // this method initializes (PValue.Null) the rest.
            _createAndInitializeRemainingLocals(state);

            //Emits IL for the functions Prexonite byte code.
            _emitInstructions(state);
        }
Пример #3
0
        public static void StoreDebugImplementation(PFunction func, Engine targetEngine)
        {
            const FunctionLinking linking = FunctionLinking.FullyStatic;
            var pass = new CompilerPass(linking);

            var m = pass.DefineImplementationMethod(func.Id);

            var il = CompilerPass.GetIlGenerator(m);

            _compile(func, il, targetEngine, pass, linking);

            pass.TargetType.CreateType();

            //var sm = tb.DefineMethod("whoop", MethodAttributes.Static | MethodAttributes.Public);

            //ab.SetEntryPoint(sm);
            pass.Assembly.Save(pass.Assembly.GetName().Name + ".dll");
        }
Пример #4
0
        public static void StoreDebugImplementation(Application app, Engine targetEngine)
        {
            _checkQualification(app.Functions, targetEngine);

            const FunctionLinking linking = FunctionLinking.FullyStatic;
            var pass = new CompilerPass(linking);

            var qfuncs = new List<PFunction>();
            foreach (var func in app.Functions)
            {
                if (!func.Meta.GetDefault(PFunction.VolatileKey, false))
                {
                    qfuncs.Add(func);
                    pass.DefineImplementationMethod(func.Id);
                }
            }

            foreach (var func in qfuncs)
            {
                _compile(func, pass.GetIlGenerator(func.Id), targetEngine, pass, linking);
            }

            pass.TargetType.CreateType();

            pass.Assembly.Save(pass.Assembly.GetName().Name + ".dll");
        }
Пример #5
0
        public static bool TryCompile(PFunction func, Engine targetEngine, FunctionLinking linking)
        {
            if (_checkQualification(func, targetEngine))
            {
                var pass = new CompilerPass(func.ParentApplication, linking);

                var m = pass.DefineImplementationMethod(func.Id);
                var il = CompilerPass.GetIlGenerator(m);

                _compile(func, il, targetEngine, pass, linking);

                func.Declaration.CilImplementation = pass.GetDelegate(m);
                pass.LinkMetadata(func);

                return true;
            }
            return false;
        }
Пример #6
0
        public static void Compile(IEnumerable<PFunction> functions, Engine targetEngine,
            FunctionLinking linking)
        {
            _checkQualification(functions, targetEngine);

            var qfuncs = new List<PFunction>();

            //Get a list of qualifying functions
            foreach (var func in functions)
                if (!func.Meta.GetDefault(PFunction.VolatileKey, false))
                    qfuncs.Add(func);

            if (qfuncs.Count == 0)
                return; //No compilation to be done

            var pass = new CompilerPass(linking);

            //Generate method stubs
            foreach (var func in qfuncs)
                pass.DefineImplementationMethod(func.Id);

            //Emit IL
            foreach (var func in qfuncs)
            {
                _compile(func, CompilerPass.GetIlGenerator(pass.Implementations[func.Id]),
                    targetEngine, pass, linking);
            }

            //Enable by name linking and link meta data to CIL implementations
            foreach (var func in qfuncs)
            {
                func.Declaration.CilImplementation = pass.GetDelegate(func.Id);
                pass.LinkMetadata(func);
            }
        }