/// <summary>
        /// Link time code generator used to compile dynamically created methods during link time.
        /// </summary>
        /// <param name="compiler">The assembly compiler used to compile this method.</param>
        /// <param name="methodName">The name of the created method.</param>
        /// <param name="instructionSet">The instruction set.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="compiler"/>, <paramref name="methodName"/> or <paramref name="instructionSet"/> is null.</exception>
        /// <exception cref="System.ArgumentException"><paramref name="methodName"/> is invalid.</exception>
        public static LinkerGeneratedMethod Compile(AssemblyCompiler compiler, string methodName, InstructionSet instructionSet, ITypeSystem typeSystem)
        {
            if (compiler == null)
                throw new ArgumentNullException(@"compiler");
            if (methodName == null)
                throw new ArgumentNullException(@"methodName");
            if (methodName.Length == 0)
                throw new ArgumentException(@"Invalid method name.");

            LinkerGeneratedType compilerGeneratedType = typeSystem.InternalTypeModule.GetType(@"Mosa.Tools.Compiler", @"LinkerGenerated") as LinkerGeneratedType;

            // Create the type if we need to.
            if (compilerGeneratedType == null)
            {
                compilerGeneratedType = new LinkerGeneratedType(typeSystem.InternalTypeModule, @"Mosa.Tools.Compiler", @"LinkerGenerated", null);
                typeSystem.AddInternalType(compilerGeneratedType);
            }

            MethodSignature signature = new MethodSignature(new SigType(CilElementType.Void), new SigType[0]);

            // Create the method
            // HACK: <$> prevents the method from being called from CIL
            LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeSystem.InternalTypeModule, "<$>" + methodName, compilerGeneratedType, signature);
            compilerGeneratedType.AddMethod(method);

            LinkerMethodCompiler methodCompiler = new LinkerMethodCompiler(compiler, compiler.Pipeline.FindFirst<ICompilationSchedulerStage>(), method, instructionSet);
            methodCompiler.Compile();
            return method;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkerGeneratedMethod"/> class.
        /// </summary>
        /// <param name="typeSystem">The type system.</param>
        /// <param name="name">The name of the method.</param>
        /// <param name="declaringType">Type of the declaring.</param>
        public LinkerGeneratedMethod(IModuleTypeSystem typeSystem, string name, RuntimeType declaringType)
            : base(typeSystem, 0, declaringType)
        {
            if (name == null)
                throw new ArgumentNullException(@"name");

            this.name = name;
            this.signature = new MethodSignature(new SigType(CilElementType.Void), new SigType[0]);
            this.Parameters = new List<RuntimeParameter>();
        }
예제 #3
0
 public CilGenericMethod(IModuleTypeSystem moduleTypeSystem, CilRuntimeMethod method, MethodSignature signature, RuntimeType declaringType)
     : base(moduleTypeSystem, method.Token, declaringType)
 {
     this.genericMethod = method;
     this.Signature = signature;
     this.Attributes = method.Attributes;
     this.ImplAttributes = method.ImplAttributes;
     this.Rva = method.Rva;
     this.Parameters = method.Parameters;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="CompilerGeneratedMethod"/> class.
        /// </summary>
        /// <param name="module">The module.</param>
        /// <param name="name">The name of the method.</param>
        /// <param name="declaringType">Type of the declaring.</param>
        public CompilerGeneratedMethod(IMetadataModule module, string name, RuntimeType declaringType)
            : base(0, module, declaringType)
        {
            if (name == null)
                throw new ArgumentNullException(@"name");

            this.name = name;
            this.signature = new MethodSignature(new SigType(CilElementType.Void), new SigType[0]);
            this.Parameters = new List<RuntimeParameter>();
        }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CilGenericMethod"/> class.
 /// </summary>
 /// <param name="module">The module.</param>
 /// <param name="genericMethod">The generic method.</param>
 /// <param name="signature">The signature.</param>
 /// <param name="declaringType">Type of the declaring.</param>
 public CilGenericMethod(ITypeModule module, CilRuntimeMethod genericMethod, MethodSignature signature, RuntimeType declaringType)
     : base(module, genericMethod.Token, declaringType)
 {
     this.Signature = signature;
     this.Attributes = genericMethod.Attributes;
     this.ImplAttributes = genericMethod.ImplAttributes;
     this.Rva = genericMethod.Rva;
     this.Parameters = genericMethod.Parameters;
     base.Name = genericMethod.Name;
 }
예제 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CilRuntimeMethod"/> class.
        /// </summary>
        /// <param name="module">The module.</param>
        /// <param name="name">The name.</param>
        /// <param name="signature">The signature.</param>
        /// <param name="token">The token.</param>
        /// <param name="declaringType">Type of the declaring.</param>
        /// <param name="methodAttributes">The method attributes.</param>
        /// <param name="methodImplAttributes">The method impl attributes.</param>
        /// <param name="rva">The rva.</param>
        public CilRuntimeMethod(ITypeModule module, string name, MethodSignature signature, Token token, RuntimeType declaringType, MethodAttributes methodAttributes, MethodImplAttributes methodImplAttributes, uint rva)
            : base(module, token, declaringType)
        {
            base.Attributes = methodAttributes;
            base.ImplAttributes = methodImplAttributes;
            base.Rva = rva;
            this.Name = name;
            this.Signature = signature;

            this.Parameters = new List<RuntimeParameter>();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkerGeneratedMethod"/> class.
        /// </summary>
        /// <param name="typeSystem">The type system.</param>
        /// <param name="name">The name of the method.</param>
        /// <param name="declaringType">Type of the declaring.</param>
        public LinkerGeneratedMethod(ITypeModule typeSystem, string name, RuntimeType declaringType, MethodSignature signature)
            : base(typeSystem, Token.Zero, declaringType)
        {
            if (name == null)
                throw new ArgumentNullException(@"name");

            base.Name = name;

            base.Signature = signature;
            this.Parameters = new List<RuntimeParameter>();
        }
예제 #8
0
        public CilGenericMethod(CilRuntimeMethod method, MethodSignature signature, ISignatureContext signatureContext)
            : base(method.Token, method.Module, method.DeclaringType)
        {
            this.genericMethod = method;
            this.signatureContext = signatureContext;

            this.Signature = signature;

            this.Attributes = method.Attributes;
            this.ImplAttributes = method.ImplAttributes;
            this.Rva = method.Rva;
            this.Parameters = method.Parameters;
        }
예제 #9
0
        public override IEnumerable GetValues()
        {
            yield return TokenString("Name", row.NameStringIdx);
            yield return Value("NameStringIdx", row.NameStringIdx);
            yield return Value("Flags", row.Flags.ToString());
            yield return Value("ImplFlags", row.ImplFlags.ToString());
            yield return Value("ParamList", row.ParamList);
            yield return Value("Rva", row.Rva.ToString());
            yield return Value("SignatureBlobIdx", row.SignatureBlobIdx);

            MethodSignature signature = new MethodSignature(Metadata, row.SignatureBlobIdx);
            yield return Value("Signature Token", signature.Token);
            yield return Value("Signature Generic Parameters", signature.GenericParameterCount.ToString());
        }
예제 #10
0
        /// <summary>
        /// Froms the member ref signature token.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public static Signature FromMemberRefSignatureToken(ISignatureContext context, IMetadataProvider provider, TokenTypes token)
        {
            Signature result;
            int index = 0;
            byte[] buffer;
            provider.Read(token, out buffer);

            if (0x06 == buffer[0])
            {
                result = new FieldSignature();
                result.ParseSignature(context, buffer, ref index);
            }
            else
            {
                result = new MethodSignature();
                result.ParseSignature(context, buffer, ref index);
            }
            Debug.Assert(index == buffer.Length, @"Not all signature bytes read.");
            return result;
        }
예제 #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodSignature"/> class.
        /// </summary>
        /// <param name="signature">The signature.</param>
        public MethodSignature(MethodSignature signature)
            : base(signature)
        {
            this.methodCallingConvention = signature.methodCallingConvention;
            this.hasExplicitThis = signature.hasExplicitThis;
            this.hasThis = signature.hasThis;
            this.returnType = signature.returnType;
            this.genericParameterCount = signature.genericParameterCount;

            this.parameters = new SigType[signature.parameters.Length];
            for (int i = 0; i < signature.parameters.Length; i++)
                this.parameters[i] = signature.parameters[i];
        }
예제 #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodSignature"/> class.
        /// </summary>
        /// <param name="context">The context of the generic method spec signature.</param>
        /// <param name="signature">The signature of the generic method.</param>
        /// <param name="specification">The signature specifying replacements for the generic signature.</param>
        public MethodSignature(ISignatureContext context, MethodSignature signature, MethodSpecSignature specification)
        {
            if (context == null)
                throw new ArgumentNullException(@"context");
            if (signature == null)
                throw new ArgumentNullException(@"signature");
            if (specification == null)
                throw new ArgumentNullException(@"specification");

            this._callingConvention = signature.CallingConvention;
            this._hasExplicitThis = signature.HasExplicitThis;
            this._hasThis = signature.HasThis;
            this._genericParameterCount = 0;

            int length = signature.Parameters.Length;
            this._parameters = new SigType[length];
            for (int index = 0; index < length; index++)
            {
                this._parameters[index] = this.ApplySpecification(context, specification, signature.Parameters[index]);
            }
            this._returnType = this.ApplySpecification(context, specification, signature.ReturnType);
        }
예제 #13
0
        /// <summary>
        /// Matcheses the specified other.
        /// </summary>
        /// <param name="other">The other.</param>
        /// <returns></returns>
        public bool Matches(MethodSignature other)
        {
            if (object.ReferenceEquals(this, other))
                return true;

            // TODO: Check this to make sure it is correct
            if (other.GenericParameterCount != this.GenericParameterCount)
                return false;
            if (other.CallingConvention != this.CallingConvention)
                return false;
            if (other.HasThis != this.HasThis)
                return false;
            if (other.HasExplicitThis != this.HasExplicitThis)
                return false;
            if (this.Parameters.Length != other.Parameters.Length)
                return false;
            if (!this.ReturnType.Matches(other.ReturnType))
                return false;

            SigType[] thisParameters = this.Parameters;
            SigType[] otherParameters = other.Parameters;
            for (int i = 0; i < thisParameters.Length; i++)
            {
                if (!thisParameters[i].Matches(otherParameters[i]))
                    return false;
            }

            return true;
        }
예제 #14
0
 /// <summary>
 /// Gets the method signature.
 /// </summary>
 /// <returns>The method signature.</returns>
 protected override MethodSignature GetMethodSignature()
 {
     MethodSignature signature = new MethodSignature();
     signature.LoadSignature(this, this.Module.Metadata, this.signatureBlobIdx);
     return signature;
 }
예제 #15
0
 /// <summary>
 /// Parses the specified provider.
 /// </summary>
 /// <param name="provider">The provider.</param>
 /// <param name="token">The token.</param>
 /// <returns></returns>
 public static MethodSignature Parse(IMetadataProvider provider, TokenTypes token)
 {
     byte[] buffer;
     int index = 0;
     provider.Read(token, out buffer);
     MethodSignature msig = new MethodSignature(token);
     msig.ParseSignature(buffer, ref index);
     Debug.Assert(index == buffer.Length, @"Signature parser didn't complete.");
     return msig;
 }
 public void SetSignature(MethodSignature signature)
 {
     this.signature = signature;
 }
        /// <summary>
        /// Decodes the method specification
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        private RuntimeMethod DecodeMethodSpec(TokenTypes token)
        {
            MethodSpecRow methodSpec = metadata.ReadMethodSpecRow(token);

            CilRuntimeMethod genericMethod = (CilRuntimeMethod)((IModuleTypeSystem)this).GetMethod(methodSpec.MethodTableIdx);

            MethodSpecSignature specSignature = new MethodSpecSignature(metadata, methodSpec.InstantiationBlobIdx);

            MethodSignature signature = new MethodSignature(genericMethod.MetadataModule.Metadata, genericMethod.Signature.Token);

            return new CilGenericMethod(this, genericMethod, signature, genericMethod.DeclaringType);
        }
예제 #18
0
        private IEnumerable<RuntimeMethod> GetMethods()
        {
            this.ProcessSignature();

            List<RuntimeMethod> methods = new List<RuntimeMethod>();
            foreach (CilRuntimeMethod method in this.genericType.Methods)
            {
                MethodSignature signature = new MethodSignature();
                signature.LoadSignature(this, method.Module.Metadata, method.Signature.Token);

                RuntimeMethod genericInstanceMethod = new CilGenericMethod(method, signature, this);
                methods.Add(genericInstanceMethod);
            }

            return methods;
        }
예제 #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MethodSignature"/> class.
 /// </summary>
 /// <param name="signature">The signature.</param>
 /// <param name="genericArguments">The generic arguments.</param>
 public MethodSignature(MethodSignature signature, SigType[] genericArguments)
     : this(signature)
 {
     ApplyGenericArguments(genericArguments);
 }
예제 #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MethodSignature"/> class.
 /// </summary>
 /// <param name="signature">The signature.</param>
 /// <param name="genericArguments">The generic arguments.</param>
 public MethodSignature(MethodSignature signature, SigType[] genericArguments)
     : this(signature)
 {
     ApplyGenericArguments(genericArguments);
 }
예제 #21
0
        private void ResolveMethods()
        {
            foreach (CilRuntimeMethod method in baseGenericType.Methods)
            {
                MethodSignature signature = new MethodSignature(method.Signature, genericArguments);

                RuntimeMethod genericInstanceMethod = new CilGenericMethod(Module, method, signature, this);
                Methods.Add(genericInstanceMethod);
            }
        }
예제 #22
0
        private IList<RuntimeMethod> GetMethods()
        {
            List<RuntimeMethod> methods = new List<RuntimeMethod>();
            foreach (CilRuntimeMethod method in this.genericType.Methods)
            {
                MethodSignature signature = new MethodSignature(method.MetadataModule.Metadata, method.Signature.Token);

                signature.ApplyGenericType(this.genericArguments);

                RuntimeMethod genericInstanceMethod = new CilGenericMethod(moduleTypeSystem, method, signature, this);

                methods.Add(genericInstanceMethod);
            }

            return methods;
        }
        private void CompileObjectEquals(string typeName)
        {
            LinkerGeneratedType type = new LinkerGeneratedType(typeModule, @"System", typeName, null);

            MethodSignature signature = new MethodSignature(BuiltInSigType.Boolean, new SigType[] { BuiltInSigType.Object });

            // Create the method
            LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeModule, @"Equals", type, signature);
            method.Parameters.Add(new RuntimeParameter(@"obj", 0, ParameterAttributes.In));
            type.AddMethod(method);

            this.Compile(method);
        }
        private RuntimeMethod GenerateMethod(string nameSpace, string typeName, string methodName)
        {
            var type = new LinkerGeneratedType(typeModule, nameSpace, typeName, null);

            MethodSignature signature = new MethodSignature(new SigType(CilElementType.Void), new SigType[0]);

            // Create the method
            LinkerGeneratedMethod method = new LinkerGeneratedMethod(typeModule, methodName, type, signature);
            type.AddMethod(method);

            return method;
        }