/// <summary> /// Decodes the specified instruction. /// </summary> /// <param name="ctx">The context.</param> /// <param name="decoder">The instruction decoder, which holds the code stream.</param> public override void Decode(Context ctx, IInstructionDecoder decoder) { base.Decode(ctx, decoder); Token token = decoder.DecodeTokenType(); ITypeModule module = null; Mosa.Runtime.TypeSystem.Generic.CilGenericType genericType = decoder.Method.DeclaringType as Mosa.Runtime.TypeSystem.Generic.CilGenericType; if (genericType != null) { module = (decoder.Method.DeclaringType as Mosa.Runtime.TypeSystem.Generic.CilGenericType).BaseGenericType.Module; } else { module = decoder.Method.Module; } ctx.RuntimeField = module.GetField(token); if (ctx.RuntimeField.ContainsGenericParameter) { ; } SigType sigType = new RefSigType(ctx.RuntimeField.SignatureType); ctx.Result = LoadInstruction.CreateResultOperand(decoder, Operand.StackTypeFromSigType(sigType), sigType); }
/// <summary> /// Initializes a new instance of the <see cref="CilGenericType"/> class. /// </summary> /// <param name="typeModule">The type module.</param> /// <param name="token">The token.</param> /// <param name="baseGenericType">Type of the base generic.</param> /// <param name="genericTypeInstanceSignature">The generic type instance signature.</param> public CilGenericType(ITypeModule typeModule, Token token, RuntimeType baseGenericType, GenericInstSigType genericTypeInstanceSignature) : base(baseGenericType.Module, token, baseGenericType.BaseType) { Debug.Assert(baseGenericType is CilRuntimeType); this.signature = genericTypeInstanceSignature; this.baseGenericType = baseGenericType as CilRuntimeType; this.InstantiationModule = typeModule; base.Attributes = baseGenericType.Attributes; base.Namespace = baseGenericType.Namespace; if (this.baseGenericType.IsNested) { // TODO: find generic type ; } // TODO: if this is a nested types, add enclosing type(s) into genericArguments first this.genericArguments = signature.GenericArguments; base.Name = GetName(typeModule); ResolveMethods(); ResolveFields(); this.containsOpenGenericArguments = CheckContainsOpenGenericParameters(); }
/// <summary> /// Initializes a new instance of the <see cref="RuntimeAttribute"/> class. /// </summary> /// <param name="typeModule">The type module.</param> /// <param name="ctor">The ctor.</param> /// <param name="ctorMethod">The ctor method.</param> /// <param name="blobIndex">Index of the blob.</param> public RuntimeAttribute(ITypeModule typeModule, Token ctor, RuntimeMethod ctorMethod, HeapIndexToken blobIndex) { this.typeModule = typeModule; this.ctorMethod = ctorMethod; this.ctor = ctor; this.blobIndex = blobIndex; }
private string GetName(ITypeModule typeModule) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0}<", baseGenericType.Name); foreach (SigType sigType in genericArguments) { if (sigType.IsOpenGenericParameter) { sb.AppendFormat("{0}, ", sigType.ToString()); } else { RuntimeType type = GetRuntimeTypeForSigType(sigType, typeModule); if (type != null) { sb.AppendFormat("{0}, ", type.FullName); } else { sb.Append("<null>"); } } } sb.Length -= 2; sb.Append(">"); return(sb.ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="CilGenericType"/> class. /// </summary> /// <param name="module">The module.</param> /// <param name="baseGenericType">Type of the base generic.</param> /// <param name="genericTypeInstanceSignature">The generic type instance signature.</param> /// <param name="token">The token.</param> /// <param name="typeModule">The type module.</param> public CilGenericType(ITypeModule module, RuntimeType baseGenericType, GenericInstSigType genericTypeInstanceSignature, Token token, ITypeModule typeModule) : base(module, token, baseGenericType.BaseType) { Debug.Assert(baseGenericType is CilRuntimeType); this.signature = genericTypeInstanceSignature; this.baseGenericType = baseGenericType as CilRuntimeType; base.Attributes = baseGenericType.Attributes; base.Namespace = baseGenericType.Namespace; if (this.baseGenericType.IsNested) { // TODO: find generic type ; } // TODO: if this is a nested types, add enclosing type(s) into genericArguments first this.genericArguments = signature.GenericArguments; base.Name = GetName(typeModule); ResolveMethods(); ResolveFields(); this.containsOpenGenericArguments = CheckContainsOpenGenericParameters(); }
/// <summary> /// Decodes the specified instruction. /// </summary> /// <param name="ctx">The context.</param> /// <param name="decoder">The instruction decoder, which holds the code stream.</param> public override void Decode(Context ctx, IInstructionDecoder decoder) { // Decode base classes first base.Decode(ctx, decoder); Token token = decoder.DecodeTokenType(); ITypeModule module = null; Mosa.Runtime.TypeSystem.Generic.CilGenericType genericType = decoder.Method.DeclaringType as Mosa.Runtime.TypeSystem.Generic.CilGenericType; if (genericType != null) module = (decoder.Method.DeclaringType as Mosa.Runtime.TypeSystem.Generic.CilGenericType).BaseGenericType.Module; else module = decoder.Method.Module; ctx.RuntimeField = module.GetField(token); if (ctx.RuntimeField.ContainsGenericParameter) { foreach (RuntimeField field in decoder.Method.DeclaringType.Fields) if (field.Name == ctx.RuntimeField.Name) { ctx.RuntimeField = field; break; } Debug.Assert(!ctx.RuntimeField.ContainsGenericParameter); } SigType sigType = ctx.RuntimeField.SignatureType; ctx.Result = LoadInstruction.CreateResultOperand(decoder, Operand.StackTypeFromSigType(sigType), sigType); }
/// <summary> /// Initializes a new instance of the <see cref="CilRuntimeField"/> 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="offset">The offset.</param> /// <param name="rva">The rva.</param> /// <param name="declaringType">Type of the declaring.</param> /// <param name="attributes">The attributes.</param> public CilRuntimeField(ITypeModule module, string name, FieldSignature signature, Token token, uint offset, uint rva, RuntimeType declaringType, FieldAttributes attributes) : base(module, token, declaringType) { this.Name = name; this.Signature = signature; base.Attributes = attributes; base.RVA = rva; }
/// <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="CilGenericField"/> class. /// </summary> /// <param name="module">The module.</param> /// <param name="genericField">The generic field.</param> /// <param name="signature">The signature.</param> /// <param name="declaringType">Type of the declaring.</param> public CilGenericField(ITypeModule module, RuntimeField genericField, FieldSignature signature, CilGenericType declaringType) : base(module, declaringType) { this.Signature = signature; this.Attributes = genericField.Attributes; //TODO //this.SetAttributes(genericField.CustomAttributes); base.Name = genericField.Name; }
/// <summary> /// Gets the runtime type for the given type name and namespace /// </summary> /// <param name="assembly">The assembly.</param> /// <param name="nameSpace">The name space.</param> /// <param name="name">The name.</param> /// <returns></returns> RuntimeType ITypeSystem.GetType(string assembly, string nameSpace, string name) { ITypeModule module = ((ITypeSystem)this).ResolveModuleReference(assembly); if (module == null) { return(null); } return(module.GetType(nameSpace, name)); }
private RuntimeType GetRuntimeTypeForSigType(SigType sigType, ITypeModule typeModule) { RuntimeType result = null; switch (sigType.Type) { case CilElementType.Class: Debug.Assert(sigType is TypeSigType, @"Failing to resolve VarSigType in GenericType."); result = typeModule.GetType((sigType as TypeSigType).Token); break; case CilElementType.ValueType: goto case CilElementType.Class; case CilElementType.GenericInst: result = typeModule.GetType((sigType as GenericInstSigType).BaseType.Token); break; case CilElementType.Var: throw new NotImplementedException(@"Failing to resolve VarSigType in GenericType."); case CilElementType.MVar: throw new NotImplementedException(@"Failing to resolve VarMSigType in GenericType."); case CilElementType.SZArray: // FIXME return(null); default: BuiltInSigType builtIn = sigType as BuiltInSigType; if (builtIn != null) { ITypeModule mscorlib; if (typeModule.Name == "mscorlib") { mscorlib = typeModule; } else { mscorlib = typeModule.TypeSystem.ResolveModuleReference("mscorlib"); } result = mscorlib.GetType(builtIn.TypeName); } else { throw new NotImplementedException(String.Format("SigType of CilElementType.{0} is not supported.", sigType.Type)); } break; } return(result); }
/// <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>(); }
/// <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>(); }
private void CreateAssemblyDefinitionTable(ITypeModule typeModule) { string assemblyNameSymbol = typeModule.Name + @"$aname"; // Emit assembly name using (Stream stream = linker.Allocate(assemblyNameSymbol, SectionKind.ROData, 0, typeLayout.NativePointerAlignment)) { using (EndianAwareBinaryWriter writer = new EndianAwareBinaryWriter(stream, architecture.IsLittleEndian)) { EmitStringWithLength(writer, typeModule.Name); } } uint moduleTypes = 0; foreach (RuntimeType type in typeModule.GetAllTypes()) { if (!type.IsModule) moduleTypes++; } string assemblyTableSymbol = typeModule.Name + @"$atable"; using (Stream stream = linker.Allocate(assemblyTableSymbol, SectionKind.ROData, 0, typeLayout.NativePointerAlignment)) { using (EndianAwareBinaryWriter writer = new EndianAwareBinaryWriter(stream, architecture.IsLittleEndian)) { // 1. Pointer to Assembly Name linker.Link(LinkType.AbsoluteAddress | LinkType.NativeI4, assemblyTableSymbol, 0, 0, assemblyNameSymbol, IntPtr.Zero); writer.Position += typeLayout.NativePointerSize; // 2. Number of types writer.Write(moduleTypes); // 3. Pointer to list of types foreach (var type in typeModule.GetAllTypes()) { if (!type.IsModule && !(type.Module is InternalTypeModule)) linker.Link(LinkType.AbsoluteAddress | LinkType.NativeI4, assemblyTableSymbol, (int)writer.Position, 0, type.FullName + @"$dtable", IntPtr.Zero); writer.Position += typeLayout.NativePointerSize; } } } foreach (var type in typeModule.GetAllTypes()) { if (!type.IsModule) { CreateTypeDefinitionTable(type, assemblyTableSymbol); } } }
/// <summary> /// Decodes the invocation target. /// </summary> /// <param name="ctx">The context.</param> /// <param name="decoder">The IL decoder, which provides decoding functionality.</param> /// <param name="flags">Flags, which control the</param> /// <returns></returns> protected static Token DecodeInvocationTarget(Context ctx, IInstructionDecoder decoder, InvokeSupportFlags flags) { // Retrieve the immediate argument - it contains the token // of the methoddef, methodref, methodspec or callsite to call. Token callTarget = decoder.DecodeTokenType(); if (!IsCallTargetSupported(callTarget.Table, flags)) { throw new InvalidOperationException(@"Invalid IL call target specification."); } RuntimeMethod method = null; ITypeModule module = null; Mosa.Runtime.TypeSystem.Generic.CilGenericType genericType = decoder.Method.DeclaringType as Mosa.Runtime.TypeSystem.Generic.CilGenericType; if (genericType != null) { module = (decoder.Method.DeclaringType as Mosa.Runtime.TypeSystem.Generic.CilGenericType).BaseGenericType.Module; } else { module = decoder.Method.Module; } switch (callTarget.Table) { case TableType.MethodDef: method = module.GetMethod(callTarget); break; case TableType.MemberRef: method = module.GetMethod(callTarget, decoder.Method.DeclaringType); if (method.DeclaringType.IsGeneric) { decoder.Compiler.Scheduler.ScheduleTypeForCompilation(method.DeclaringType); } break; case TableType.MethodSpec: method = module.GetMethod(callTarget); decoder.Compiler.Scheduler.ScheduleTypeForCompilation(method.DeclaringType); break; default: Debug.Assert(false, @"Should never reach this!"); break; } SetInvokeTarget(ctx, decoder.Compiler, method); return(callTarget); }
/// <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="LinkerGeneratedType"/> class. /// </summary> /// <param name="moduleTypeSystem">The module type system.</param> /// <param name="nameSpace">The name space.</param> /// <param name="name">The name.</param> /// <param name="baseType">Type of the base.</param> public LinkerGeneratedType(ITypeModule moduleTypeSystem, string nameSpace, string name, RuntimeType baseType) : base(moduleTypeSystem, Token.Zero, baseType) { if (nameSpace == null) throw new ArgumentNullException(@"namespace"); if (name == null) throw new ArgumentNullException(@"name"); base.Namespace = nameSpace; base.Name = name; this.methods = new List<RuntimeMethod>(); }
/// <summary> /// Loads the module. /// </summary> /// <param name="modules">The modules.</param> void ITypeSystem.LoadModules(IList <IMetadataModule> modules) { foreach (IMetadataModule module in modules) { ITypeModule typeModule = new TypeModule(this, module); typeModules.Add(typeModule); if (typeModule.MetadataModule.ModuleType == ModuleType.Executable) { mainTypeModule = typeModule; } } }
/// <summary> /// Initializes a new instance of the <see cref="RuntimeType"/> class. /// </summary> /// <param name="module">The module.</param> /// <param name="token">The token of the type.</param> /// <param name="baseType">Type of the base.</param> public RuntimeType(ITypeModule module, Token token, RuntimeType baseType) : base(module, token, null) { this.baseType = baseType; if (baseType == null) { this.isValueType = false; this.isDelegate = false; this.isEnum = false; } else { if (baseType.isValueType) { this.isValueType = true; } else if (baseType.FullName == "System.ValueType") { this.isValueType = true; } if (baseType.isDelegate) { this.isDelegate = true; } else if (baseType.FullName == "System.Delegate") { this.isDelegate = true; } if (baseType.isEnum) { this.isEnum = true; } else if (baseType.FullName == "System.Enum") { this.isEnum = true; } } this.fields = new List <RuntimeField>(); this.methods = new List <RuntimeMethod>(); this.interfaces = new List <RuntimeType>(); this.genericParameters = new List <GenericParameter>(); }
/// <summary> /// Performs stage specific processing on the compiler context. /// </summary> void IAssemblyCompilerStage.Run() { ITypeModule mainTypeModule = typeSystem.MainTypeModule; if (mainTypeModule.MetadataModule.EntryPoint.RID != 0) { RuntimeMethod entrypoint = mainTypeModule.GetMethod(mainTypeModule.MetadataModule.EntryPoint); Schedule(entrypoint); } ctx.AppendInstruction(IR.Instruction.EpilogueInstruction); ctx.Other = 0; method = LinkTimeCodeGenerator.Compile(compiler, @"AssemblyInit", instructionSet, typeSystem); }
public void ResolveInterfaces(ITypeModule typeModule) { foreach (RuntimeType type in baseGenericType.Interfaces) { if (!type.ContainsOpenGenericParameters) { Interfaces.Add(type); } else { CilGenericType genericType = type as CilGenericType; Debug.Assert(genericType != null); RuntimeType matchedInterfaceType = null; // -- only needs to search generic type interfaces foreach (RuntimeType runtimetype in typeModule.GetAllTypes()) { if (runtimetype.IsInterface) { CilGenericType runtimetypegeneric = runtimetype as CilGenericType; if (runtimetypegeneric != null) { if (genericType.baseGenericType == runtimetypegeneric.baseGenericType) { if (SigType.Equals(GenericArguments, runtimetypegeneric.GenericArguments)) { matchedInterfaceType = runtimetype; //Interfaces.Add(runtimetype); break; } } } } } if (matchedInterfaceType != null) { Interfaces.Add(matchedInterfaceType); } else { continue; } } } }
/// <summary> /// Initializes a new instance of the <see cref="LinkerGeneratedType"/> class. /// </summary> /// <param name="moduleTypeSystem">The module type system.</param> /// <param name="nameSpace">The name space.</param> /// <param name="name">The name.</param> /// <param name="baseType">Type of the base.</param> public LinkerGeneratedType(ITypeModule moduleTypeSystem, string nameSpace, string name, RuntimeType baseType) : base(moduleTypeSystem, Token.Zero, baseType) { if (nameSpace == null) { throw new ArgumentNullException(@"namespace"); } if (name == null) { throw new ArgumentNullException(@"name"); } base.Namespace = nameSpace; base.Name = name; this.methods = new List <RuntimeMethod>(); }
/// <summary> /// Initializes a new instance of the <see cref="CilRuntimeType"/> class. /// </summary> /// <param name="module">The module.</param> /// <param name="name">The name.</param> /// <param name="typeNamespace">The type namespace.</param> /// <param name="packing">The packing.</param> /// <param name="size">The size.</param> /// <param name="token">The token.</param> /// <param name="baseType">Type of the base.</param> /// <param name="enclosingType">Type of the enclosing.</param> /// <param name="attributes">The attributes.</param> /// <param name="baseToken">The base token.</param> public CilRuntimeType(ITypeModule module, string name, string typeNamespace, int packing, int size, Token token, RuntimeType baseType, RuntimeType enclosingType, TypeAttributes attributes, Token baseToken) : base(module, token, baseType) { this.baseTypeToken = baseToken; this.enclosingType = enclosingType; base.Attributes = attributes; base.Pack = packing; base.LayoutSize = size; base.Name = name; base.Namespace = typeNamespace; if (IsNested) { Debug.Assert(enclosingType != null); this.Namespace = enclosingType.Namespace + "." + enclosingType.Name; } }
/// <summary> /// Setups the specified compiler. /// </summary> /// <param name="compiler">The compiler.</param> public void Setup(IMethodCompiler compiler) { if (compiler == null) { throw new ArgumentNullException(@"compiler"); } methodCompiler = compiler; InstructionSet = compiler.InstructionSet; basicBlocks = compiler.BasicBlocks; architecture = compiler.Architecture; typeModule = compiler.Method.Module; typeSystem = compiler.TypeSystem; typeLayout = compiler.TypeLayout; callingConvention = architecture.GetCallingConvention(); architecture.GetTypeRequirements(BuiltInSigType.IntPtr, out nativePointerSize, out nativePointerAlignment); }
/// <summary> /// Initializes a new instance of the <see cref="BaseMethodCompiler"/> class. /// </summary> /// <param name="linker">The _linker.</param> /// <param name="architecture">The target compilation Architecture.</param> /// <param name="type">The type, which owns the method to compile.</param> /// <param name="method">The method to compile by this instance.</param> protected BaseMethodCompiler( IAssemblyLinker linker, IArchitecture architecture, ICompilationSchedulerStage compilationScheduler, RuntimeType type, RuntimeMethod method, ITypeSystem typeSystem, ITypeLayout typeLayout) { if (architecture == null) { throw new ArgumentNullException(@"architecture"); } if (linker == null) { throw new ArgumentNullException(@"linker"); } if (compilationScheduler == null) { throw new ArgumentNullException(@"compilationScheduler"); } this.linker = linker; this.architecture = architecture; this.method = method; this.type = type; parameters = new List <Operand>(new Operand[method.Parameters.Count]); nextStackSlot = 0; basicBlocks = new List <BasicBlock>(); instructionSet = null; // this will be set later pipeline = new CompilerPipeline(); this.compilationScheduler = compilationScheduler; this.moduleTypeSystem = method.Module; this.typeSystem = typeSystem; this.typeLayout = typeLayout; }
/// <summary> /// Decodes the specified instruction. /// </summary> /// <param name="ctx">The context.</param> /// <param name="decoder">The instruction decoder, which holds the code stream.</param> public override void Decode(Context ctx, IInstructionDecoder decoder) { // Decode base classes first base.Decode(ctx, decoder); // Load the _stackFrameIndex token from the immediate Token token = decoder.DecodeTokenType(); //Console.WriteLine("Stfld used in {0}.{1}", decoder.Method.DeclaringType.FullName, decoder.Method.Name); Debug.Assert(token.Table == TableType.Field || token.Table == TableType.MemberRef, @"Invalid token type."); ITypeModule module = null; Mosa.Runtime.TypeSystem.Generic.CilGenericType genericType = decoder.Method.DeclaringType as Mosa.Runtime.TypeSystem.Generic.CilGenericType; if (genericType != null) { module = (decoder.Method.DeclaringType as Mosa.Runtime.TypeSystem.Generic.CilGenericType).BaseGenericType.Module; } else { module = decoder.Method.Module; } ctx.RuntimeField = module.GetField(token); if (ctx.RuntimeField.ContainsGenericParameter) { foreach (RuntimeField field in decoder.Method.DeclaringType.Fields) { if (field.Name == ctx.RuntimeField.Name) { ctx.RuntimeField = field; break; } } Debug.Assert(!ctx.RuntimeField.ContainsGenericParameter); } }
/// <summary> /// Performs stage specific processing on the compiler context. /// </summary> void IAssemblyCompilerStage.Run() { CheckImplementation(); ITypeModule mainModule = typeSystem.MainTypeModule; if (mainModule.MetadataModule.EntryPoint.RID != 0) { RuntimeMethod entrypoint = mainModule.GetMethod(mainModule.MetadataModule.EntryPoint); implementation.EntryPoint = GetSymbol(entrypoint.ToString()); } // Run the real linker IAssemblyCompilerStage assemblyCompilerStage = implementation as IAssemblyCompilerStage; Debug.Assert(assemblyCompilerStage != null, @"Linker doesn't implement IAssemblyCompilerStage."); if (assemblyCompilerStage != null) { assemblyCompilerStage.Run(); } }
RuntimeType ITypeSystem.ResolveGenericType(ITypeModule typeModule, TypeSpecSignature typeSpecSignature, Token token) { var genericInstSigType = typeSpecSignature.Type as GenericInstSigType; if (genericInstSigType == null) return null; RuntimeType genericType = null; SigType sigType = genericInstSigType; switch (genericInstSigType.Type) { case CilElementType.ValueType: goto case CilElementType.Class; case CilElementType.Class: TypeSigType typeSigType = (TypeSigType)sigType; genericType = typeModule.GetType(typeSigType.Token); break; case CilElementType.GenericInst: var genericBaseType = typeModule.GetType(genericInstSigType.BaseType.Token); genericType = new CilGenericType(typeModule, token, genericBaseType, genericInstSigType); break; default: throw new NotSupportedException(String.Format(@"LoadTypeSpecs does not support CilElementType.{0}", genericInstSigType.Type)); } return genericType; }
/// <summary> /// Patches the field. /// </summary> /// <param name="typeModule">The type module.</param> /// <param name="enclosingType">Type of the closed.</param> /// <param name="openField">The open field.</param> /// <returns></returns> RuntimeField IGenericTypePatcher.PatchField(ITypeModule typeModule, CilGenericType enclosingType, RuntimeField openField) { var openType = openField.DeclaringType as CilGenericType; var genericArguments = CloseGenericArguments(enclosingType, openType); var patchedType = GetPatchedType(openType, genericArguments); if (patchedType == null) { var typeToken = new Token(0xFE000000 | ++typeTokenCounter); var signatureToken = new Token(0xFD000000 | ++signatureTokenCounter); var sigtype = new TypeSigType(signatureToken, CilElementType.Var); var signature = new GenericInstSigType(sigtype, genericArguments); patchedType = new CilGenericType(enclosingType.InstantiationModule, typeToken, openType.BaseGenericType, signature); AddPatchedType(openType, genericArguments, patchedType); } foreach (var field in patchedType.Fields) { if (field.Name == openField.Name) { return field; } } throw new MissingFieldException(); }
/// <summary> /// Setups the specified compiler. /// </summary> /// <param name="compiler">The compiler.</param> public void Setup(IMethodCompiler compiler) { if (compiler == null) throw new ArgumentNullException(@"compiler"); methodCompiler = compiler; instructionSet = compiler.InstructionSet; basicBlocks = compiler.BasicBlocks; architecture = compiler.Architecture; typeModule = compiler.Method.Module; typeSystem = compiler.TypeSystem; typeLayout = compiler.TypeLayout; callingConvention = architecture.GetCallingConvention(); architecture.GetTypeRequirements(BuiltInSigType.IntPtr, out nativePointerSize, out nativePointerAlignment); }
/// <summary> /// Initializes a new instance of <see cref="RuntimeMember"/>. /// </summary> /// <param name="module">The module.</param> /// <param name="token">Holds the token of this runtime metadata.</param> /// <param name="declaringType">The declaring type of the member.</param> protected RuntimeMember(ITypeModule module, Token token, RuntimeType declaringType) : base(module, token) { this.declaringType = declaringType; this.attributes = new List <RuntimeAttribute>(); }
/// <summary> /// Initializes a new instance of <see cref="RuntimeField"/>. /// </summary> /// <param name="module">The module.</param> /// <param name="declaringType">Specifies the type, which contains this field.</param> public RuntimeField(ITypeModule module, RuntimeType declaringType) : base(module, Token.Zero, declaringType) { }
/// <summary> /// Patches the field. /// </summary> /// <param name="typeModule">The type module.</param> /// <param name="enclosingType">Type of the closed.</param> /// <param name="openField">The open field.</param> /// <returns></returns> RuntimeField IGenericTypePatcher.PatchField(ITypeModule typeModule, CilGenericType enclosingType, RuntimeField openField) { var openType = openField.DeclaringType as CilGenericType; var genericArguments = CloseGenericArguments(enclosingType, openType); var patchedType = GetType(openType, genericArguments); if (patchedType == null) { var typeToken = new Token(0xFE000000 | ++typeTokenCounter); var signatureToken = new Token(0xFD000000 | ++signatureTokenCounter); var sigtype = new TypeSigType(signatureToken, CilElementType.Var); var signature = new GenericInstSigType(sigtype, genericArguments); // FIXME: There has got to be a better way to do this... try { patchedType = new CilGenericType(enclosingType.Module, typeToken, openType.BaseGenericType, signature); } catch (Exception) { foreach (var module in typeModule.TypeSystem.TypeModules) { try { patchedType = new CilGenericType(module, typeToken, openType.BaseGenericType, signature); break; } catch (Exception) { ; } } } AddType(patchedType, genericArguments); } foreach (var field in patchedType.Fields) { if (field.Name == openField.Name) { return field; } } throw new MissingFieldException(); }
public void ResolveInterfaces(ITypeModule typeModule) { foreach (var type in baseGenericType.Interfaces) { if (!type.ContainsOpenGenericParameters) { Interfaces.Add(type); } else { var genericType = type as CilGenericType; Debug.Assert(genericType != null); RuntimeType matchedInterfaceType = null; // -- only needs to search generic type interfaces foreach (var runtimetype in typeModule.GetAllTypes()) { if (runtimetype.IsInterface) { var runtimetypegeneric = runtimetype as CilGenericType; if (runtimetypegeneric != null) { if (genericType.baseGenericType == runtimetypegeneric.baseGenericType) { if (SigType.Equals(GenericArguments, runtimetypegeneric.GenericArguments)) { matchedInterfaceType = runtimetype; //Interfaces.Add(runtimetype); break; } } } } } if (matchedInterfaceType != null) Interfaces.Add(matchedInterfaceType); else continue; } } }
/// <summary> /// Patches the type of the signature. /// </summary> /// <param name="typemodule"></param> /// <param name="token">The token.</param> /// <returns></returns> SigType IGenericTypePatcher.PatchSignatureType(ITypeModule typemodule, RuntimeType enclosingType, Token token) { if (typemodule.MetadataModule == null) return new ClassSigType(token); if (token.Table == TableType.TypeSpec) { var typespecRow = typemodule.MetadataModule.Metadata.ReadTypeSpecRow(token); var signature = new TypeSpecSignature(typemodule.MetadataModule.Metadata, typespecRow.SignatureBlobIdx); if (enclosingType is CilGenericType) { var enclosingGenericType = enclosingType as CilGenericType; if (signature.Type is VarSigType) { return enclosingGenericType.GenericArguments[(signature.Type as VarSigType).Index]; } else if (signature.Type is GenericInstSigType) { var openGenericSigType = (signature.Type as GenericInstSigType); if (openGenericSigType.ContainsGenericParameters) { return new GenericInstSigType(openGenericSigType.BaseType, this.CloseGenericArguments(enclosingGenericType, openGenericSigType)); } } } return signature.Type; } return new ClassSigType(token); }
/// <summary> /// Patches the type. /// </summary> /// <param name="typeModule">The type module.</param> /// <param name="enclosingType">Type of the enclosing.</param> /// <param name="openType">Type of the open.</param> /// <returns></returns> RuntimeType IGenericTypePatcher.PatchType(ITypeModule typeModule, CilGenericType enclosingType, CilGenericType openType) { var genericArguments = CloseGenericArguments(enclosingType, openType); var patchedType = GetType(openType, genericArguments); if (patchedType == null) { var typeToken = new Token(0xFE000000 | ++typeTokenCounter); var signatureToken = new Token(0xFD000000 | ++signatureTokenCounter); var sigtype = new TypeSigType(signatureToken, CilElementType.Var); var signature = new GenericInstSigType(sigtype, genericArguments); patchedType = new CilGenericType(enclosingType.InstantiationModule, typeToken, openType.BaseGenericType, signature); AddType(patchedType, genericArguments); } return patchedType; }
/// <summary> /// Patches the method. /// </summary> /// <param name="typeModule">The type module.</param> /// <param name="enclosingType">Type of the enclosing.</param> /// <param name="openMethod">The open method.</param> /// <returns></returns> RuntimeMethod IGenericTypePatcher.PatchMethod(ITypeModule typeModule, CilGenericType enclosingType, RuntimeMethod openMethod) { var openType = openMethod.DeclaringType as CilGenericType; var genericArguments = CloseGenericArguments(enclosingType, openType); var patchedType = GetType(openType, genericArguments); if (patchedType == null) { var typeToken = new Token(0xFE000000 | ++typeTokenCounter); var signatureToken = new Token(0xFD000000 | ++signatureTokenCounter); var sigtype = new TypeSigType(signatureToken, CilElementType.Var); var signature = new GenericInstSigType(sigtype, genericArguments); patchedType = new CilGenericType(enclosingType.InstantiationModule, typeToken, openType.BaseGenericType, signature); AddType(patchedType, genericArguments); } var methodIndex = GetMethodIndex(openMethod); return patchedType.Methods[methodIndex]; }
/// <summary> /// Initializes a new instance of the <see cref="BaseMethodCompiler"/> class. /// </summary> /// <param name="linker">The _linker.</param> /// <param name="architecture">The target compilation Architecture.</param> /// <param name="type">The type, which owns the method to compile.</param> /// <param name="method">The method to compile by this instance.</param> protected BaseMethodCompiler( IAssemblyLinker linker, IArchitecture architecture, ICompilationSchedulerStage compilationScheduler, RuntimeType type, RuntimeMethod method, ITypeSystem typeSystem, ITypeLayout typeLayout) { if (architecture == null) throw new ArgumentNullException(@"architecture"); if (linker == null) throw new ArgumentNullException(@"linker"); if (compilationScheduler == null) throw new ArgumentNullException(@"compilationScheduler"); this.linker = linker; this.architecture = architecture; this.method = method; this.type = type; parameters = new List<Operand>(new Operand[method.Parameters.Count]); nextStackSlot = 0; basicBlocks = new List<BasicBlock>(); instructionSet = null; // this will be set later pipeline = new CompilerPipeline(); this.compilationScheduler = compilationScheduler; this.moduleTypeSystem = method.Module; this.typeSystem = typeSystem; this.typeLayout = typeLayout; }
/// <summary> /// Initializes a new instance of <see cref="RuntimeMember"/>. /// </summary> /// <param name="module">The module.</param> /// <param name="token">Holds the token of this runtime metadata.</param> /// <param name="declaringType">The declaring type of the member.</param> protected RuntimeMember(ITypeModule module, Token token, RuntimeType declaringType) : base(module, token) { this.declaringType = declaringType; this.attributes = new List<RuntimeAttribute>(); }
private string GetName(ITypeModule typeModule) { var sb = new StringBuilder(); sb.AppendFormat("{0}<", baseGenericType.Name); foreach (var sigType in genericArguments) { if (sigType.IsOpenGenericParameter) { sb.AppendFormat("{0}, ", sigType.ToString()); } else { var type = GetRuntimeTypeForSigType(sigType, typeModule); if (type != null) sb.AppendFormat("{0}, ", type.FullName); else sb.Append("<null>"); } } sb.Length -= 2; sb.Append(">"); return sb.ToString(); }
/// <summary> /// Initializes a new instance of <see cref="RuntimeField"/>. /// </summary> /// <param name="module">The module.</param> /// <param name="token">The token.</param> /// <param name="declaringType">Specifies the type, which contains this field.</param> public RuntimeField(ITypeModule module, Token token, RuntimeType declaringType) : base(module, token, declaringType) { }
private static RuntimeType GetRuntimeTypeForSigType(SigType sigType, ITypeModule typeModule) { RuntimeType result = null; switch (sigType.Type) { case CilElementType.Class: Debug.Assert(sigType is TypeSigType, @"Failing to resolve VarSigType in GenericType."); result = typeModule.GetType((sigType as TypeSigType).Token); break; case CilElementType.ValueType: goto case CilElementType.Class; case CilElementType.GenericInst: result = typeModule.GetType((sigType as GenericInstSigType).BaseType.Token); break; case CilElementType.Var: throw new NotImplementedException(@"Failing to resolve VarSigType in GenericType."); case CilElementType.MVar: throw new NotImplementedException(@"Failing to resolve VarMSigType in GenericType."); case CilElementType.SZArray: // FIXME return null; default: var builtIn = sigType as BuiltInSigType; if (builtIn != null) { ITypeModule mscorlib; if (typeModule.Name == "mscorlib") mscorlib = typeModule; else mscorlib = typeModule.TypeSystem.ResolveModuleReference("mscorlib"); result = mscorlib.GetType(builtIn.TypeName); } else { throw new NotImplementedException(String.Format("SigType of CilElementType.{0} is not supported.", sigType.Type)); } break; } return result; }
/// <summary> /// Loads the module. /// </summary> /// <param name="modules">The modules.</param> void ITypeSystem.LoadModules(IList<IMetadataModule> modules) { foreach (IMetadataModule module in modules) { ITypeModule typeModule = new TypeModule(this, module); typeModules.Add(typeModule); if (typeModule.MetadataModule.ModuleType == ModuleType.Executable) mainTypeModule = typeModule; } }
/// <summary> /// Initializes a new instance of the <see cref="RuntimeMethod"/> class. /// </summary> /// <param name="token">The token.</param> /// <param name="declaringType">The type, which declared this method.</param> public RuntimeMethod(ITypeModule module, Token token, RuntimeType declaringType) : base(module, token, declaringType) { this.genericParameters = new List <GenericParameter>(); }
/// <summary> /// Initializes a new instance of <see cref="RuntimeObject"/>. /// </summary> /// <param name="module">The module.</param> /// <param name="token">The runtime token of this metadata.</param> protected RuntimeObject(ITypeModule module, Token token) { this.module = module; this.token = token; }
/// <summary> /// Initializes a new instance of the <see cref="RuntimeType"/> class. /// </summary> /// <param name="module">The module.</param> /// <param name="token">The token of the type.</param> /// <param name="baseType">Type of the base.</param> public RuntimeType(ITypeModule module, Token token, RuntimeType baseType) : base(module, token, null) { this.baseType = baseType; if (baseType == null) { this.isValueType = false; this.isDelegate = false; this.isEnum = false; } else { if (baseType.isValueType) this.isValueType = true; else if (baseType.FullName == "System.ValueType") this.isValueType = true; if (baseType.isDelegate) this.isDelegate = true; else if (baseType.FullName == "System.Delegate") this.isDelegate = true; if (baseType.isEnum) this.isEnum = true; else if (baseType.FullName == "System.Enum") this.isEnum = true; } this.fields = new List<RuntimeField>(); this.methods = new List<RuntimeMethod>(); this.interfaces = new List<RuntimeType>(); this.genericParameters = new List<GenericParameter>(); }
/// <summary> /// Initializes a new instance of the <see cref="BaseMethodCompiler"/> class. /// </summary> /// <param name="assemblyCompiler">The assembly compiler.</param> /// <param name="type">The type, which owns the method to compile.</param> /// <param name="method">The method to compile by this instance.</param> /// <param name="instructionSet">The instruction set.</param> /// <param name="compilationScheduler">The compilation scheduler.</param> protected BaseMethodCompiler(AssemblyCompiler assemblyCompiler, RuntimeType type, RuntimeMethod method, InstructionSet instructionSet, ICompilationSchedulerStage compilationScheduler) { if (compilationScheduler == null) throw new ArgumentNullException(@"compilationScheduler"); this.assemblyCompiler = assemblyCompiler; this.method = method; this.type = type; this.compilationScheduler = compilationScheduler; this.moduleTypeSystem = method.Module; this.architecture = assemblyCompiler.Architecture; this.typeSystem = assemblyCompiler.TypeSystem; this.typeLayout = AssemblyCompiler.TypeLayout; this.internalTrace = AssemblyCompiler.InternalTrace; this.linker = assemblyCompiler.Pipeline.FindFirst<IAssemblyLinker>(); this.plugSystem = assemblyCompiler.Pipeline.FindFirst<IPlugSystem>(); this.parameters = new List<Operand>(new Operand[method.Parameters.Count]); this.basicBlocks = new BasicBlocks(); this.instructionSet = instructionSet ?? new InstructionSet(256); this.pipeline = new CompilerPipeline(); this.stackLayout = new StackLayout(architecture, method.Parameters.Count + (method.Signature.HasThis || method.Signature.HasExplicitThis ? 1 : 0)); this.virtualRegisterLayout = new VirtualRegisterLayout(architecture, stackLayout); EvaluateParameterOperands(); }
/// <summary> /// Loads the interfaces. /// </summary> protected void LoadTypeReferences() { Token maxToken = GetMaxTokenValue(TableType.TypeRef); foreach (Token token in new Token(TableType.TypeRef, 1).Upto(maxToken)) { RuntimeType runtimeType = null; TypeRefRow row = metadataProvider.ReadTypeRefRow(token); string typeName = GetString(row.TypeNameIdx); switch (row.ResolutionScope.Table) { case TableType.Module: goto case TableType.TypeRef; case TableType.TypeDef: throw new NotImplementedException(); case TableType.TypeRef: { TypeRefRow row2 = metadataProvider.ReadTypeRefRow(row.ResolutionScope); string typeName2 = GetString(row2.TypeNameIdx); string typeNamespace2 = GetString(row2.TypeNamespaceIdx) + "." + typeName2; AssemblyRefRow asmRefRow = metadataProvider.ReadAssemblyRefRow(row2.ResolutionScope); string assemblyName = GetString(asmRefRow.Name); ITypeModule module = typeSystem.ResolveModuleReference(assemblyName); runtimeType = module.GetType(typeNamespace2, typeName); if (runtimeType == null) { throw new TypeLoadException("Could not find type: " + typeNamespace2 + Type.Delimiter + typeName); } break; } case TableType.TypeSpec: throw new NotImplementedException(); case TableType.ModuleRef: throw new NotImplementedException(); case TableType.AssemblyRef: { string typeNamespace = GetString(row.TypeNamespaceIdx); AssemblyRefRow asmRefRow = metadataProvider.ReadAssemblyRefRow(row.ResolutionScope); string assemblyName = GetString(asmRefRow.Name); ITypeModule module = typeSystem.ResolveModuleReference(assemblyName); runtimeType = module.GetType(typeNamespace, typeName); if (runtimeType == null) { throw new TypeLoadException("Could not find type: " + typeNamespace + Type.Delimiter + typeName); } break; } default: throw new NotImplementedException(); } typeRef[token.RID - 1] = runtimeType; } }
/// <summary> /// Initializes a new instance of the <see cref="RuntimeMethod"/> class. /// </summary> /// <param name="token">The token.</param> /// <param name="declaringType">The type, which declared this method.</param> public RuntimeMethod(ITypeModule module, Token token, RuntimeType declaringType) : base(module, token, declaringType) { this.genericParameters = new List<GenericParameter>(); }
/// <summary> /// Initializes a new instance of the <see cref="BaseMethodCompiler"/> class. /// </summary> /// <param name="compiler">The assembly compiler.</param> /// <param name="method">The method to compile by this instance.</param> /// <param name="instructionSet">The instruction set.</param> protected BaseMethodCompiler(BaseCompiler compiler, RuntimeMethod method, InstructionSet instructionSet) { this.compiler = compiler; this.method = method; this.type = method.DeclaringType; this.compilationScheduler = compiler.Scheduler; this.moduleTypeSystem = method.Module; this.architecture = compiler.Architecture; this.typeSystem = compiler.TypeSystem; this.typeLayout = Compiler.TypeLayout; this.internalTrace = Compiler.InternalTrace; this.linker = compiler.Linker; this.basicBlocks = new BasicBlocks(); this.instructionSet = instructionSet ?? new InstructionSet(256); this.pipeline = new CompilerPipeline(); this.stackLayout = new StackLayout(architecture, method.Parameters.Count + (method.Signature.HasThis || method.Signature.HasExplicitThis ? 1 : 0)); this.virtualRegisterLayout = new VirtualRegisterLayout(architecture, stackLayout); EvaluateParameterOperands(); this.stopMethodCompiler = false; }