Esempio n. 1
0
 public virtual Instruction GetInstruction(LightCompiler compiler) {
     compiler.Compile(_context);
     if (_isLocal) {
         return new LookupNameInstruction(_name);
     } else {
         return new LookupGlobalNameInstruction(_name);
     }
 }
 public void AddInstructions(LightCompiler compiler) {
     compiler.Compile(_target);
     switch (Type.GetTypeCode(_binder.Type)) {
         case TypeCode.Boolean:
             compiler.Instructions.Emit(BooleanConversionInstruction.Instance);
             break;
         default:
             compiler.Instructions.Emit(new TypedConversionInstruction(_binder.Type));
             break;
     }
 }
Esempio n. 3
0
 private void EnsureCompiled(bool optimized) {
     //TODO too much duplicated code between these two blocks
     if (optimized) {
         if (_optimizedCode != null) return;
         var rewriter = new LightGlobalRewriter();
         var newLambda = rewriter.RewriteLambda(Code, Code.Name, LanguageContext, optimized);
         _optimizedScope = rewriter.Scope;
         var compiler = new LightCompiler();
         var interpreter = compiler.CompileTop(newLambda);
         _optimizedCode = new LightLambda(interpreter);
     } else {
         if (_code != null) return;
         var rewriter = new LightGlobalRewriter();
         var newLambda = rewriter.RewriteLambda(Code, Code.Name, LanguageContext, optimized);
         var compiler = new LightCompiler();
         var interpreter = compiler.CompileTop(newLambda);
         _code = new LightLambda(interpreter);
     }
 }
        public void AddInstructions(LightCompiler compiler) {
            Instruction instr = DynamicInstructionN.CreateUntypedInstruction(_binder, ArgumentCount);
            if (instr == null) {
                var lightBinder = _binder as ILightCallSiteBinder;
                if (lightBinder == null || !lightBinder.AcceptsArgumentArray) {
                    compiler.Compile(Reduce());
                    return;
                }

                Debug.Assert(Type == typeof(object));
                instr = new DynamicSplatInstruction(ArgumentCount, CallSite<Func<CallSite, ArgumentArray, object>>.Create(_binder));
            }

            for (int i = 0; i < ArgumentCount; i++) {
                compiler.Compile(GetArgument(i));
            }

            compiler.Instructions.Emit(instr);
        }
 public override void AddInstructions(LightCompiler compiler)
 {
     if (Argument0.Type == typeof(CodeContext))
     {
         compiler.Compile(Argument0);
         compiler.Compile(Argument1);
         compiler.Instructions.EmitDynamic<CodeContext, object, object>(Binder);
     }
     else if (Argument1.Type == typeof(CodeContext))
     {
         // GetMember sites
         compiler.Compile(Argument0);
         compiler.Compile(Argument1);
         compiler.Instructions.EmitDynamic<object, CodeContext, object>(Binder);
     }
     else
     {
         base.AddInstructions(compiler);
     }
 }
Esempio n. 6
0
 internal override ParameterExpression GetExpression(LightCompiler compiler) {
     return compiler.ClosureVariables[_index];
 }
Esempio n. 7
0
 public override string ToString(LightCompiler compiler) {
     return InstructionName + "(" + GetExpression(compiler).Name + ": " + _index + ")";
 }
Esempio n. 8
0
        void IInstructionProvider.AddInstructions(LightCompiler compiler) {
            if (NeedComparisonTransformation()) {
                // chained comparisons aren't supported for optimized light compiling
                compiler.Compile(Reduce());
                return;
            }

            switch (_op) {
                case PythonOperator.Is:
                    compiler.Compile(_left);
                    compiler.Compile(_right);
                    compiler.Instructions.Emit(IsInstruction.Instance);
                    break;
                case PythonOperator.IsNot:
                    compiler.Compile(_left);
                    compiler.Compile(_right);
                    compiler.Instructions.Emit(IsNotInstruction.Instance);
                    break;
                default:
                    compiler.Compile(Reduce());
                    break;
            }
        }
Esempio n. 9
0
 public virtual object GetDebugCookie(LightCompiler compiler) {
     return null;
 }
Esempio n. 10
0
 public void AddInstructions(LightCompiler compiler) {
     compiler.Instructions.EmitLoad(_value);
 }
 public void AddInstructions(LightCompiler compiler)
 {
     compiler.Compile(_value);
     compiler.Instructions.Emit(new TotemSetGlobalInstruction(_global.Global));
 }
Esempio n. 12
0
 public void AddInstructions(LightCompiler compiler) {
     compiler.Instructions.Emit(MakeClosureCellInstruction.Instance);
 }
Esempio n. 13
0
 public virtual Instruction GetInstruction(LightCompiler compiler) {
     return new GetGlobalInstruction(_global);
 }
Esempio n. 14
0
 void IInstructionProvider.AddInstructions(LightCompiler compiler) {
     if (_value is bool) {
         compiler.Instructions.EmitLoad((bool)_value);
     } else if (_value is UnicodeWrapper) {
         compiler.Instructions.EmitLoad(((UnicodeWrapper)_value).Value);
     } else {
         compiler.Instructions.EmitLoad(_value);
     }
 }
Esempio n. 15
0
 public override void AddInstructions(LightCompiler compiler) {
     if (Argument0.Type == typeof(CodeContext)) {
         compiler.Compile(Argument0);
         compiler.Compile(Argument1);
         compiler.Compile(Argument2);
         compiler.Compile(Argument3);
         compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object>(Binder);
         return;
     } else {
         base.AddInstructions(compiler);
     }
 }
Esempio n. 16
0
 void IInstructionProvider.AddInstructions(LightCompiler compiler) {
     // optimizing bool conversions does no good in the light compiler
     compiler.Compile(ReduceWorker(false));
 }
 void IInstructionProvider.AddInstructions(LightCompiler compiler)
 {
     compiler.Compile(_codeContextExpr);
     compiler.Instructions.Emit(new LookupGlobalInstruction(_name, _isLocal, _lightThrow));
 }
Esempio n. 18
0
 public override Instruction GetInstruction(LightCompiler compiler) {
     compiler.Compile(this._value);
     return new SetGlobalInstruction(Global);
 }
Esempio n. 19
0
 internal abstract ParameterExpression GetExpression(LightCompiler compiler);
Esempio n. 20
0
 public Label(LightCompiler compiler) {
     this._compiler = compiler;
     this._index = -1;
 }
Esempio n. 21
0
 internal override ParameterExpression GetExpression(LightCompiler compiler) {
     return compiler.Locals[_index];
 }
Esempio n. 22
0
 public override Instruction GetInstruction(LightCompiler compiler) {
     compiler.Compile(_context);
     compiler.Compile(_value);
     if (_isLocal) {
         return new SetNameInstruction(_name);
     } else {
         return new SetGlobalNameInstruction(_name);
     }
 }
Esempio n. 23
0
        public override void AddInstructions(LightCompiler compiler) {
            if (ArgumentCount > 15) {
                compiler.Compile(Reduce());
            } else if (GetArgument(0).Type == typeof(CodeContext)) {
                for (int i = 0; i < ArgumentCount; i++) {
                    compiler.Compile(GetArgument(i));
                }

                switch(ArgumentCount) {
                    case 1: compiler.Instructions.EmitDynamic<CodeContext, object>(Binder); break;
                    case 2: compiler.Instructions.EmitDynamic<CodeContext, object, object>(Binder); break;
                    case 3: compiler.Instructions.EmitDynamic<CodeContext, object, object, object>(Binder); break;
                    case 4: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object>(Binder); break;
                    case 5: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object>(Binder); break;
                    case 6: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object>(Binder); break;
                    case 7: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object>(Binder); break;
                    case 8: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object, object>(Binder); break;
                    case 9: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object, object, object>(Binder); break;
                    case 10: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object, object, object, object>(Binder); break;
                    case 11: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object, object, object, object, object>(Binder); break;
                    case 12: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object, object, object, object, object, object>(Binder); break;
                    case 13: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object, object, object, object, object, object, object>(Binder); break;
                    case 14: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object, object, object, object, object, object, object, object>(Binder); break;
                    case 15: compiler.Instructions.EmitDynamic<CodeContext, object, object, object, object, object, object, object, object, object, object, object, object, object, object, object>(Binder); break;                    
                }
            } else {
                base.AddInstructions(compiler);
            }
        }
 public void AddInstructions(LightCompiler compiler)
 {
     compiler.Compile(FunctionDefinition._functionParam);
     compiler.Instructions.Emit(GetParentContextFromFunctionInstruction.Instance);
 }
Esempio n. 25
0
        private void CompileLambdaExpression(Expression expr) {
            var node = (LambdaExpression)expr;
            var compiler = new LightCompiler(this);
            var interpreter = compiler.CompileTop(node);

            int[] closureBoxes = new int[compiler._closureVariables.Count];
            for (int i = 0; i < closureBoxes.Length; i++) {
                var closureVar = compiler._closureVariables[i];
                AddInstruction(GetBoxedVariable(closureVar));
            }
            AddInstruction(new CreateDelegateInstruction(node.Type, interpreter, compiler._closureVariables.Count));
        }
Esempio n. 26
0
        void IInstructionProvider.AddInstructions(LightCompiler compiler) {
            if (_decorators != null) {
                // decorators aren't supported, skip using the optimized instruction.
                compiler.Compile(Reduce());
                return;
            }

            // currently needed so we can later compile
            MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
            FuncCodeExpr = funcCode;

            var variable = Parent.GetVariableExpression(_variable);

            CompileAssignment(compiler, variable, CreateFunctionInstructions);
        }
Esempio n. 27
0
 private LightCompiler(LightCompiler parent) : this() {
     this._parent = parent;
 }
Esempio n. 28
0
        private void CreateFunctionInstructions(LightCompiler compiler) {
            // emit context if we have a special local context
            CodeContext globalContext = null;

            // potential optimization to avoid loading the context:
            /*if (Parent.LocalContext == PythonAst._globalContext) {
                globalContext = GlobalParent.ModuleContext.GlobalContext;
            } else*/ {
                compiler.Compile(Parent.LocalContext);
            }

            // emit name if necessary
            PythonGlobalVariableExpression name = GetVariableExpression(_nameVariable) as PythonGlobalVariableExpression;
            PythonGlobal globalName = null;
            if (name == null) {
                compiler.Compile(((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue());
            } else {
                globalName = name.Global;
            }

            // emit defaults
            int defaultCount = 0;
            for (int i = _parameters.Length - 1; i >= 0; i--) {
                var param = _parameters[i];

                if (param.DefaultValue != null) {
                    compiler.Compile(AstUtils.Convert(param.DefaultValue, typeof(object)));
                    defaultCount++;
                }
            }

            compiler.Instructions.Emit(new FunctionDefinitionInstruction(globalContext, this, defaultCount, globalName));
        }
 public void AddInstructions(LightCompiler compiler) {
     compiler.Compile(_parentContext);
     compiler.Instructions.Emit(GetGlobalContextInstruction.Instance);
 }
Esempio n. 30
0
        private static void CompileAssignment(LightCompiler compiler, MSAst.Expression variable, Action<LightCompiler> compileValue) {
            var instructions = compiler.Instructions;

            ClosureExpression closure = variable as ClosureExpression;
            if (closure != null) {
                compiler.Compile(closure.ClosureCell);
            }
            LookupGlobalVariable lookup = variable as LookupGlobalVariable;
            if (lookup != null) {
                compiler.Compile(lookup.CodeContext);
                instructions.EmitLoad(lookup.Name);
            }

            compileValue(compiler);

            if (closure != null) {
                instructions.EmitStoreField(ClosureExpression._cellField);
                return;
            }
            if (lookup != null) {
                instructions.EmitCall(typeof(PythonOps).GetMethod(lookup.IsLocal ? "SetLocal" : "SetGlobal"));
                return;
            }

            MSAst.ParameterExpression functionValueParam = variable as MSAst.ParameterExpression;
            if (functionValueParam != null) {
                instructions.EmitStoreLocal(compiler.GetVariableIndex(functionValueParam));
                return;
            }

            var globalVar = variable as PythonGlobalVariableExpression;
            if (globalVar != null) {
                instructions.Emit(new PythonSetGlobalInstruction(globalVar.Global));
                instructions.EmitPop();
                return;
            }
            Debug.Assert(false, "Unsupported variable type for light compiling function");
        }