Ejemplo n.º 1
0
        private void NewParameterInfo(AbcCode code, AbcInstance instance, IParameter param, int varMethod, int varParam)
        {
            var pitype = _generator.Corlib.GetType(CorlibTypeId.ParameterInfo);

            if (param.Type == null)
            {
                throw new InvalidOperationException("Parametr type is null");
            }

            var ctor = pitype.FindConstructor(0);

            if (ctor == null)
            {
                throw new InvalidOperationException(".ctor not found");
            }
            code.NewObject(ctor, () => { });
            code.SetLocal(varParam);

            code.GetLocal(varParam);
            code.PushInt(GetTypeId(param.Type));
            code.SetField(FieldId.ParameterInfo_ClassImpl);

            code.GetLocal(varParam);
            code.PushString(param.Name);
            code.SetField(FieldId.ParameterInfo_NameImpl);

            code.GetLocal(varParam);
            code.GetLocal(varMethod);
            code.SetField(FieldId.ParameterInfo_MemberImpl);

            InitCustomAttributes(code, instance, param, varParam);

            code.GetLocal(varParam);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create, initialize and put on stack PropertyInfo object
        /// </summary>
        /// <param name="code"></param>
        /// <param name="prop"></param>
        private void NewPropertyInfo(AbcCode code, AbcInstance instance, IProperty prop, int varProp)
        {
            var type = _generator.Corlib.GetType(CorlibTypeId.PropertyInfo);
            var ctor = type.FindConstructor(0);

            if (ctor == null)
            {
                throw new InvalidOperationException(".ctor not found");
            }
            code.NewObject(ctor, () => { });
            code.SetLocal(varProp);

            code.GetLocal(varProp);
            code.PushString(prop.Name);
            code.SetField(FieldId.PropertyInfo_Name);

            code.GetLocal(varProp);
            code.PushTypeId(prop.Type);
            code.SetField(FieldId.PropertyInfo_Type);

            SetAccessor(code, prop.Getter, FieldId.PropertyInfo_Getter, varProp);
            SetAccessor(code, prop.Setter, FieldId.PropertyInfo_Setter, varProp);

            InitCustomAttributes(code, instance, prop, varProp);

            code.GetLocal(varProp);
        }
Ejemplo n.º 3
0
        public void DelayCalls(AbcCode code, IList <AbcInstance> list, int arr)
        {
            int vf = arr + 1;

            code.GetLocal(arr);
            int n = list.Count;

            for (int i = 0; i < n; ++i)
            {
                var instance = list[i];

                code.PushNativeBool(false);                 //delayed
                code.SetLocal(vf);

                GetCalledFlag(code, instance);
                var called = code.IfTrue();

                SetCalledFlag(code, instance, true);

                code.PushNativeBool(true);
                code.SetLocal(vf);

                called.BranchTarget = code.Label();

                code.GetLocal(arr);
                code.GetLocal(vf);
                code.CallAS3("push", 1);
            }
        }
Ejemplo n.º 4
0
 public static void set_Item(IMethod method, AbcCode code)
 {
     code.LoadThis();
     code.GetLocal(1);
     code.GetLocal(2);
     code.SetArrayElem(true);
     code.ReturnVoid();
 }
Ejemplo n.º 5
0
 public static void CopyTo(IMethod method, AbcCode code)
 {
     code.LoadThis();
     code.GetLocal(1);
     code.GetLocal(2);
     code.Call(ArrayMethodId.CopyTo);
     code.ReturnVoid();
 }
Ejemplo n.º 6
0
        private IEnumerable <IInstruction> LoadLocal(int index)
        {
            var code = new AbcCode(_abc);

            code.GetLocal(index);
            return(code);
        }
Ejemplo n.º 7
0
        private static void TryUnboxNumber(AbcCode code, IType type)
        {
            const int varValue = 1;

            code.GetLocal(varValue);
            var ifNotNumber = code.IfType(AvmTypeCode.Number);

            code.ThrowInvalidCastException();
            ifNotNumber.BranchTarget = code.Label();
            code.GetLocal(varValue);
            if (!code.TryCastToSystemType(null, type.SystemType()))
            {
                code.Coerce(type, true);
            }
            code.ReturnValue();
        }
Ejemplo n.º 8
0
        private void InitProperties(AbcCode code, AbcInstance instance, IType type, int varObj)
        {
            var propertiesInitializer = DefinePropertiesInitializer(instance, type);

            code.GetLocal(varObj);
            code.GetStaticFunction(propertiesInitializer);
            code.SetProperty(Const.Type.PropertiesInit);
        }
Ejemplo n.º 9
0
        private void InitCustomAttributes(AbcCode code, AbcInstance instance, ICustomAttributeProvider provider, int var)
        {
            var init = DefineCustomAttributesInitializer(instance, provider);

            code.GetLocal(var);
            code.GetStaticFunction(init);
            code.SetProperty(Const.MemberInfo.CustomAttrsInit);
        }
Ejemplo n.º 10
0
        private void InitMethods(AbcCode code, AbcInstance instance, IType type, int var)
        {
            var init = DefineMethodsInitializer(instance, type, false);

            code.GetLocal(var);
            code.GetStaticFunction(init);
            code.SetProperty(Const.Type.MethodsInit);
        }
Ejemplo n.º 11
0
        public static void get_Item(IMethod method, AbcCode code)
        {
            var type = method.DeclaringType.GetTypeArgument(0);

            code.LoadThis();
            code.GetLocal(1);
            code.GetArrayElem(type, true);
            code.ReturnValue();
        }
Ejemplo n.º 12
0
 private static void CreateInstance(AbcCode code, AbcInstance instance, int argLocal)
 {
     code.CreateInstance(instance,
                         () =>
     {
         code.GetLocal(argLocal);
         return(1);
     });
 }
Ejemplo n.º 13
0
        private void InitConstructors(AbcCode code, AbcInstance instance, IType type, int var)
        {
            // TODO: problem is here
            var init = DefineMethodsInitializer(instance, type, true);

            code.GetLocal(var);
            code.GetStaticFunction(init);
            code.SetProperty(Const.Type.ConstructorsInit);
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Loads value from temporary variable onto the stack
 /// </summary>
 /// <param name="var"></param>
 /// <returns></returns>
 public IEnumerable <IInstruction> GetTempVar(int var)
 {
     if (var >= 0)
     {
         var code = new AbcCode(_abc);
         code.GetLocal(var);
         return(code);
     }
     return(null);
 }
Ejemplo n.º 15
0
        public IEnumerable <IInstruction> Rethrow(ISehBlock block)
        {
            var ci   = block.GetCatchInfo();
            var code = new AbcCode(_abc);

            code.GetLocal(ci.ExceptionVar);
            KillExceptionVariable(code, ci);
            code.Throw();
            return(code);
        }
Ejemplo n.º 16
0
        private void InitTypeFuncs(AbcCode code, IType type, AbcInstance instance)
        {
            var f = _generator.Boxing.Box(type);

            if (f != null)
            {
                code.GetLocal(varType);
                code.GetStaticFunction(f);
                code.SetProperty(Const.Type.BoxFunction);
            }

            f = _generator.Boxing.Unbox(type, false);
            if (f != null)
            {
                code.GetLocal(varType);
                code.GetStaticFunction(f);
                code.SetProperty(Const.Type.UnboxFunction);
            }

            f = CopyImpl.StaticCopy(instance);
            if (f != null)
            {
                code.GetLocal(varType);
                code.GetStaticFunction(f);
                code.SetProperty(Const.Type.CopyFunction);
            }

            var ctor = type.FindParameterlessConstructor();

            if (ctor != null)
            {
                f = _generator.MethodBuilder.Build(ctor) as AbcMethod;
                if (f != null && !f.IsInitializer)
                {
                    f = _generator.TypeBuilder.DefineCtorStaticCall(ctor);
                    Debug.Assert(f != null);
                    code.GetLocal(varType);
                    code.GetStaticFunction(f);
                    code.SetProperty(Const.Type.CreateFunction);
                }
            }
        }
Ejemplo n.º 17
0
 private void NewAttribute(AbcCode code, ICustomAttribute attr, int varAttr)
 {
     code.NewObject(attr.Constructor,
                    () =>
     {
         foreach (var arg in attr.FixedArguments())
         {
             code.PushValue(code, arg.Value);
         }
     });
     code.SetLocal(varAttr);
     //TODO: Set fields and properties
     foreach (var arg in attr.NamedArguments())
     {
         code.GetLocal(varAttr);
         code.PushValue(code, arg.Value);
         if (arg.Kind == ArgumentKind.Field)
         {
             var field = arg.Member as IField;
             if (field == null)
             {
                 throw new InvalidOperationException();
             }
             code.SetField(field);
         }
         else
         {
             var prop = arg.Member as IProperty;
             if (prop == null)
             {
                 throw new InvalidOperationException();
             }
             var s = _generator.MethodBuilder.BuildAbcMethod(prop.Setter);
             code.Call(s);
         }
     }
     code.GetLocal(varAttr);
 }
Ejemplo n.º 18
0
        private IEnumerable <IInstruction> EndFinally(ISehHandlerBlock block, bool fault)
        {
            var handlerInfo = block.GetHandlerInfo();
            var ci          = handlerInfo.CatchInfo;
            var fi          = handlerInfo.FinallyInfo;

            if (fi.IsFault != fault)
            {
                throw new InvalidOperationException("Finally block type mistmatch!");
            }

            var code = new AbcCode(_abc);

            if (fault)
            {
                code.GetLocal(ci.ExceptionVar);
                KillExceptionVariable(code, ci);
                code.Throw();
            }
            else
            {
                // check if we should rethrow exception
                code.GetLocal(fi.RethrowFlagVariable);
                // trying to fix IVDiffGramTest
                // KillTempVar(code, fi.RethrowFlagVariable);
                var br = code.IfFalse();

                code.GetLocal(ci.ExceptionVar);
                KillExceptionVariable(code, ci);
                var end = code.Throw();

                br.GotoNext(end);
            }

            return(code);
        }
Ejemplo n.º 19
0
        private void NewMethodInfo(AbcCode code, AbcInstance instance, IMethod method,
                                   int varMethod, int varParams, int varParam,
                                   IType mtype, int index)
        {
            var ctor = mtype.FindConstructor(0);

            if (ctor == null)
            {
                throw new InvalidOperationException(".ctor not found");
            }

            var abcMethod = method.AbcMethod();

            if (abcMethod == null)
            {
                throw new InvalidOperationException();
            }
            abcMethod.MethodInfoIndex = index;

            code.NewObject(ctor, () => { });
            code.SetLocal(varMethod);

            code.GetLocal(varMethod);
            code.PushString(method.Name);
            code.SetField(FieldId.MethodBase_Name);

            code.GetLocal(varMethod);
            var wrapper = DefineMetodWrapper(method, true);

            code.GetStaticFunction(wrapper);
            code.SetField(FieldId.MethodBase_Function);

            if (method.IsConstructor)
            {
                code.GetLocal(varMethod);
                wrapper = DefineMetodWrapper(method, false);
                code.GetStaticFunction(wrapper);
                code.SetField(FieldId.ConstructorInfo_CreateFunction);
            }

            var mattrs = (int)GetMethodAttributes(method);

            code.GetLocal(varMethod);
            code.PushInt(mattrs);
            code.SetField(FieldId.MethodBase_Attributes);

            code.GetLocal(varMethod);
            code.NewArray(varParams, mtype, method.Parameters,
                          param => NewParameterInfo(code, instance, param, varMethod, varParam));
            code.SetField(FieldId.MethodBase_Parameters);

            InitCustomAttributes(code, instance, method, varMethod);

            code.GetLocal(varMethod);
        }
Ejemplo n.º 20
0
        private static void SetAccessor(AbcCode code, IMethod accessor, FieldId fieldId, int varProp)
        {
            if (accessor == null)
            {
                return;
            }
            var abcMethod = accessor.AbcMethod();

            if (abcMethod == null)
            {
                return;
            }
            int index = abcMethod.MethodInfoIndex;

            code.GetLocal(varProp);
            code.PushInt(index);
            code.SetField(fieldId);
        }
Ejemplo n.º 21
0
        void CallToException(AbcCode code, int var)
        {
            var avmErrors = Assembly.Corlib().FindType("AvmErrors");

            if (avmErrors == null)
            {
                throw new InvalidOperationException(string.Format("Unable to find AvmErrors. Invalid corlib."));
            }

            EnsureType(avmErrors);

            var fromError = avmErrors.Methods.Find("ExceptionFromError", 1);
            var m         = DefineAbcMethod(fromError);

            code.Getlex(m);
            code.GetLocal(var);
            code.Call(m);
        }
Ejemplo n.º 22
0
        public void UndelayCalls(AbcCode code, IList <AbcInstance> list, int arr)
        {
            int n = list.Count;

            for (int i = 0; i < n; ++i)
            {
                var instance = list[i];

                code.GetLocal(arr);
                code.PushInt(i);
                code.GetNativeArrayItem();
                var br = code.IfFalse();

                SetCalledFlag(code, instance, false);

                br.BranchTarget = code.Label();
            }
        }
Ejemplo n.º 23
0
        private void InitFields(AbcCode code, AbcInstance instance, IType type)
        {
            if (!MustInitFields)
            {
                return;
            }
            if (type.IsInterface)
            {
                return;
            }
            var init = DefineMyFieldsInitializer(instance, type);

            if (init != null)
            {
                code.GetLocal(varType);
                code.GetStaticFunction(init);
                code.SetProperty(Const.Type.MyFieldsInit);
            }
        }
Ejemplo n.º 24
0
        private void NewFieldInfo(AbcCode code, AbcInstance instance, IField field, int varField)
        {
            var trait = field.Data as AbcTrait;

            if (trait == null)
            {
                code.PushNull();
                return;
            }

            var name = trait.Name;

            var fieldInfo = _generator.Corlib.GetInstance(CorlibTypeId.FieldInfo);

            code.CreateInstance(fieldInfo);
            code.SetLocal(varField);

            var ns = name.Namespace;

            code.GetLocal(varField);
            code.PushNamespace(ns);
            code.SetField(FieldId.FieldInfo_Namespace);

            code.GetLocal(varField);
            code.PushString(field.Name);
            code.SetField(FieldId.FieldInfo_Name);

            int typeIndex = GetTypeId(field.Type);

            code.GetLocal(varField);
            code.PushInt(typeIndex);
            code.SetField(FieldId.FieldInfo_Type);

            typeIndex = GetTypeId(field.DeclaringType);
            code.GetLocal(varField);
            code.PushInt(typeIndex);
            code.SetField(FieldId.FieldInfo_DeclType);

            code.GetLocal(varField);
            code.PushBool(field.IsStatic);
            code.SetField(FieldId.FieldInfo_IsStatic);

            if (GlobalSettings.ReflectionSupport)
            {
                InitCustomAttributes(code, instance, field, varField);
            }

            code.GetLocal(varField);
        }
Ejemplo n.º 25
0
        private void RunTest(AbcCode code, IMethod test)
        {
            var testFixture = test.DeclaringType;

            var       testType     = GetType(NUnitTypeId.Test);
            var       testInstance = GetInstance(NUnitTypeId.Test);
            const int varTest      = 1;

            code.CreateInstance(testInstance);
            code.SetLocal(varTest);

            code.GetLocal(varTest);
            code.PushString(test.FullName);
            code.SetProperty(testType, "Name");

            code.GetLocal(varTest);
            code.PushString(testFixture.FullName);
            code.SetProperty(testType, "SuiteName");

            string desc = test.GetTestDescription();

            if (!string.IsNullOrEmpty(desc))
            {
                code.GetLocal(varTest);
                code.PushString(desc);
                code.SetProperty(testType, "Description");
            }

            desc = testFixture.GetTestDescription();
            if (!string.IsNullOrEmpty(desc))
            {
                code.GetLocal(varTest);
                code.PushString(desc);
                code.SetProperty(testType, "SuiteDescription");
            }

            var func = DefineTestRunner(test);

            code.GetLocal(varTest);
            code.GetStaticFunction(func);
            //code.CallSetter(testType, "Func");
            code.SetField(testType, "Func");

            //register test in FlashTestRunner
            code.CallStatic(GetMethod(NUnitMethodId.TestRunner_Register), () => code.GetLocal(varTest));
        }
Ejemplo n.º 26
0
 private void InitFields(AbcCode code, IType type, IType elemType, int varArray)
 {
     InitFields(code, type, elemType, () => code.GetLocal(varArray));
 }
Ejemplo n.º 27
0
        private AbcMethod BuildCtorImpl(IMethod method, AbcInstance instance)
        {
            if (!method.IsConstructor)
            {
                return(null);
            }
            if (method.IsStatic)
            {
                return(null);
            }
            var type = method.DeclaringType;

            if (!type.IsArray)
            {
                return(null);
            }

            var ctor = new AbcMethod
            {
                ReturnType = Abc.BuiltinTypes.Void
            };

            _generator.MethodBuilder.BuildParameters(ctor, method);

            string name1 = "arrctor_" + type.GetSigName();
            var    name  = Abc.DefineName(QName.Global(name1));
            var    trait = AbcTrait.CreateMethod(ctor, name);

            instance.Traits.Add(trait);

            var body = new AbcMethodBody(ctor);

            Abc.AddMethod(ctor);

            var code = new AbcCode(Abc);

            code.PushThisScope();
            code.ConstructSuper();

            //check arguments
            int n = method.Parameters.Count;

            for (int i = 0; i < n; ++i)
            {
                code.GetLocal(i + 1);
                code.PushInt(0);
                var br            = code.If(BranchOperator.GreaterThanOrEqual);
                var exceptionType = _generator.Corlib.GetType(CorlibTypeId.ArgumentOutOfRangeException);
                code.ThrowException(exceptionType);
                br.BranchTarget = code.Label();
            }

            //m_rank = n
            code.LoadThis();
            code.PushInt(n);
            code.SetProperty(Const.Array.Rank);

            int varSize = n + 1;

            for (int i = 0; i < n; ++i)
            {
                code.GetLocal(i + 1);
            }
            for (int i = 1; i < n; ++i)
            {
                code.Add(InstructionCode.Multiply_i);
            }
            code.SetLocal(varSize);

            //init m_value
            code.LoadThis();
            code.CreateArrayVarSize(varSize);
            code.SetProperty(Const.Array.Value);

            //init m_lengths
            code.LoadThis();
            for (int i = 0; i < n; ++i)
            {
                code.GetLocal(i + 1);
            }
            code.Add(InstructionCode.Newarray, n);
            code.SetProperty(Const.Array.Lengths);

            int varDimArr = varSize + 1;

            //init m_dims
            code.CreateArray(n - 1);
            code.SetLocal(varDimArr);

            //1, n, n * (n-1), ..., n * (n-1) * ... * n0
            for (int i = n - 2; i >= 0; --i)
            {
                int leni = i + 2;
                code.GetLocal(varDimArr);
                code.PushInt(i);

                if (i != n - 2)
                {
                    code.GetLocal(varDimArr);
                    code.PushInt(i + 1);
                    code.GetNativeArrayItem();
                    code.CoerceInt32();                     //prev

                    code.GetLocal(leni);
                    code.Add(InstructionCode.Multiply_i);                     //prev * leni
                }
                else
                {
                    code.GetLocal(leni);
                }

                code.SetNativeArrayItem();
            }

            code.LoadThis();
            code.GetLocal(varDimArr);
            code.SetProperty(Const.Array.Dims);

            var elemType = type.GetElementType();

            InitFields(code, type, elemType, 0);

            if (InternalTypeExtensions.IsInitArray(elemType))
            {
                code.InitArray(elemType,
                               () =>
                {
                    code.LoadThis();
                    code.GetProperty(Const.Array.Value);
                }, varSize);
            }

            code.ReturnVoid();

            body.Finish(code);

            return(ctor);
        }
Ejemplo n.º 28
0
        private void InitPointers(AbcCode code)
        {
            if (!HasActivationVar)
            {
                return;
            }

            if (AbcGenConfig.UseActivationTraits)
            {
                code.NewActivation();
                if (AbcGenConfig.UseFuncPointers)
                {
                    code.Dup();
                    code.PushThisScope();
                    code.PushScope();
                }
            }
            else
            {
                code.CreateInstance(_activation);
            }
            code.SetLocal(_activationVar);

            if (IsThisAddressed)
            {
                var ptr = DefineThisPtr();
                GetActivation(code);
                code.LoadThis();
                code.SetSlot(ptr.Slot);
                InitSlotPtr(code, ptr);
            }

            //store arguments in slots
            int n = _method.Parameters.Count;

            for (int i = 0; i < n; ++i)
            {
                var p   = _method.Parameters[i];
                var ptr = p.Data as VarPtr;
                if (ptr == null)
                {
                    continue;
                }

                GetActivation(code);
                code.GetLocal(GetArgIndex(i));
                code.SetSlot(ptr.Slot);

                InitSlotPtr(code, ptr);
            }

            if (HasLocalVariables)
            {
                n = VarCount;
                for (int i = 0; i < n; ++i)
                {
                    var v   = GetVar(i);
                    var ptr = v.Data as VarPtr;
                    if (ptr == null)
                    {
                        continue;
                    }
                    InitSlotPtr(code, ptr);
                }
            }

            //NOTE: Because of VerifyError #1068
            if (AbcGenConfig.UseActivationTraits && AbcGenConfig.UseFuncPointers)
            {
                code.PopScope(); //activation
                code.PopScope(); //this
            }
        }
Ejemplo n.º 29
0
 private void GetActivation(AbcCode code)
 {
     code.GetLocal(_activationVar);
 }
Ejemplo n.º 30
0
        void RouteException(AbcCode code, ISehHandlerBlock block, int var)
        {
            var exceptionType = block.ExceptionType;

            if (block.PrevHandler == null)
            {
                //if err is AVM error then we translate it to System.Exception.
                //code.GetLocal(var);
                //code.As(AvmTypeCode.Error);
                //code.PushNull();
                //var ifNotError = code.IfEquals();

                code.GetLocal(var);
                code.As(SystemTypes.Exception, true);
                code.PushNull();
                var ifExc = code.IfNotEquals();

                code.GetLocal(var);
                code.As(AvmTypeCode.Error);
                code.PushNull();
                var ifNotError = code.IfEquals();

                CallToException(code, var);
                code.CoerceAnyType();
                code.SetLocal(var);

                //check my exception
                var labelNotError = code.Label();
                ifExc.BranchTarget      = labelNotError;
                ifNotError.BranchTarget = labelNotError;
            }

            code.GetLocal(var);

            var handlerInfo = (SehHandlerInfo)block.Tag;

            handlerInfo.CheckExceptionLabel = code.Label();
            //NOTE: Exception on stack can be routed from previous handlers
            code.SetLocal(var);
            code.GetLocal(var);
            code.As(exceptionType, true);
            code.PushNull();
            var ifMyException = code.IfNotEquals();

            //Routing to another exception handler or rethrow
            //Instruction routing = Label();
            if (block.NextHandler == null)
            {
                code.GetLocal(var);
                code.Throw();
            }
            else
            {
                code.GetLocal(var);
                handlerInfo.JumpToNextHandler = code.Goto();
            }

            //Normal Execution: Prepare stack for handler
            var normal = code.Label();

            ifMyException.BranchTarget = normal;

            code.GetLocal(var);
            code.Coerce(exceptionType, true);

            //21 instructions for first handler
            //11 instructions for other handlers
        }