Beispiel #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];
        }
Beispiel #2
0
 public CompilerPass(Application app, FunctionLinking linking)
     : this(
         app,
         (linking & FunctionLinking.AvailableForLinking) ==
             FunctionLinking.AvailableForLinking)
 {
 }
Beispiel #3
0
        public static void Compile(StackContext sctx, List<PValue> lst, FunctionLinking linking)
        {
            if (lst == null)
                throw new ArgumentNullException("lst");
            if (sctx == null)
                throw new ArgumentNullException("sctx");
            var functions = new List<PFunction>();
            foreach (var value in lst)
            {
                if (value == null)
                    continue;
                var T = value.Type.ToBuiltIn();
                PFunction func;
                switch (T)
                {
                    case PType.BuiltIn.String:
                        if (
                            !sctx.ParentApplication.Functions.TryGetValue((string) value.Value,
                                out func))
                            continue;
                        break;
                    case PType.BuiltIn.Object:
                        if (!value.TryConvertTo(sctx, false, out func))
                            continue;
                        break;
                    default:
                        continue;
                }
                functions.Add(func);
            }

            Compile(functions, sctx.ParentEngine, linking);
        }
Beispiel #4
0
 public CompilerPass(FunctionLinking linking)
     : this(null, linking)
 {
 }
Beispiel #5
0
 public static void Compile(StackContext sctx, Application app, FunctionLinking linking)
 {
     if (sctx == null)
         throw new ArgumentNullException("sctx");
     Compile(app, sctx.ParentEngine, linking);
 }
Beispiel #6
0
 public static void Compile(Application app, Engine targetEngine, FunctionLinking linking)
 {
     if (app == null)
         throw new ArgumentNullException("app");
     Compile(app.Functions, targetEngine, linking);
 }
Beispiel #7
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);
        }
Beispiel #8
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;
        }
Beispiel #9
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);
            }
        }