Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="axScript3.NetFunction"/> class.
 /// </summary>
 /// <param name='function'>
 /// Function.
 /// </param>
 /// <param name='target'>
 /// Owner of function, leave null if static function.
 /// </param>
 public NetFunction(MethodBase function, object target = null)
 {
     Method = function;
     Inputs = function.GetParameters();
     Output = function.GetType();
     Target = target;
     HasParamArray = Inputs.Length > 0 && Inputs[Inputs.Length - 1].GetCustomAttributes(typeof (ParamArrayAttribute), false).Length > 0;
 }
Esempio n. 2
0
        private static Function EmitFunction(Module module, MethodBase method)
        {
            var methodInfo = method as MethodInfo;
            var methodConstructor = method as ConstructorInfo;
            var declaringType = method.DeclaringType;
            if (methodInfo == null && methodConstructor == null)
                throw new CudaSharpException("Unknown MethodBase type " + method.GetType().FullName);
            if (declaringType == null)
                throw new CudaSharpException("Could not find the declaring type of " + method.Name.StripNameToValidPtx());

            var parameters = method.GetParameters().Select(p => p.ParameterType);
            if (methodConstructor != null)
                parameters = new[] { declaringType.MakeByRefType() }.Concat(parameters);
            if (methodInfo != null && methodInfo.IsStatic == false)
            {
                if (declaringType.IsValueType == false)
                    throw new CudaSharpException("Cannot compile object instance methods (did you forget to mark the method as static?)");
                parameters = new[] { declaringType.MakeByRefType() }.Concat(parameters);
            }
            var llvmParameters =
                parameters.Select(t => ConvertType(module, t)).ToArray();
            var funcType = new FunctionType(ConvertType(module, methodInfo == null ? typeof(void) : methodInfo.ReturnType), llvmParameters);

            var intrinsic = method.GetCustomAttribute<Gpu.BuiltinAttribute>();
            if (intrinsic != null)
            {
                var name = intrinsic.Intrinsic;
                var preExisting = module.GetFunction(name);
                if (preExisting != null)
                    return preExisting;
                return module.CreateFunction(name, funcType);
            }

            var function = module.CreateFunction(methodConstructor == null ? method.Name.StripNameToValidPtx() : declaringType.Name.StripNameToValidPtx() + "_ctor", funcType);

            var block = new Block("entry", module.Context, function);
            var writer = new InstructionBuilder(module.Context, block);

            var opcodes = method.Disassemble().ToList();
            FindBranchTargets(opcodes, module.Context, function);

            var body = method.GetMethodBody();
            var efo = new EmitFuncObj(module, function, body, writer, null, new Stack<Value>(),
                body == null ? null : new Value[body.LocalVariables.Count], new Value[llvmParameters.Length]);

            PrintHeader(efo);
            foreach (var opcode in opcodes)
            {
                if (EmitFunctions.ContainsKey(opcode.Opcode) == false)
                    throw new CudaSharpException("Unsupported CIL instruction " + opcode.Opcode);
                var func = EmitFunctions[opcode.Opcode];
                efo.Argument = opcode.Parameter;
                func(efo);
            }

            return function;
        }
        public static bool IsInternalFrame(MethodBase mb) {
            //All the dynamic methods created for DLR rules have a special name.
            //We also check if the method has a different type than the known
            //non-static method. If it does, it is a dynamic method.
            //This could be improved if the CLR provides a way to attach some information
            //to the dynamic method we create, like CustomAttributes.
            if (mb.Name == "CallSite.Target" && mb.GetType() != _knownNonDynamicMethodType) {
                return true;
            }

            //Filter out the helper methods.
            if (mb.DeclaringType == typeof(System.Dynamic.UpdateDelegates)) {
                return true;
            }

            return false;
        }
        public static MethodBodyInfo Create(MethodBase method)
        {
            MethodBodyInfo mbi = new MethodBodyInfo();

            mbi.Identity = method.GetHashCode();
            mbi.TypeName = method.GetType().Name;
            mbi.MethodToString = ".method " + method.ToIL();

            ILReader reader = ILReaderFactory.Create(method);
            mbi.Exceptions = reader.ILProvider.GetExceptionInfos();

            ReadableILStringVisitor visitor = new ReadableILStringVisitor(
                new MethodBodyInfoBuilder(mbi),
                DefaultFormatProvider.Instance);

            reader.Accept(visitor);

            return mbi;
        }
Esempio n. 5
0
 private void CreateMethod(Type aType, Classifier aCls, MethodBase aMethod, PdOOM.BasePackage aPckg, Operation anOper, InfluenceTable anInfTable)
 {
     Operation operation;
     if (anOper != null)
     {
         operation = anOper;
     }
     else
     {
         operation = (Operation) aCls.CreateObject(0x18112066, "", -1, true);
     }
     operation.Static = aMethod.IsStatic;
     operation.Visibility = GetMethodVisibility(aMethod);
     operation.Final = aMethod.IsFinal;
     if (aMethod.IsGenericMethod)
     {
         operation.Generic = true;
         foreach (TypeParameter parameter in operation.TypeParameters)
         {
             parameter.delete();
         }
         foreach (Type type in aMethod.GetGenericArguments())
         {
             TypeParameter parameter2 = (TypeParameter) operation.CreateObject(0x1811207d, "", -1, true);
             this.ProcessType(type, false);
             TypeMapping mapping = TypeMapping.Retrieve(type);
             parameter2.Name = parameter2.Code = mapping.Name;
             operation.TypeParameters.Add(parameter2);
         }
     }
     if (aMethod.IsVirtual)
     {
         if (LZ.Reverse.Info._bVBNet)
         {
             operation.SetExtendedAttribute("VB.NET.Overridable", "true");
         }
         else
         {
             operation.SetExtendedAttribute("C#.virtual", "true");
         }
     }
     if (aMethod.IsAbstract)
     {
         operation.Abstract = true;
     }
     new CustomHandlerMethod(aMethod, operation).Convert();
     if (anOper == null)
     {
         foreach (ParameterInfo info in aMethod.GetParameters())
         {
             this.AddParameter(operation, info, aPckg);
         }
     }
     bool flag = aMethod.IsConstructor || aMethod.GetType().IsSubclassOf(typeof(ConstructorInfo));
     if (!flag)
     {
         MethodInfo info2 = (MethodInfo) aMethod;
         Type returnType = info2.ReturnType;
         if (returnType.IsArray)
         {
             operation.Array = true;
             returnType = returnType.GetElementType();
         }
         this.ProcessType(returnType, false);
         TypeMapping mapping2 = TypeMapping.Retrieve(returnType);
         if (mapping2.HasClassifier())
         {
             PdOOM.BaseObject obj2 = ProperRef.Link(NamespaceMapping.Retrieve(aPckg), mapping2);
             operation.UseQualifiedDataType = false;
             operation.ReturnTypeObject = obj2;
         }
         else
         {
             operation.ReturnType = mapping2.Name;
         }
         if ((anInfTable != null) && anInfTable.Contains(info2))
         {
             anInfTable.Impact(info2, operation);
         }
         if (LZ.Reverse.Info._bVBNet)
         {
             new CustomHandlerReturn(info2, anOper).Convert();
         }
         bool flag2 = false;
         if (!info2.IsHideBySig && info2.GetBaseDefinition().Equals(info2))
         {
             string name = info2.Name;
             if (!aType.BaseType.Equals(aType))
             {
                 BindingFlags bindingAttr = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
                 bool flag3 = false;
                 foreach (MethodInfo info3 in aType.GetMethods(bindingAttr))
                 {
                     if (info3.Name == name)
                     {
                         flag3 = true;
                         break;
                     }
                 }
                 if (flag3)
                 {
                     flag2 = true;
                 }
             }
         }
         if (info2.GetBaseDefinition().Equals(info2))
         {
             if (flag2)
             {
                 if (LZ.Reverse.Info._bVBNet)
                 {
                     operation.SetExtendedAttribute("VB.NET.Shadowing", "Shadows");
                 }
                 else
                 {
                     operation.SetExtendedAttribute("C#.new", "true");
                 }
             }
             else if (aMethod.IsHideBySig && LZ.Reverse.Info._bVBNet)
             {
                 operation.SetExtendedAttribute("VB.NET.Shadowing", "Overloads");
             }
         }
         else if (LZ.Reverse.Info._bVBNet)
         {
             operation.SetExtendedAttribute("VB.NET.Overrides", "true");
         }
         else
         {
             operation.SetExtendedAttribute("C#.override", "true");
         }
     }
     if (flag)
     {
         operation.Stereotype = "Constructor";
         operation.ReturnType = "";
         operation.InfluentObject = aCls;
         operation.Automatic = true;
         try
         {
             if (LZ.Reverse.Info._bVBNet)
             {
                 operation.Name = operation.Code = "New";
             }
             else
             {
                 operation.Name = operation.Code = TypeInfoHelper.GetTypeName(aType);
             }
             return;
         }
         catch (COMException)
         {
             if (LZ.Reverse.Info._bDebug)
             {
                 LZ.Reverse.Info.Write(new string[] { "*** exception while naming the operation \"{1}\" on the type \"{0}\"", aType.FullName, aMethod.Name });
             }
             return;
         }
     }
     if (anOper == null)
     {
         string aName = aMethod.Name;
         if (!LZ.Reverse.Info._bVBNet && ShortenOperator(aMethod.GetParameters().Length, ref aName))
         {
             operation.Stereotype = "Operator";
         }
         try
         {
             operation.Name = operation.Code = aName;
         }
         catch (COMException)
         {
             if (LZ.Reverse.Info._bDebug)
             {
                 LZ.Reverse.Info.Write(new string[] { "*** exception while naming the operation \"{1}\" on the type \"{0}\"", aType.FullName, aMethod.Name });
             }
         }
     }
 }
 // Return the type of the returned value of the method:
 private static Type GetMethodReturnType(MethodBase method)
 {
     MethodInfo info = method as MethodInfo;
     if (info == null)
     {
         throw new ArgumentException("Unsupported MethodBase : " + method.GetType().Name, "method");
     }
     return info.ReturnType;
 }
Esempio n. 7
0
		private void GetMethodBaseChildren (NodeInfoCollection c, NodeInfo parent, MethodBase mb)
		{
			AddSubnodes (c, parent, mb.GetType(), mb);
		}
 private static Type GetMethodReturnType(MethodBase method)
 {
     var methodInfo = method as MethodInfo;
     if (methodInfo == null)
     {
         // Constructor info.
         throw new ArgumentException("Unsupported MethodBase : " + method.GetType().Name, "method");
     }
     return methodInfo.ReturnType;
 }
Esempio n. 9
0
        public List<IlInstruction> ReadInstructions(MethodBase method)
        {
            ilInstructions = new List<IlInstruction>();
            this.currentMethod = method;

            var body = method.GetMethodBody();
            parameters = method.GetParameters();
            if (body == null)
                return ilInstructions;
            locals = body.LocalVariables;
            instructions = method.GetMethodBody().GetILAsByteArray();
            var str = new ByteStream(instructions, instructions.Length);
            stream = new BinaryReader(str);

            if (!(typeof(ConstructorInfo).IsAssignableFrom(method.GetType())))
                methodArgs = method.GetGenericArguments();

            if (method.DeclaringType != null)
                typeArgs = method.DeclaringType.GetGenericArguments();

            IlInstruction instruction = null;

            while (stream.BaseStream.Position < stream.BaseStream.Length)
            {
                instruction = new IlInstruction();
                bool isDoubleByte = false;
                OpCode code = ReadOpCode(ref isDoubleByte);
                instruction.OpCode = code;
                instruction.Offset = stream.BaseStream.Position-1;
                if (isDoubleByte)
                {
                    instruction.Offset--;
                }
                instruction.Operand = ReadOperand(code, method.Module,ref instruction.LocalVariableIndex);
                ilInstructions.Add(instruction);
            }

            return ilInstructions;
        }
Esempio n. 10
0
        private static void GetMethod(MethodDescription md,MethodBase mi, string NickName,string returnName)
        {
            md.Method = mi;
            md.InputArgs = new List<MethodFieldDescription>();
            md.Output = null;
            md.RawType = mi.GetType();
            md.TypeName = GeneratorHelper.GetTypeName(md.RawType);
            md.Name = mi.Name;
            md.NickName = string.IsNullOrEmpty(NickName) ? md.Name : NickName;
            md.IsStatic = mi.IsStatic;

            foreach (ParameterInfo pi in md.Method.GetParameters())
            {
                MethodFieldDescription mfd = new MethodFieldDescription();
                mfd.Name = pi.Name;
                mfd.RawType = pi.ParameterType;
                mfd.TypeName = GeneratorHelper.GetTypeName(mfd.RawType);
                mfd.IsStatic = false;
                mfd.Type = GetMessageFieldType(mfd.RawType);
                md.InputArgs.Add(mfd);
            }

            MethodInfo mii = md.Method as MethodInfo;
            if (null != mii)
            {
                if (!string.Equals(mii.ReturnType.FullName, "System.Void"))
                {
                    MethodFieldDescription mfd = new MethodFieldDescription();
                    mfd.Name = returnName;
                    mfd.RawType = mii.ReturnType;
                    mfd.TypeName = GeneratorHelper.GetTypeName(mfd.RawType);
                    mfd.IsStatic = false;
                    mfd.Type = GetMessageFieldType(mfd.RawType);
                    md.Output = mfd;
                }
            }
        }
 public static bool IsDynamicOrRTDynamicMethod(MethodBase method)
 {
     Type type = method.GetType();
     return rtDynamicMethodType.Equals(type) || dynamicMethodType.Equals(type);
 }
 public static bool IsRTDynamicMethod(MethodBase method)
 {
     return rtDynamicMethodType.Equals(method.GetType());
 }
Esempio n. 13
0
            public ILReader(MethodBase method)
            {
                if (method == null)
                {
                    throw new ArgumentNullException("method");
                }

                Type rtType = method.GetType();
                if (rtType != s_runtimeMethodInfoType && rtType != s_runtimeConstructorInfoType)
                {
                    throw new ArgumentException("method must be RuntimeMethodInfo or RuntimeConstructorInfo for this constructor.");
                }

                m_ilProvider = new MethodBaseILProvider(method);
                m_resolver = new ModuleScopeTokenResolver(method);
                InstructionBytes = m_ilProvider.GetByteArray();
                m_position = 0;
            }
Esempio n. 14
0
 public static MethodBodyInfo Create(MethodBase method)
 {
     MethodBodyInfo mbi = new MethodBodyInfo();
       mbi.Identity = method.GetHashCode();
       mbi.TypeName = method.GetType().Name;
       mbi.MethodToString = method.ToString();
       ReadableILStringVisitor readableIlStringVisitor = new ReadableILStringVisitor((IILStringCollector) new MethodBodyInfo.MethodBodyInfoBuilder(mbi), (IFormatProvider) DefaultFormatProvider.Instance);
       new ILReader(method).Accept((ILInstructionVisitor) readableIlStringVisitor);
       return mbi;
 }