/// <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(BuiltInSigType.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;
        }
        public override IEnumerable GetValues()
        {
            yield return TokenString("Name", row.NameString);
            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());

            MethodSignature signature = new MethodSignature(Metadata, row.SignatureBlob);
            yield return Value("Signature", signature.ToString());

            if (row.Rva != 0)
            {
                var code = MetadataModule.GetInstructionStream((long)row.Rva);
                var codeReader = new EndianAwareBinaryReader(code, Endianness.Little);
                var header = new MethodHeader(codeReader);

                if (header.LocalVarSigTok.RID != 0)
                {
                    yield return Value("LocalVarSigTok", header.LocalVarSigTok.ToString());

                    StandAloneSigRow standAlongSigRow = Metadata.ReadStandAloneSigRow(header.LocalVarSigTok);
                    var local = new LocalVariableSignature(Metadata, standAlongSigRow.SignatureBlob);
                    yield return Value("Method Header", local.ToString());
                }
            }
        }
 /// <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;
 }
        /// <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>();
        }
        /// <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>();
        }
Beispiel #6
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());
        }
        /// <summary>
        /// Matches 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.MethodCallingConvention != this.MethodCallingConvention)
                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;

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

            return true;
        }
 /// <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);
 }
        /// <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];
        }
Beispiel #10
0
 private void ResolveMethods()
 {
     foreach (CilRuntimeMethod method in baseGenericType.Methods)
     {
         var signature = new MethodSignature(method.Signature, genericArguments);
         var genericInstanceMethod = new CilGenericMethod(Module, method, signature, this);
         Methods.Add(genericInstanceMethod);
     }
 }