X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Fcodegen.cs;h=79dee5e3a56b419fade9e0c1bbdc2e4ac0a2c7bf;hb=394140f5a495ff5fc718249c1406735a001edf85;hp=5f7d7bc512df61c813a181917eb4df7df442aa55;hpb=feb700323765777d4efff14ed48f200c104b3127;p=mono.git diff --git a/mcs/mcs/codegen.cs b/mcs/mcs/codegen.cs index 5f7d7bc512d..79dee5e3a56 100644 --- a/mcs/mcs/codegen.cs +++ b/mcs/mcs/codegen.cs @@ -1,325 +1,188 @@ // // codegen.cs: The code generator // -// Author: +// Authors: // Miguel de Icaza (miguel@ximian.com) +// Marek Safar (marek.safar@gmail.com) // // Copyright 2001, 2002, 2003 Ximian, Inc. // Copyright 2004 Novell, Inc. // -// -// Please leave this defined on SVN: The idea is that when we ship the -// compiler to end users, if the compiler crashes, they have a chance -// to narrow down the problem. -// -// Only remove it if you need to debug locally on your tree. -// -//#define PRODUCTION - using System; -using System.IO; -using System.Collections; using System.Collections.Generic; -using System.Globalization; -using System.Reflection; -using System.Reflection.Emit; -using System.Runtime.InteropServices; -using System.Security; -using System.Security.Cryptography; -using System.Security.Permissions; - -using Mono.Security.Cryptography; - -namespace Mono.CSharp { - - /// - /// Code generator class. - /// - public class CodeGen { - static AppDomain current_domain; - - public static AssemblyClass Assembly; - - static CodeGen () - { - Reset (); - } - - public static void Reset () - { - Assembly = new AssemblyClass (); - } - - public static string Basename (string name) - { - int pos = name.LastIndexOf ('/'); - - if (pos != -1) - return name.Substring (pos + 1); - - pos = name.LastIndexOf ('\\'); - if (pos != -1) - return name.Substring (pos + 1); - - return name; - } - - public static string Dirname (string name) - { - int pos = name.LastIndexOf ('/'); - - if (pos != -1) - return name.Substring (0, pos); - - pos = name.LastIndexOf ('\\'); - if (pos != -1) - return name.Substring (0, pos); - - return "."; - } - - static public string FileName; -#if MS_COMPATIBLE - const AssemblyBuilderAccess COMPILER_ACCESS = 0; +#if STATIC +using MetaType = IKVM.Reflection.Type; +using IKVM.Reflection; +using IKVM.Reflection.Emit; #else - /* Keep this in sync with System.Reflection.Emit.AssemblyBuilder */ - const AssemblyBuilderAccess COMPILER_ACCESS = (AssemblyBuilderAccess) 0x800; +using MetaType = System.Type; +using System.Reflection; +using System.Reflection.Emit; #endif - - // - // Initializes the code generator variables for interactive use (repl) - // - static public void InitDynamic (CompilerContext ctx, string name) - { - current_domain = AppDomain.CurrentDomain; - AssemblyName an = Assembly.GetAssemblyName (name, name); - - Assembly.Builder = current_domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Run | COMPILER_ACCESS); - RootContext.ToplevelTypes = new ModuleCompiled (ctx, true); - RootContext.ToplevelTypes.Builder = Assembly.Builder.DefineDynamicModule (Basename (name), false); - Assembly.Name = Assembly.Builder.GetName (); - } - - // - // Initializes the code generator variables - // - static public bool Init (string name, string output, bool want_debugging_support, CompilerContext ctx) - { - FileName = output; - AssemblyName an = Assembly.GetAssemblyName (name, output); - if (an == null) - return false; - - if (an.KeyPair != null) { - // If we are going to strong name our assembly make - // sure all its refs are strong named - foreach (Assembly a in GlobalRootNamespace.Instance.Assemblies) { - AssemblyName ref_name = a.GetName (); - byte [] b = ref_name.GetPublicKeyToken (); - if (b == null || b.Length == 0) { - ctx.Report.Error (1577, "Assembly generation failed " + - "-- Referenced assembly '" + - ref_name.Name + - "' does not have a strong name."); - //Environment.Exit (1); - } - } - } - - current_domain = AppDomain.CurrentDomain; - - try { - Assembly.Builder = current_domain.DefineDynamicAssembly (an, - AssemblyBuilderAccess.RunAndSave | COMPILER_ACCESS, Dirname (name)); - } - catch (ArgumentException) { - // specified key may not be exportable outside it's container - if (RootContext.StrongNameKeyContainer != null) { - ctx.Report.Error (1548, "Could not access the key inside the container `" + - RootContext.StrongNameKeyContainer + "'."); - Environment.Exit (1); - } - throw; - } - catch (CryptographicException) { - if ((RootContext.StrongNameKeyContainer != null) || (RootContext.StrongNameKeyFile != null)) { - ctx.Report.Error (1548, "Could not use the specified key to strongname the assembly."); - Environment.Exit (1); - } - return false; - } - - // Get the complete AssemblyName from the builder - // (We need to get the public key and token) - Assembly.Name = Assembly.Builder.GetName (); - - // - // Pass a path-less name to DefineDynamicModule. Wonder how - // this copes with output in different directories then. - // FIXME: figure out how this copes with --output /tmp/blah - // - // If the third argument is true, the ModuleBuilder will dynamically - // load the default symbol writer. - // - try { - RootContext.ToplevelTypes.Builder = Assembly.Builder.DefineDynamicModule ( - Basename (name), Basename (output), want_debugging_support); - -#if !MS_COMPATIBLE - // TODO: We should use SymbolWriter from DefineDynamicModule - if (want_debugging_support && !SymbolWriter.Initialize (RootContext.ToplevelTypes.Builder, output)) { - ctx.Report.Error (40, "Unexpected debug information initialization error `{0}'", - "Could not find the symbol writer assembly (Mono.CompilerServices.SymbolWriter.dll)"); - return false; - } -#endif - } catch (ExecutionEngineException e) { - ctx.Report.Error (40, "Unexpected debug information initialization error `{0}'", - e.Message); - return false; - } - - return true; - } - - static public void Save (string name, bool saveDebugInfo, Report Report) - { - PortableExecutableKinds pekind; - ImageFileMachine machine; - - switch (RootContext.Platform) { - case Platform.X86: - pekind = PortableExecutableKinds.Required32Bit; - machine = ImageFileMachine.I386; - break; - case Platform.X64: - pekind = PortableExecutableKinds.PE32Plus; - machine = ImageFileMachine.AMD64; - break; - case Platform.IA64: - pekind = PortableExecutableKinds.PE32Plus; - machine = ImageFileMachine.IA64; - break; - case Platform.AnyCPU: - default: - pekind = PortableExecutableKinds.ILOnly; - machine = ImageFileMachine.I386; - break; - } - try { - Assembly.Builder.Save (Basename (name), pekind, machine); - } - catch (COMException) { - if ((RootContext.StrongNameKeyFile == null) || (!RootContext.StrongNameDelaySign)) - throw; - - // FIXME: it seems Microsoft AssemblyBuilder doesn't like to delay sign assemblies - Report.Error (1548, "Couldn't delay-sign the assembly with the '" + - RootContext.StrongNameKeyFile + - "', Use MCS with the Mono runtime or CSC to compile this assembly."); - } - catch (System.IO.IOException io) { - Report.Error (16, "Could not write to file `"+name+"', cause: " + io.Message); - return; - } - catch (System.UnauthorizedAccessException ua) { - Report.Error (16, "Could not write to file `"+name+"', cause: " + ua.Message); - return; - } - catch (System.NotImplementedException nie) { - Report.RuntimeMissingSupport (Location.Null, nie.Message); - return; - } - - // - // Write debuger symbol file - // - if (saveDebugInfo) - SymbolWriter.WriteSymbolFile (); - } - } +namespace Mono.CSharp +{ /// /// An Emit Context is created for each body of code (from methods, /// properties bodies, indexer bodies or constructor bodies) /// public class EmitContext : BuilderContext { - public ILGenerator ig; + // TODO: Has to be private + public readonly ILGenerator ig; /// /// The value that is allowed to be returned or NULL if there is no /// return type. /// - Type return_type; + readonly TypeSpec return_type; /// /// Keeps track of the Type to LocalBuilder temporary storage created /// to store structures (used to compute the address of the structure /// value on structure method invocations) /// - Hashtable temporary_storage; + Dictionary temporary_storage; /// /// The location where we store the return value. /// public LocalBuilder return_value; + + /// + /// Current loop begin and end labels. + /// + public Label LoopBegin, LoopEnd; + /// - /// The location where return has to jump to return the - /// value + /// Default target in a switch statement. Only valid if + /// InSwitch is true /// - public Label ReturnLabel; + public Label DefaultTarget; /// - /// If we already defined the ReturnLabel + /// If this is non-null, points to the current switch statement /// - public bool HasReturnLabel; + public Switch Switch; /// /// Whether we are inside an anonymous method. /// public AnonymousExpression CurrentAnonymousMethod; - public readonly IMemberContext MemberContext; + readonly IMemberContext member_context; - public EmitContext (IMemberContext rc, ILGenerator ig, Type return_type) + DynamicSiteClass dynamic_site_container; + + Label? return_label; + + public EmitContext (IMemberContext rc, ILGenerator ig, TypeSpec return_type) { - this.MemberContext = rc; + this.member_context = rc; this.ig = ig; - this.return_type = return_type; + + if (rc.Module.Compiler.Settings.Checked) + flags |= Options.CheckedScope; + +#if STATIC + ig.__CleverExceptionBlockAssistance (); +#endif + } + + #region Properties + + internal AsyncTaskStorey AsyncTaskStorey { + get { + return CurrentAnonymousMethod.Storey as AsyncTaskStorey; + } + } + + public BuiltinTypes BuiltinTypes { + get { + return MemberContext.Module.Compiler.BuiltinTypes; + } } - public Type CurrentType { - get { return MemberContext.CurrentType; } + public TypeSpec CurrentType { + get { return member_context.CurrentType; } } public TypeParameter[] CurrentTypeParameters { - get { return MemberContext.CurrentTypeParameters; } + get { return member_context.CurrentTypeParameters; } } - public TypeContainer CurrentTypeDefinition { - get { return MemberContext.CurrentTypeDefinition; } + public MemberCore CurrentTypeDefinition { + get { return member_context.CurrentMemberDefinition; } + } + + public bool HasReturnLabel { + get { + return return_label.HasValue; + } } public bool IsStatic { - get { return MemberContext.IsStatic; } + get { return member_context.IsStatic; } } - // Has to be used for emitter errors only + public bool IsAnonymousStoreyMutateRequired { + get { + return CurrentAnonymousMethod != null && + CurrentAnonymousMethod.Storey != null && + CurrentAnonymousMethod.Storey.Mutator != null; + } + } + + public IMemberContext MemberContext { + get { + return member_context; + } + } + + public ModuleContainer Module { + get { + return member_context.Module; + } + } + + // Has to be used for specific emitter errors only any + // possible resolver errors have to be reported during Resolve public Report Report { - get { return MemberContext.Compiler.Report; } + get { + return member_context.Module.Compiler.Report; + } } - public Type ReturnType { + public TypeSpec ReturnType { get { return return_type; } } + // + // The label where we have to jump before leaving the context + // + public Label ReturnLabel { + get { + return return_label.Value; + } + } + + #endregion + + public void AssertEmptyStack () + { +#if STATIC + if (ig.__StackHeight != 0) + throw new InternalErrorException ("Await yields with non-empty stack in `{0}", + member_context.GetSignatureForError ()); +#endif + } + /// /// This is called immediately before emitting an IL opcode to tell the symbol /// writer to which source line this opcode belongs. @@ -337,658 +200,833 @@ namespace Mono.CSharp { SymbolWriter.DefineLocalVariable (name, builder); } - public void BeginScope () + public void BeginCatchBlock (TypeSpec type) { - ig.BeginScope(); - SymbolWriter.OpenScope(ig); + ig.BeginCatchBlock (type.GetMetaInfo ()); } - public void EndScope () + public void BeginExceptionBlock () { - ig.EndScope(); - SymbolWriter.CloseScope(ig); + ig.BeginExceptionBlock (); } - /// - /// Returns a temporary storage for a variable of type t as - /// a local variable in the current body. - /// - public LocalBuilder GetTemporaryLocal (Type t) + public void BeginFinallyBlock () { - if (temporary_storage != null) { - object o = temporary_storage [t]; - if (o != null) { - if (o is Stack) { - Stack s = (Stack) o; - o = s.Count == 0 ? null : s.Pop (); - } else { - temporary_storage.Remove (t); - } - } - if (o != null) - return (LocalBuilder) o; - } - return ig.DeclareLocal (TypeManager.TypeToReflectionType (t)); + ig.BeginFinallyBlock (); } - public void FreeTemporaryLocal (LocalBuilder b, Type t) + public void BeginScope () { - if (temporary_storage == null) { - temporary_storage = new Hashtable (); - temporary_storage [t] = b; - return; - } - object o = temporary_storage [t]; - if (o == null) { - temporary_storage [t] = b; - return; - } - Stack s = o as Stack; - if (s == null) { - s = new Stack (); - s.Push (o); - temporary_storage [t] = s; - } - s.Push (b); + SymbolWriter.OpenScope(ig); } - /// - /// Current loop begin and end labels. - /// - public Label LoopBegin, LoopEnd; - - /// - /// Default target in a switch statement. Only valid if - /// InSwitch is true - /// - public Label DefaultTarget; + public void EndExceptionBlock () + { + ig.EndExceptionBlock (); + } - /// - /// If this is non-null, points to the current switch statement - /// - public Switch Switch; + public void EndScope () + { + SymbolWriter.CloseScope(ig); + } - /// - /// ReturnValue creates on demand the LocalBuilder for the - /// return value from the function. By default this is not - /// used. This is only required when returns are found inside - /// Try or Catch statements. - /// - /// This method is typically invoked from the Emit phase, so - /// we allow the creation of a return label if it was not - /// requested during the resolution phase. Could be cleaned - /// up, but it would replicate a lot of logic in the Emit phase - /// of the code that uses it. - /// - public LocalBuilder TemporaryReturn () + // + // Creates a nested container in this context for all dynamic compiler generated stuff + // + internal DynamicSiteClass CreateDynamicSite () { - if (return_value == null){ - return_value = ig.DeclareLocal (return_type); - if (!HasReturnLabel){ - ReturnLabel = ig.DefineLabel (); - HasReturnLabel = true; - } + if (dynamic_site_container == null) { + var mc = member_context.CurrentMemberDefinition as MemberBase; + dynamic_site_container = new DynamicSiteClass (CurrentTypeDefinition.Parent.PartialContainer, mc, CurrentTypeParameters); + + CurrentTypeDefinition.Module.AddCompilerGeneratedClass (dynamic_site_container); + dynamic_site_container.CreateType (); + dynamic_site_container.DefineType (); + dynamic_site_container.ResolveTypeParameters (); + dynamic_site_container.Define (); + + var inflator = new TypeParameterInflator (Module, CurrentType, TypeParameterSpec.EmptyTypes, TypeSpec.EmptyTypes); + var inflated = dynamic_site_container.CurrentType.InflateMember (inflator); + CurrentType.MemberCache.AddMember (inflated); } - return return_value; + return dynamic_site_container; } - } - public abstract class CommonAssemblyModulClass : Attributable, IMemberContext - { - public void AddAttributes (List attrs, IMemberContext context) + public Label CreateReturnLabel () { - foreach (Attribute a in attrs) - a.AttachTo (this, context); + if (!return_label.HasValue) + return_label = DefineLabel (); - if (attributes == null) { - attributes = new Attributes (attrs); - return; - } - attributes.AddAttributes (attrs); + return return_label.Value; } - public virtual void Emit (TypeContainer tc) + public LocalBuilder DeclareLocal (TypeSpec type, bool pinned) { - if (OptAttributes == null) - return; + if (IsAnonymousStoreyMutateRequired) + type = CurrentAnonymousMethod.Storey.Mutator.Mutate (type); - OptAttributes.Emit (); + return ig.DeclareLocal (type.GetMetaInfo (), pinned); } - protected Attribute ResolveAttribute (PredefinedAttribute a_type) + public Label DefineLabel () { - Attribute a = OptAttributes.Search (a_type); - if (a != null) { - a.Resolve (); - } - return a; + return ig.DefineLabel (); } - #region IMemberContext Members - - public CompilerContext Compiler { - get { return RootContext.ToplevelTypes.Compiler; } + // + // Creates temporary field in current async storey + // + public FieldExpr GetTemporaryField (TypeSpec type) + { + var f = AsyncTaskStorey.AddCapturedLocalVariable (type); + var fexpr = new FieldExpr (f, Location.Null); + fexpr.InstanceExpression = new CompilerGeneratedThis (CurrentType, Location.Null); + return fexpr; } - public Type CurrentType { - get { return null; } + public void MarkLabel (Label label) + { + ig.MarkLabel (label); } - public TypeParameter[] CurrentTypeParameters { - get { return null; } + public void Emit (OpCode opcode) + { + ig.Emit (opcode); } - public TypeContainer CurrentTypeDefinition { - get { return RootContext.ToplevelTypes; } + public void Emit (OpCode opcode, LocalBuilder local) + { + ig.Emit (opcode, local); } - public string GetSignatureForError () + public void Emit (OpCode opcode, string arg) { - return ""; + ig.Emit (opcode, arg); } - public bool IsObsolete { - get { return false; } + public void Emit (OpCode opcode, double arg) + { + ig.Emit (opcode, arg); } - public bool IsUnsafe { - get { return false; } + public void Emit (OpCode opcode, float arg) + { + ig.Emit (opcode, arg); } - public bool IsStatic { - get { return false; } + public void Emit (OpCode opcode, Label label) + { + ig.Emit (opcode, label); } - public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc) + public void Emit (OpCode opcode, Label[] labels) { - throw new NotImplementedException (); + ig.Emit (opcode, labels); } - public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104) + public void Emit (OpCode opcode, TypeSpec type) { - return RootContext.ToplevelTypes.LookupNamespaceOrType (name, loc, ignore_cs0104); + if (IsAnonymousStoreyMutateRequired) + type = CurrentAnonymousMethod.Storey.Mutator.Mutate (type); + + ig.Emit (opcode, type.GetMetaInfo ()); } - public FullNamedExpression LookupNamespaceAlias (string name) + public void Emit (OpCode opcode, FieldSpec field) { - return null; - } + if (IsAnonymousStoreyMutateRequired) + field = field.Mutate (CurrentAnonymousMethod.Storey.Mutator); - #endregion - } - - public class AssemblyClass : CommonAssemblyModulClass { - // TODO: make it private and move all builder based methods here - public AssemblyBuilder Builder; - bool is_cls_compliant; - bool wrap_non_exception_throws; + ig.Emit (opcode, field.GetMetaInfo ()); + } - public Attribute ClsCompliantAttribute; + public void Emit (OpCode opcode, MethodSpec method) + { + if (IsAnonymousStoreyMutateRequired) + method = method.Mutate (CurrentAnonymousMethod.Storey.Mutator); - Dictionary declarative_security; - bool has_extension_method; - public AssemblyName Name; - MethodInfo add_type_forwarder; - Dictionary emitted_forwarders; + if (method.IsConstructor) + ig.Emit (opcode, (ConstructorInfo) method.GetMetaInfo ()); + else + ig.Emit (opcode, (MethodInfo) method.GetMetaInfo ()); + } - // Module is here just because of error messages - static string[] attribute_targets = new string [] { "assembly", "module" }; + // TODO: REMOVE breaks mutator + public void Emit (OpCode opcode, MethodInfo method) + { + ig.Emit (opcode, method); + } - public AssemblyClass () + public void Emit (OpCode opcode, MethodSpec method, MetaType[] vargs) { - wrap_non_exception_throws = true; + // TODO MemberCache: This should mutate too + ig.EmitCall (opcode, (MethodInfo) method.GetMetaInfo (), vargs); } - public bool HasExtensionMethods { - set { - has_extension_method = value; + public void EmitArrayNew (ArrayContainer ac) + { + if (ac.Rank == 1) { + var type = IsAnonymousStoreyMutateRequired ? + CurrentAnonymousMethod.Storey.Mutator.Mutate (ac.Element) : + ac.Element; + + ig.Emit (OpCodes.Newarr, type.GetMetaInfo ()); + } else { + if (IsAnonymousStoreyMutateRequired) + ac = (ArrayContainer) ac.Mutate (CurrentAnonymousMethod.Storey.Mutator); + + ig.Emit (OpCodes.Newobj, ac.GetConstructor ()); } } - public bool IsClsCompliant { - get { - return is_cls_compliant; + public void EmitArrayAddress (ArrayContainer ac) + { + if (ac.Rank > 1) { + if (IsAnonymousStoreyMutateRequired) + ac = (ArrayContainer) ac.Mutate (CurrentAnonymousMethod.Storey.Mutator); + + ig.Emit (OpCodes.Call, ac.GetAddressMethod ()); + } else { + var type = IsAnonymousStoreyMutateRequired ? + CurrentAnonymousMethod.Storey.Mutator.Mutate (ac.Element) : + ac.Element; + + ig.Emit (OpCodes.Ldelema, type.GetMetaInfo ()); } } - public bool WrapNonExceptionThrows { - get { - return wrap_non_exception_throws; + // + // Emits the right opcode to load from an array + // + public void EmitArrayLoad (ArrayContainer ac) + { + if (ac.Rank > 1) { + if (IsAnonymousStoreyMutateRequired) + ac = (ArrayContainer) ac.Mutate (CurrentAnonymousMethod.Storey.Mutator); + + ig.Emit (OpCodes.Call, ac.GetGetMethod ()); + return; } - } - public override AttributeTargets AttributeTargets { - get { - return AttributeTargets.Assembly; + + var type = ac.Element; + if (type.Kind == MemberKind.Enum) + type = EnumSpec.GetUnderlyingType (type); + + switch (type.BuiltinType) { + case BuiltinTypeSpec.Type.Byte: + case BuiltinTypeSpec.Type.Bool: + ig.Emit (OpCodes.Ldelem_U1); + break; + case BuiltinTypeSpec.Type.SByte: + ig.Emit (OpCodes.Ldelem_I1); + break; + case BuiltinTypeSpec.Type.Short: + ig.Emit (OpCodes.Ldelem_I2); + break; + case BuiltinTypeSpec.Type.UShort: + case BuiltinTypeSpec.Type.Char: + ig.Emit (OpCodes.Ldelem_U2); + break; + case BuiltinTypeSpec.Type.Int: + ig.Emit (OpCodes.Ldelem_I4); + break; + case BuiltinTypeSpec.Type.UInt: + ig.Emit (OpCodes.Ldelem_U4); + break; + case BuiltinTypeSpec.Type.ULong: + case BuiltinTypeSpec.Type.Long: + ig.Emit (OpCodes.Ldelem_I8); + break; + case BuiltinTypeSpec.Type.Float: + ig.Emit (OpCodes.Ldelem_R4); + break; + case BuiltinTypeSpec.Type.Double: + ig.Emit (OpCodes.Ldelem_R8); + break; + case BuiltinTypeSpec.Type.IntPtr: + ig.Emit (OpCodes.Ldelem_I); + break; + default: + switch (type.Kind) { + case MemberKind.Struct: + if (IsAnonymousStoreyMutateRequired) + type = CurrentAnonymousMethod.Storey.Mutator.Mutate (type); + + ig.Emit (OpCodes.Ldelema, type.GetMetaInfo ()); + ig.Emit (OpCodes.Ldobj, type.GetMetaInfo ()); + break; + case MemberKind.TypeParameter: + if (IsAnonymousStoreyMutateRequired) + type = CurrentAnonymousMethod.Storey.Mutator.Mutate (type); + + ig.Emit (OpCodes.Ldelem, type.GetMetaInfo ()); + break; + case MemberKind.PointerType: + ig.Emit (OpCodes.Ldelem_I); + break; + default: + ig.Emit (OpCodes.Ldelem_Ref); + break; + } + break; } } - public override bool IsClsComplianceRequired () + // + // Emits the right opcode to store to an array + // + public void EmitArrayStore (ArrayContainer ac) { - return is_cls_compliant; + if (ac.Rank > 1) { + if (IsAnonymousStoreyMutateRequired) + ac = (ArrayContainer) ac.Mutate (CurrentAnonymousMethod.Storey.Mutator); + + ig.Emit (OpCodes.Call, ac.GetSetMethod ()); + return; + } + + var type = ac.Element; + + if (type.Kind == MemberKind.Enum) + type = EnumSpec.GetUnderlyingType (type); + + switch (type.BuiltinType) { + case BuiltinTypeSpec.Type.Byte: + case BuiltinTypeSpec.Type.SByte: + case BuiltinTypeSpec.Type.Bool: + Emit (OpCodes.Stelem_I1); + return; + case BuiltinTypeSpec.Type.Short: + case BuiltinTypeSpec.Type.UShort: + case BuiltinTypeSpec.Type.Char: + Emit (OpCodes.Stelem_I2); + return; + case BuiltinTypeSpec.Type.Int: + case BuiltinTypeSpec.Type.UInt: + Emit (OpCodes.Stelem_I4); + return; + case BuiltinTypeSpec.Type.Long: + case BuiltinTypeSpec.Type.ULong: + Emit (OpCodes.Stelem_I8); + return; + case BuiltinTypeSpec.Type.Float: + Emit (OpCodes.Stelem_R4); + return; + case BuiltinTypeSpec.Type.Double: + Emit (OpCodes.Stelem_R8); + return; + } + + switch (type.Kind) { + case MemberKind.Struct: + Emit (OpCodes.Stobj, type); + break; + case MemberKind.TypeParameter: + Emit (OpCodes.Stelem, type); + break; + case MemberKind.PointerType: + Emit (OpCodes.Stelem_I); + break; + default: + Emit (OpCodes.Stelem_Ref); + break; + } } - Report Report { - get { return Compiler.Report; } + public void EmitInt (int i) + { + EmitIntConstant (i); } - public void Resolve () + void EmitIntConstant (int i) { - if (RootContext.Unsafe) { - // - // Emits [assembly: SecurityPermissionAttribute (SecurityAction.RequestMinimum, SkipVerification = true)] - // when -unsafe option was specified - // - - Location loc = Location.Null; + switch (i) { + case -1: + ig.Emit (OpCodes.Ldc_I4_M1); + break; - MemberAccess system_security_permissions = new MemberAccess (new MemberAccess ( - new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Security", loc), "Permissions", loc); + case 0: + ig.Emit (OpCodes.Ldc_I4_0); + break; - Arguments pos = new Arguments (1); - pos.Add (new Argument (new MemberAccess (new MemberAccess (system_security_permissions, "SecurityAction", loc), "RequestMinimum"))); + case 1: + ig.Emit (OpCodes.Ldc_I4_1); + break; - Arguments named = new Arguments (1); - named.Add (new NamedArgument ("SkipVerification", loc, new BoolLiteral (true, loc))); + case 2: + ig.Emit (OpCodes.Ldc_I4_2); + break; - GlobalAttribute g = new GlobalAttribute (new NamespaceEntry (null, null, null), "assembly", - new MemberAccess (system_security_permissions, "SecurityPermissionAttribute"), - new Arguments[] { pos, named }, loc, false); - g.AttachTo (this, this); + case 3: + ig.Emit (OpCodes.Ldc_I4_3); + break; - if (g.Resolve () != null) { - declarative_security = new Dictionary (); - g.ExtractSecurityPermissionSet (declarative_security); - } - } + case 4: + ig.Emit (OpCodes.Ldc_I4_4); + break; - if (OptAttributes == null) - return; + case 5: + ig.Emit (OpCodes.Ldc_I4_5); + break; - // Ensure that we only have GlobalAttributes, since the Search isn't safe with other types. - if (!OptAttributes.CheckTargets()) - return; + case 6: + ig.Emit (OpCodes.Ldc_I4_6); + break; - ClsCompliantAttribute = ResolveAttribute (PredefinedAttributes.Get.CLSCompliant); + case 7: + ig.Emit (OpCodes.Ldc_I4_7); + break; - if (ClsCompliantAttribute != null) { - is_cls_compliant = ClsCompliantAttribute.GetClsCompliantAttributeValue (); - } + case 8: + ig.Emit (OpCodes.Ldc_I4_8); + break; - Attribute a = ResolveAttribute (PredefinedAttributes.Get.RuntimeCompatibility); - if (a != null) { - object val = a.GetPropertyValue ("WrapNonExceptionThrows"); - if (val != null) - wrap_non_exception_throws = (bool) val; + default: + if (i >= -128 && i <= 127) { + ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i); + } else + ig.Emit (OpCodes.Ldc_I4, i); + break; } } - // fix bug #56621 - private void SetPublicKey (AssemblyName an, byte[] strongNameBlob) + public void EmitLong (long l) { - try { - // check for possible ECMA key - if (strongNameBlob.Length == 16) { - // will be rejected if not "the" ECMA key - an.SetPublicKey (strongNameBlob); - } - else { - // take it, with or without, a private key - RSA rsa = CryptoConvert.FromCapiKeyBlob (strongNameBlob); - // and make sure we only feed the public part to Sys.Ref - byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob (rsa); - - // AssemblyName.SetPublicKey requires an additional header - byte[] publicKeyHeader = new byte [12] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00 }; - - byte[] encodedPublicKey = new byte [12 + publickey.Length]; - Buffer.BlockCopy (publicKeyHeader, 0, encodedPublicKey, 0, 12); - Buffer.BlockCopy (publickey, 0, encodedPublicKey, 12, publickey.Length); - an.SetPublicKey (encodedPublicKey); - } + if (l >= int.MinValue && l <= int.MaxValue) { + EmitIntConstant (unchecked ((int) l)); + ig.Emit (OpCodes.Conv_I8); + } else if (l >= 0 && l <= uint.MaxValue) { + EmitIntConstant (unchecked ((int) l)); + ig.Emit (OpCodes.Conv_U8); + } else { + ig.Emit (OpCodes.Ldc_I8, l); } - catch (Exception) { - Error_AssemblySigning ("The specified file `" + RootContext.StrongNameKeyFile + "' is incorrectly encoded"); - Environment.Exit (1); - } - } - - // TODO: rewrite this code (to kill N bugs and make it faster) and use standard ApplyAttribute way. - public AssemblyName GetAssemblyName (string name, string output) - { - if (OptAttributes != null) { - foreach (Attribute a in OptAttributes.Attrs) { - // cannot rely on any resolve-based members before you call Resolve - if (a.ExplicitTarget == null || a.ExplicitTarget != "assembly") - continue; - - // TODO: This code is buggy: comparing Attribute name without resolving is wrong. - // However, this is invoked by CodeGen.Init, when none of the namespaces - // are loaded yet. - // TODO: Does not handle quoted attributes properly - switch (a.Name) { - case "AssemblyKeyFile": - case "AssemblyKeyFileAttribute": - case "System.Reflection.AssemblyKeyFileAttribute": - if (RootContext.StrongNameKeyFile != null) { - Report.SymbolRelatedToPreviousError (a.Location, a.GetSignatureForError ()); - Report.Warning (1616, 1, "Option `{0}' overrides attribute `{1}' given in a source file or added module", - "keyfile", "System.Reflection.AssemblyKeyFileAttribute"); - } else { - string value = a.GetString (); - if (value != null && value.Length != 0) - RootContext.StrongNameKeyFile = value; - } - break; - case "AssemblyKeyName": - case "AssemblyKeyNameAttribute": - case "System.Reflection.AssemblyKeyNameAttribute": - if (RootContext.StrongNameKeyContainer != null) { - Report.SymbolRelatedToPreviousError (a.Location, a.GetSignatureForError ()); - Report.Warning (1616, 1, "Option `{0}' overrides attribute `{1}' given in a source file or added module", - "keycontainer", "System.Reflection.AssemblyKeyNameAttribute"); - } else { - string value = a.GetString (); - if (value != null && value.Length != 0) - RootContext.StrongNameKeyContainer = value; - } - break; - case "AssemblyDelaySign": - case "AssemblyDelaySignAttribute": - case "System.Reflection.AssemblyDelaySignAttribute": - RootContext.StrongNameDelaySign = a.GetBoolean (); - break; - } + } + + // + // Load the object from the pointer. + // + public void EmitLoadFromPtr (TypeSpec type) + { + if (type.Kind == MemberKind.Enum) + type = EnumSpec.GetUnderlyingType (type); + + switch (type.BuiltinType) { + case BuiltinTypeSpec.Type.Int: + ig.Emit (OpCodes.Ldind_I4); + break; + case BuiltinTypeSpec.Type.UInt: + ig.Emit (OpCodes.Ldind_U4); + break; + case BuiltinTypeSpec.Type.Short: + ig.Emit (OpCodes.Ldind_I2); + break; + case BuiltinTypeSpec.Type.UShort: + case BuiltinTypeSpec.Type.Char: + ig.Emit (OpCodes.Ldind_U2); + break; + case BuiltinTypeSpec.Type.Byte: + ig.Emit (OpCodes.Ldind_U1); + break; + case BuiltinTypeSpec.Type.SByte: + case BuiltinTypeSpec.Type.Bool: + ig.Emit (OpCodes.Ldind_I1); + break; + case BuiltinTypeSpec.Type.ULong: + case BuiltinTypeSpec.Type.Long: + ig.Emit (OpCodes.Ldind_I8); + break; + case BuiltinTypeSpec.Type.Float: + ig.Emit (OpCodes.Ldind_R4); + break; + case BuiltinTypeSpec.Type.Double: + ig.Emit (OpCodes.Ldind_R8); + break; + case BuiltinTypeSpec.Type.IntPtr: + ig.Emit (OpCodes.Ldind_I); + break; + default: + switch (type.Kind) { + case MemberKind.Struct: + case MemberKind.TypeParameter: + if (IsAnonymousStoreyMutateRequired) + type = CurrentAnonymousMethod.Storey.Mutator.Mutate (type); + + ig.Emit (OpCodes.Ldobj, type.GetMetaInfo ()); + break; + case MemberKind.PointerType: + ig.Emit (OpCodes.Ldind_I); + break; + default: + ig.Emit (OpCodes.Ldind_Ref); + break; } + break; } - - AssemblyName an = new AssemblyName (); - an.Name = Path.GetFileNameWithoutExtension (name); + } + + public void EmitNull () + { + ig.Emit (OpCodes.Ldnull); + } + + public void EmitArgumentAddress (int pos) + { + if (!IsStatic) + ++pos; - // note: delay doesn't apply when using a key container - if (RootContext.StrongNameKeyContainer != null) { - an.KeyPair = new StrongNameKeyPair (RootContext.StrongNameKeyContainer); - return an; + if (pos > byte.MaxValue) + ig.Emit (OpCodes.Ldarga, pos); + else + ig.Emit (OpCodes.Ldarga_S, (byte) pos); + } + + public void EmitArgumentLoad (int pos) + { + if (!IsStatic) + ++pos; + + switch (pos) { + case 0: ig.Emit (OpCodes.Ldarg_0); break; + case 1: ig.Emit (OpCodes.Ldarg_1); break; + case 2: ig.Emit (OpCodes.Ldarg_2); break; + case 3: ig.Emit (OpCodes.Ldarg_3); break; + default: + if (pos > byte.MaxValue) + ig.Emit (OpCodes.Ldarg, pos); + else + ig.Emit (OpCodes.Ldarg_S, (byte) pos); + break; } + } - // strongname is optional - if (RootContext.StrongNameKeyFile == null) - return an; + public void EmitArgumentStore (int pos) + { + if (!IsStatic) + ++pos; - string AssemblyDir = Path.GetDirectoryName (output); + if (pos > byte.MaxValue) + ig.Emit (OpCodes.Starg, pos); + else + ig.Emit (OpCodes.Starg_S, (byte) pos); + } - // the StrongName key file may be relative to (a) the compiled - // file or (b) to the output assembly. See bugzilla #55320 - // http://bugzilla.ximian.com/show_bug.cgi?id=55320 + // + // The stack contains the pointer and the value of type `type' + // + public void EmitStoreFromPtr (TypeSpec type) + { + if (type.IsEnum) + type = EnumSpec.GetUnderlyingType (type); - // (a) relative to the compiled file - string filename = Path.GetFullPath (RootContext.StrongNameKeyFile); - bool exist = File.Exists (filename); - if ((!exist) && (AssemblyDir != null) && (AssemblyDir != String.Empty)) { - // (b) relative to the outputed assembly - filename = Path.GetFullPath (Path.Combine (AssemblyDir, RootContext.StrongNameKeyFile)); - exist = File.Exists (filename); + switch (type.BuiltinType) { + case BuiltinTypeSpec.Type.Int: + case BuiltinTypeSpec.Type.UInt: + ig.Emit (OpCodes.Stind_I4); + return; + case BuiltinTypeSpec.Type.Long: + case BuiltinTypeSpec.Type.ULong: + ig.Emit (OpCodes.Stind_I8); + return; + case BuiltinTypeSpec.Type.Char: + case BuiltinTypeSpec.Type.Short: + case BuiltinTypeSpec.Type.UShort: + ig.Emit (OpCodes.Stind_I2); + return; + case BuiltinTypeSpec.Type.Float: + ig.Emit (OpCodes.Stind_R4); + return; + case BuiltinTypeSpec.Type.Double: + ig.Emit (OpCodes.Stind_R8); + return; + case BuiltinTypeSpec.Type.Byte: + case BuiltinTypeSpec.Type.SByte: + case BuiltinTypeSpec.Type.Bool: + ig.Emit (OpCodes.Stind_I1); + return; + case BuiltinTypeSpec.Type.IntPtr: + ig.Emit (OpCodes.Stind_I); + return; } - if (exist) { - using (FileStream fs = new FileStream (filename, FileMode.Open, FileAccess.Read)) { - byte[] snkeypair = new byte [fs.Length]; - fs.Read (snkeypair, 0, snkeypair.Length); + switch (type.Kind) { + case MemberKind.Struct: + case MemberKind.TypeParameter: + if (IsAnonymousStoreyMutateRequired) + type = CurrentAnonymousMethod.Storey.Mutator.Mutate (type); - if (RootContext.StrongNameDelaySign) { - // delayed signing - DO NOT include private key - SetPublicKey (an, snkeypair); - } - else { - // no delay so we make sure we have the private key - try { - CryptoConvert.FromCapiPrivateKeyBlob (snkeypair); - an.KeyPair = new StrongNameKeyPair (snkeypair); - } - catch (CryptographicException) { - if (snkeypair.Length == 16) { - // error # is different for ECMA key - Report.Error (1606, "Could not sign the assembly. " + - "ECMA key can only be used to delay-sign assemblies"); - } - else { - Error_AssemblySigning ("The specified file `" + RootContext.StrongNameKeyFile + "' does not have a private key"); - } - return null; - } - } - } - } - else { - Error_AssemblySigning ("The specified file `" + RootContext.StrongNameKeyFile + "' does not exist"); - return null; + ig.Emit (OpCodes.Stobj, type.GetMetaInfo ()); + break; + default: + ig.Emit (OpCodes.Stind_Ref); + break; } - return an; } - void Error_AssemblySigning (string text) + public void EmitThis () { - Report.Error (1548, "Error during assembly signing. " + text); + ig.Emit (OpCodes.Ldarg_0); } - bool CheckInternalsVisibleAttribute (Attribute a) + /// + /// Returns a temporary storage for a variable of type t as + /// a local variable in the current body. + /// + public LocalBuilder GetTemporaryLocal (TypeSpec t) { - string assembly_name = a.GetString (); - if (assembly_name.Length == 0) - return false; - - AssemblyName aname = null; - try { - aname = new AssemblyName (assembly_name); - } catch (FileLoadException) { - } catch (ArgumentException) { - } - - // Bad assembly name format - if (aname == null) - Report.Warning (1700, 3, a.Location, "Assembly reference `" + assembly_name + "' is invalid and cannot be resolved"); - // Report error if we have defined Version or Culture - else if (aname.Version != null || aname.CultureInfo != null) - throw new Exception ("Friend assembly `" + a.GetString () + - "' is invalid. InternalsVisibleTo cannot have version or culture specified."); - else if (aname.GetPublicKey () == null && Name.GetPublicKey () != null && Name.GetPublicKey ().Length != 0) { - Report.Error (1726, a.Location, "Friend assembly reference `" + aname.FullName + "' is invalid." + - " Strong named assemblies must specify a public key in their InternalsVisibleTo declarations"); - return false; + if (temporary_storage != null) { + object o; + if (temporary_storage.TryGetValue (t, out o)) { + if (o is Stack) { + var s = (Stack) o; + o = s.Count == 0 ? null : s.Pop (); + } else { + temporary_storage.Remove (t); + } + } + if (o != null) + return (LocalBuilder) o; } - - return true; + return DeclareLocal (t, false); } - static bool IsValidAssemblyVersion (string version) + public void FreeTemporaryLocal (LocalBuilder b, TypeSpec t) { - Version v; - try { - v = new Version (version); - } catch { - try { - int major = int.Parse (version, CultureInfo.InvariantCulture); - v = new Version (major, 0); - } catch { - return false; - } + if (temporary_storage == null) { + temporary_storage = new Dictionary (ReferenceEquality.Default); + temporary_storage.Add (t, b); + return; } - - foreach (int candidate in new int [] { v.Major, v.Minor, v.Build, v.Revision }) { - if (candidate > ushort.MaxValue) - return false; + object o; + + if (!temporary_storage.TryGetValue (t, out o)) { + temporary_storage.Add (t, b); + return; } - - return true; + var s = o as Stack; + if (s == null) { + s = new Stack (); + s.Push ((LocalBuilder)o); + temporary_storage [t] = s; + } + s.Push (b); } - public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa) + /// + /// ReturnValue creates on demand the LocalBuilder for the + /// return value from the function. By default this is not + /// used. This is only required when returns are found inside + /// Try or Catch statements. + /// + /// This method is typically invoked from the Emit phase, so + /// we allow the creation of a return label if it was not + /// requested during the resolution phase. Could be cleaned + /// up, but it would replicate a lot of logic in the Emit phase + /// of the code that uses it. + /// + public LocalBuilder TemporaryReturn () { - if (a.IsValidSecurityAttribute ()) { - if (declarative_security == null) - declarative_security = new Dictionary (); - - a.ExtractSecurityPermissionSet (declarative_security); - return; + if (return_value == null){ + return_value = DeclareLocal (return_type, false); } - if (a.Type == pa.AssemblyCulture) { - string value = a.GetString (); - if (value == null || value.Length == 0) - return; + return return_value; + } + } - if (RootContext.Target == Target.Exe) { - a.Error_AttributeEmitError ("The executables cannot be satelite assemblies, remove the attribute or keep it empty"); - return; - } - } + struct CallEmitter + { + public Expression InstanceExpression; - if (a.Type == pa.AssemblyVersion) { - string value = a.GetString (); - if (value == null || value.Length == 0) - return; + // + // When set leaves an extra copy of all arguments on the stack + // + public bool DuplicateArguments; - value = value.Replace ('*', '0'); + // + // Does not emit InstanceExpression load when InstanceExpressionOnStack + // is set. Used by compound assignments. + // + public bool InstanceExpressionOnStack; - if (!IsValidAssemblyVersion (value)) { - a.Error_AttributeEmitError (string.Format ("Specified version `{0}' is not valid", value)); - return; - } - } + // + // Any of arguments contains await expression + // + public bool HasAwaitArguments; + + // + // When dealing with await arguments the original arguments are converted + // into a new set with hoisted stack results + // + public Arguments EmittedArguments; - if (a.Type == pa.InternalsVisibleTo && !CheckInternalsVisibleAttribute (a)) + public void Emit (EmitContext ec, MethodSpec method, Arguments Arguments, Location loc) + { + // Speed up the check by not doing it on not allowed targets + if (method.ReturnType.Kind == MemberKind.Void && method.IsConditionallyExcluded (ec.Module.Compiler, loc)) return; - if (a.Type == pa.TypeForwarder) { - Type t = a.GetArgumentType (); - if (t == null || TypeManager.HasElementType (t)) { - Report.Error (735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute"); - return; - } + EmitPredefined (ec, method, Arguments); + } + + public void EmitPredefined (EmitContext ec, MethodSpec method, Arguments Arguments) + { + Expression instance_copy = null; - t = TypeManager.DropGenericTypeArguments (t); - if (emitted_forwarders == null) { - emitted_forwarders = new Dictionary (); - } else if (emitted_forwarders.ContainsKey (t)) { - Report.SymbolRelatedToPreviousError(emitted_forwarders[t].Location, null); - Report.Error(739, a.Location, "A duplicate type forward of type `{0}'", - TypeManager.CSharpName(t)); - return; + if (!HasAwaitArguments && ec.HasSet (BuilderContext.Options.AsyncBody)) { + HasAwaitArguments = Arguments != null && Arguments.ContainsEmitWithAwait (); + if (HasAwaitArguments && InstanceExpressionOnStack) { + throw new NotSupportedException (); } + } - emitted_forwarders.Add(t, a); + OpCode call_op; + LocalTemporary lt = null; - if (TypeManager.LookupDeclSpace (t) != null) { - Report.SymbolRelatedToPreviousError (t); - Report.Error (729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly", - TypeManager.CSharpName (t)); - return; + if (method.IsStatic) { + call_op = OpCodes.Call; + } else { + if (IsVirtualCallRequired (InstanceExpression, method)) { + call_op = OpCodes.Callvirt; + } else { + call_op = OpCodes.Call; } - if (t.DeclaringType != null) { - Report.Error (730, a.Location, "Cannot forward type `{0}' because it is a nested type", - TypeManager.CSharpName (t)); - return; + if (HasAwaitArguments) { + instance_copy = InstanceExpression.EmitToField (ec); + if (Arguments == null) + EmitCallInstance (ec, instance_copy, method.DeclaringType, call_op); + } else if (!InstanceExpressionOnStack) { + var instance_on_stack_type = EmitCallInstance (ec, InstanceExpression, method.DeclaringType, call_op); + + if (DuplicateArguments) { + ec.Emit (OpCodes.Dup); + if (Arguments != null && Arguments.Count != 0) { + lt = new LocalTemporary (instance_on_stack_type); + lt.Store (ec); + instance_copy = lt; + } + } } + } - if (add_type_forwarder == null) { - add_type_forwarder = typeof (AssemblyBuilder).GetMethod ("AddTypeForwarder", - BindingFlags.NonPublic | BindingFlags.Instance); + if (Arguments != null && !InstanceExpressionOnStack) { + EmittedArguments = Arguments.Emit (ec, DuplicateArguments, HasAwaitArguments); + if (EmittedArguments != null) { + if (instance_copy != null) { + EmitCallInstance (ec, instance_copy, method.DeclaringType, call_op); - if (add_type_forwarder == null) { - Report.RuntimeMissingSupport (a.Location, "TypeForwardedTo attribute"); - return; + if (lt != null) + lt.Release (ec); } + + EmittedArguments.Emit (ec); } + } - add_type_forwarder.Invoke (Builder, new object[] { t }); - return; + if (call_op == OpCodes.Callvirt && (InstanceExpression.Type.IsGenericParameter || InstanceExpression.Type.IsStruct)) { + ec.Emit (OpCodes.Constrained, InstanceExpression.Type); } - - if (a.Type == pa.Extension) { - a.Error_MisusedExtensionAttribute (); + + // + // Set instance expression to actual result expression. When it contains await it can be + // picked up by caller + // + InstanceExpression = instance_copy; + + if (method.Parameters.HasArglist) { + var varargs_types = GetVarargsTypes (method, Arguments); + ec.Emit (call_op, method, varargs_types); return; } - Builder.SetCustomAttribute (cb); + // + // If you have: + // this.DoFoo (); + // and DoFoo is not virtual, you can omit the callvirt, + // because you don't need the null checking behavior. + // + ec.Emit (call_op, method); } - public override void Emit (TypeContainer tc) + static TypeSpec EmitCallInstance (EmitContext ec, Expression instance, TypeSpec declaringType, OpCode callOpcode) { - base.Emit (tc); + var instance_type = instance.Type; - if (has_extension_method) - PredefinedAttributes.Get.Extension.EmitAttribute (Builder); + // + // Push the instance expression + // + if ((instance_type.IsStruct && (callOpcode == OpCodes.Callvirt || (callOpcode == OpCodes.Call && declaringType.IsStruct))) || + instance_type.IsGenericParameter || declaringType.IsNullableType) { + // + // If the expression implements IMemoryLocation, then + // we can optimize and use AddressOf on the + // return. + // + // If not we have to use some temporary storage for + // it. + var iml = instance as IMemoryLocation; + if (iml != null) { + iml.AddressOf (ec, AddressOp.Load); + } else { + LocalTemporary temp = new LocalTemporary (instance_type); + instance.Emit (ec); + temp.Store (ec); + temp.AddressOf (ec, AddressOp.Load); + temp.Release (ec); + } - // FIXME: Does this belong inside SRE.AssemblyBuilder instead? - PredefinedAttribute pa = PredefinedAttributes.Get.RuntimeCompatibility; - if (pa.IsDefined && (OptAttributes == null || !OptAttributes.Contains (pa))) { - ConstructorInfo ci = TypeManager.GetPredefinedConstructor ( - pa.Type, Location.Null, Type.EmptyTypes); - PropertyInfo [] pis = new PropertyInfo [1]; - pis [0] = TypeManager.GetPredefinedProperty (pa.Type, - "WrapNonExceptionThrows", Location.Null, TypeManager.bool_type); - object [] pargs = new object [1]; - pargs [0] = true; - Builder.SetCustomAttribute (new CustomAttributeBuilder (ci, new object [0], pis, pargs)); + return ReferenceContainer.MakeType (ec.Module, instance_type); } - if (declarative_security != null) { + if (instance_type.IsEnum || instance_type.IsStruct) { + instance.Emit (ec); + ec.Emit (OpCodes.Box, instance_type); + return ec.BuiltinTypes.Object; + } - MethodInfo add_permission = typeof (AssemblyBuilder).GetMethod ("AddPermissionRequests", BindingFlags.Instance | BindingFlags.NonPublic); - object builder_instance = Builder; + instance.Emit (ec); + return instance_type; + } - try { - // Microsoft runtime hacking - if (add_permission == null) { - Type assembly_builder = typeof (AssemblyBuilder).Assembly.GetType ("System.Reflection.Emit.AssemblyBuilderData"); - add_permission = assembly_builder.GetMethod ("AddPermissionRequests", BindingFlags.Instance | BindingFlags.NonPublic); + static MetaType[] GetVarargsTypes (MethodSpec method, Arguments arguments) + { + AParametersCollection pd = method.Parameters; - FieldInfo fi = typeof (AssemblyBuilder).GetField ("m_assemblyData", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); - builder_instance = fi.GetValue (Builder); - } + Argument a = arguments[pd.Count - 1]; + Arglist list = (Arglist) a.Expr; - var args = new PermissionSet [3]; - declarative_security.TryGetValue (SecurityAction.RequestMinimum, out args [0]); - declarative_security.TryGetValue (SecurityAction.RequestOptional, out args [1]); - declarative_security.TryGetValue (SecurityAction.RequestRefuse, out args [2]); - add_permission.Invoke (builder_instance, args); - } - catch { - Report.RuntimeMissingSupport (Location.Null, "assembly permission setting"); - } - } + return list.ArgumentTypes; } - public override string[] ValidAttributeTargets { - get { - return attribute_targets; - } - } - - // Wrapper for AssemblyBuilder.AddModule - static MethodInfo adder_method; - static public MethodInfo AddModule_Method { - get { - if (adder_method == null) - adder_method = typeof (AssemblyBuilder).GetMethod ("AddModule", BindingFlags.Instance|BindingFlags.NonPublic); - return adder_method; - } - } - public Module AddModule (string module) + // + // Used to decide whether call or callvirt is needed + // + static bool IsVirtualCallRequired (Expression instance, MethodSpec method) { - MethodInfo m = AddModule_Method; - if (m == null) { - Report.RuntimeMissingSupport (Location.Null, "/addmodule"); - Environment.Exit (1); - } + // + // There are 2 scenarious where we emit callvirt + // + // Case 1: A method is virtual and it's not used to call base + // Case 2: A method instance expression can be null. In this casen callvirt ensures + // correct NRE exception when the method is called + // + var decl_type = method.DeclaringType; + if (decl_type.IsStruct || decl_type.IsEnum) + return false; - try { - return (Module) m.Invoke (Builder, new object [] { module }); - } catch (TargetInvocationException ex) { - throw ex.InnerException; - } - } + if (instance is BaseThis) + return false; + + // + // It's non-virtual and will never be null + // + if (!method.IsVirtual && (instance is This || instance is New || instance is ArrayCreation || instance is DelegateCreation)) + return false; + + return true; + } } }