2004-11-02 Ben Maurer <bmaurer@ximian.com>
[mono.git] / mcs / mcs / expression.cs
index 012372d12e6b375e52479538401b0a5fb149a881..af1be64d86bcf0604f3389d24c92f1a71dbcae81 100755 (executable)
@@ -46,13 +46,13 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        if (args != null) 
-                               Invocation.EmitArguments (ec, mi, args, false, null);
+                               Invocation.EmitArguments (ec, mi, args);
 
                        ec.ig.Emit (OpCodes.Call, mi);
                        return;
                }
                
-               static public StaticCallExpr MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
+               static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
                                                         Expression e, Location loc)
                {
                        ArrayList args;
@@ -80,10 +80,6 @@ namespace Mono.CSharp {
                        if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
                                ec.ig.Emit (OpCodes.Pop);
                }
-               
-               public MethodInfo Method {
-                       get { return mi; }
-               }
        }
 
        public class ParenthesizedExpression : Expression
@@ -647,7 +643,7 @@ namespace Mono.CSharp {
        public class Indirection : Expression, IMemoryLocation, IAssignMethod {
                Expression expr;
                LocalTemporary temporary;
-               bool prepared;
+               bool have_temporary;
                
                public Indirection (Expression expr, Location l)
                {
@@ -663,47 +659,54 @@ namespace Mono.CSharp {
                
                public override void Emit (EmitContext ec)
                {
-                       if (!prepared)
+                       ILGenerator ig = ec.ig;
+
+                       if (temporary != null){
+                               if (have_temporary) {
+                                       temporary.Emit (ec);
+                               } else {
+                                       expr.Emit (ec);
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       temporary.Store (ec);
+                                       have_temporary = true;
+                               }
+                       } else
                                expr.Emit (ec);
                        
-                       LoadFromPtr (ec.ig, Type);
+                       LoadFromPtr (ig, Type);
                }
 
-               public void Emit (EmitContext ec, bool leave_copy)
+               public void EmitAssign (EmitContext ec, Expression source)
                {
-                       Emit (ec);
-                       if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
-                               temporary = new LocalTemporary (ec, expr.Type);
-                               temporary.Store (ec);
-                       }
-               }
-               
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
-               {
-                       prepared = prepare_for_load;
-                       
-                       expr.Emit (ec);
+                       if (temporary != null){
+                               if (have_temporary)
+                                       temporary.Emit (ec);
+                               else {
+                                       expr.Emit (ec);
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       temporary.Store (ec);
+                                       have_temporary = true;
+                               }
+                       } else 
+                               expr.Emit (ec);
 
-                       if (prepare_for_load)
-                               ec.ig.Emit (OpCodes.Dup);
-                       
                        source.Emit (ec);
-                       if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
-                               temporary = new LocalTemporary (ec, expr.Type);
-                               temporary.Store (ec);
-                       }
-                       
                        StoreFromPtr (ec.ig, type);
-                       
-                       if (temporary != null)
-                               temporary.Emit (ec);
                }
                
                public void AddressOf (EmitContext ec, AddressOp Mode)
                {
-                       expr.Emit (ec);
+                       if (temporary != null){
+                               if (have_temporary){
+                                       temporary.Emit (ec);
+                                       return;
+                               }
+                               expr.Emit (ec);
+                               ec.ig.Emit (OpCodes.Dup);
+                               temporary.Store (ec);
+                               have_temporary = true;
+                       } else
+                               expr.Emit (ec);
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -713,7 +716,12 @@ namespace Mono.CSharp {
                        //
                        return this;
                }
-               
+
+               public new void CacheTemporaries (EmitContext ec)
+               {
+                       temporary = new LocalTemporary (ec, expr.Type);
+               }
+
                public override string ToString ()
                {
                        return "*(" + expr + ")";
@@ -749,15 +757,13 @@ namespace Mono.CSharp {
                }
                
                Mode mode;
-               bool is_expr = false;
-               bool recurse = false;
-               
                Expression expr;
+               LocalTemporary temp_storage;
 
                //
                // This is expensive for the simplest case.
                //
-               StaticCallExpr method;
+               Expression method;
                        
                public UnaryMutator (Mode m, Expression e, Location l)
                {
@@ -849,7 +855,9 @@ namespace Mono.CSharp {
                        } else if (expr.eclass == ExprClass.IndexerAccess){
                                IndexerAccess ia = (IndexerAccess) expr;
                                
-                               expr = ia.ResolveLValue (ec, this);
+                               temp_storage = new LocalTemporary (ec, expr.Type);
+                               
+                               expr = ia.ResolveLValue (ec, temp_storage);
                                if (expr == null)
                                        return null;
 
@@ -967,39 +975,114 @@ namespace Mono.CSharp {
                        }
                        
                }
+
+               static EmptyExpression empty_expr;
                
                void EmitCode (EmitContext ec, bool is_expr)
                {
-                       recurse = true;
-                       this.is_expr = is_expr;
-                       ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
-               }
-               
+                       ILGenerator ig = ec.ig;
+                       IAssignMethod ia = (IAssignMethod) expr;
+                       Type expr_type = expr.Type;
+
+                       ia.CacheTemporaries (ec);
 
-               public override void Emit (EmitContext ec)
-               {
                        //
-                       // We use recurse to allow ourselfs to be the source
-                       // of an assignment. This little hack prevents us from
-                       // having to allocate another expression
+                       // NOTE: We should probably handle three cases:
                        //
-                       if (recurse) {
-                               ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement  || mode == Mode.PostDecrement));
-                               if (method == null)
-                                       LoadOneAndEmitOp (ec, expr.Type);
-                               else
-                                       ec.ig.Emit (OpCodes.Call, method.Method);
-                               recurse = false;
-                               return;
+                       //     * method invocation required.
+                       //     * direct stack manipulation possible
+                       //     * the object requires an "instance" field
+                       //
+                       if (temp_storage == null){
+                               //
+                               // Temporary improvement: if we are dealing with something that does
+                               // not require complicated instance setup, avoid using a temporary
+                               //
+                               // For now: only localvariables when not remapped
+                               //
+
+                               if (method == null &&
+                                   ((expr is LocalVariableReference) ||(expr is FieldExpr && ((FieldExpr) expr).FieldInfo.IsStatic))){
+                                       if (empty_expr == null)
+                                               empty_expr = new EmptyExpression ();
+                                       
+                                       switch (mode){
+                                       case Mode.PreIncrement:
+                                       case Mode.PreDecrement:
+                                               expr.Emit (ec);
+                                       
+                                               LoadOneAndEmitOp (ec, expr_type);
+                                               if (is_expr)
+                                                       ig.Emit (OpCodes.Dup);
+                                               ia.EmitAssign (ec, empty_expr);
+                                               break;
+                                               
+                                       case Mode.PostIncrement:
+                                       case Mode.PostDecrement:
+                                               expr.Emit (ec);
+                                               if (is_expr)
+                                                       ig.Emit (OpCodes.Dup);
+                                               
+                                               LoadOneAndEmitOp (ec, expr_type);
+                                               ia.EmitAssign (ec, empty_expr);
+                                               break;
+                                       }
+                                       return;
+                               }
+                               temp_storage = new LocalTemporary (ec, expr_type);
                        }
                        
+                       switch (mode){
+                       case Mode.PreIncrement:
+                       case Mode.PreDecrement:
+                               if (method == null){
+                                       expr.Emit (ec);
+                                       
+                                       LoadOneAndEmitOp (ec, expr_type);
+                               } else 
+                                       method.Emit (ec);
+                               
+                               temp_storage.Store (ec);
+                               ia.EmitAssign (ec, temp_storage);
+                               if (is_expr)
+                                       temp_storage.Emit (ec);
+                               break;
+                               
+                       case Mode.PostIncrement:
+                       case Mode.PostDecrement:
+                               if (is_expr)
+                                       expr.Emit (ec);
+                               
+                               if (method == null){
+                                       if (!is_expr)
+                                               expr.Emit (ec);
+                                       else
+                                               ig.Emit (OpCodes.Dup);
+                                       
+                                       LoadOneAndEmitOp (ec, expr_type);
+                               } else {
+                                       method.Emit (ec);
+                               }
+                               
+                               temp_storage.Store (ec);
+                               ia.EmitAssign (ec, temp_storage);
+                               break;
+                       }
+
+                       temp_storage.Release (ec);
+               }
+
+               public override void Emit (EmitContext ec)
+               {
                        EmitCode (ec, true);
+                       
                }
                
                public override void EmitStatement (EmitContext ec)
                {
                        EmitCode (ec, false);
                }
+
        }
 
        /// <summary>
@@ -1160,11 +1243,16 @@ namespace Mono.CSharp {
                                warning_never_matches = true;
                        }
                        
-                       if (warning_always_matches)
-                               Warning (183, "The given expression is always of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
-                       else if (warning_never_matches){
-                               if (!(probe_type.IsInterface || expr.Type.IsInterface))
-                                       Warning (184, "The given expression is never of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
+                       if (RootContext.WarningLevel >= 1){
+                               if (warning_always_matches)
+                                       Warning (183, "The expression is always of type `" +
+                                                TypeManager.CSharpName (probe_type) + "'");
+                               else if (warning_never_matches){
+                                       if (!(probe_type.IsInterface || expr.Type.IsInterface))
+                                               Warning (184,
+                                                        "The expression is never of type `" +
+                                                        TypeManager.CSharpName (probe_type) + "'");
+                               }
                        }
 
                        return this;
@@ -2225,8 +2313,8 @@ namespace Mono.CSharp {
                        // Special cases: string comapred to null
                        //
                        if (oper == Operator.Equality || oper == Operator.Inequality){
-                               if ((l == TypeManager.string_type && (right is NullLiteral)) ||
-                                   (r == TypeManager.string_type && (left is NullLiteral))){
+                               if ((!TypeManager.IsValueType (l) && (right is NullLiteral)) ||
+                                   (!TypeManager.IsValueType (r) && (left is NullLiteral))) {
                                        Type = TypeManager.bool_type;
                                        
                                        return this;
@@ -2449,16 +2537,16 @@ namespace Mono.CSharp {
                                                if (r == l)
                                                        return new PointerArithmetic (
                                                                false, left, right, TypeManager.int64_type,
-                                                               loc);
+                                                               loc).Resolve (ec);
                                        } else {
                                                Expression t = Make32or64 (ec, right);
                                                if (t != null)
-                                                       return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc);
+                                                       return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc).Resolve (ec);
                                        }
                                } else if (r.IsPointer && oper == Operator.Addition){
                                        Expression t = Make32or64 (ec, left);
                                        if (t != null)
-                                               return new PointerArithmetic (true, right, t, r, loc);
+                                               return new PointerArithmetic (true, right, t, r, loc).Resolve (ec);
                                }
                        }
                        
@@ -3042,7 +3130,7 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        
                        if (Arguments != null) 
-                               Invocation.EmitArguments (ec, method, Arguments, false, null);
+                               Invocation.EmitArguments (ec, method, Arguments);
                        
                        if (method is MethodInfo)
                                ig.Emit (OpCodes.Call, (MethodInfo) method);
@@ -3058,7 +3146,11 @@ namespace Mono.CSharp {
        public class StringConcat : Expression {
                ArrayList operands;
                bool invalid = false;
-               
+               bool emit_conv_done = false;
+               //
+               // Are we also concating objects?
+               //
+               bool is_strings_only = true;
                
                public StringConcat (EmitContext ec, Location loc, Expression left, Expression right)
                {
@@ -3112,30 +3204,30 @@ namespace Mono.CSharp {
                {
                        MethodInfo concat_method = null;
                        
-                       //
-                       // Are we also concating objects?
-                       //
-                       bool is_strings_only = true;
-                       
                        //
                        // Do conversion to arguments; check for strings only
                        //
-                       for (int i = 0; i < operands.Count; i ++) {
-                               Expression e = (Expression) operands [i];
-                               is_strings_only &= e.Type == TypeManager.string_type;
-                       }
                        
-                       for (int i = 0; i < operands.Count; i ++) {
-                               Expression e = (Expression) operands [i];
+                       // This can get called multiple times, so we have to deal with that.
+                       if (!emit_conv_done) {
+                               emit_conv_done = true;
+                               for (int i = 0; i < operands.Count; i ++) {
+                                       Expression e = (Expression) operands [i];
+                                       is_strings_only &= e.Type == TypeManager.string_type;
+                               }
                                
-                               if (! is_strings_only && e.Type == TypeManager.string_type) {
-                                       // need to make sure this is an object, because the EmitParams
-                                       // method might look at the type of this expression, see it is a
-                                       // string and emit a string [] when we want an object [];
+                               for (int i = 0; i < operands.Count; i ++) {
+                                       Expression e = (Expression) operands [i];
                                        
-                                       e = Convert.ImplicitConversion (ec, e, TypeManager.object_type, loc);
+                                       if (! is_strings_only && e.Type == TypeManager.string_type) {
+                                               // need to make sure this is an object, because the EmitParams
+                                               // method might look at the type of this expression, see it is a
+                                               // string and emit a string [] when we want an object [];
+                                               
+                                               e = new EmptyCast (e, TypeManager.object_type);
+                                       }
+                                       operands [i] = new Argument (e, Argument.AType.Expression);
                                }
-                               operands [i] = new Argument (e, Argument.AType.Expression);
                        }
                        
                        //
@@ -3177,7 +3269,7 @@ namespace Mono.CSharp {
                                break;
                        }
                        
-                       Invocation.EmitArguments (ec, concat_method, operands, false, null);
+                       Invocation.EmitArguments (ec, concat_method, operands);
                        ec.ig.Emit (OpCodes.Call, concat_method);
                }
        }
@@ -3206,7 +3298,7 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                        
-                       Invocation.EmitArguments (ec, method, args, false, null);
+                       Invocation.EmitArguments (ec, method, args);
                        
                        ig.Emit (OpCodes.Call, (MethodInfo) method);
                        ig.Emit (OpCodes.Castclass, type);
@@ -3322,7 +3414,6 @@ namespace Mono.CSharp {
                public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
                {
                        type = t;
-                       eclass = ExprClass.Variable;
                        this.loc = loc;
                        left = l;
                        right = r;
@@ -3331,9 +3422,13 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       //
-                       // We are born fully resolved
-                       //
+                       eclass = ExprClass.Variable;
+                       
+                       if (left.Type == TypeManager.void_ptr_type) {
+                               Error (242, "The operation in question is undefined on void pointers");
+                               return null;
+                       }
+                       
                        return this;
                }
 
@@ -3378,8 +3473,11 @@ namespace Mono.CSharp {
                                        else if (rtype == TypeManager.uint64_type)
                                                ig.Emit (OpCodes.Conv_U8);
                                        ig.Emit (OpCodes.Mul);
-                                       ig.Emit (OpCodes.Conv_I);
                                }
+                               
+                               if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
+                                       ig.Emit (OpCodes.Conv_I);
+                               
                                if (is_add)
                                        ig.Emit (OpCodes.Add);
                                else
@@ -3634,19 +3732,12 @@ namespace Mono.CSharp {
                        ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
                }
                
-               public void Emit (EmitContext ec, bool leave_copy)
-               {
-                       Emit (ec);
-                       if (leave_copy)
-                               ec.ig.Emit (OpCodes.Dup);
-               }
-               
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+               public void EmitAssign (EmitContext ec, Expression source)
                {
+                       ILGenerator ig = ec.ig;
+
                        source.Emit (ec);
-                       if (leave_copy)
-                               ec.ig.Emit (OpCodes.Dup);
-                       ec.ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
+                       ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
                }
                
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -3673,8 +3764,7 @@ namespace Mono.CSharp {
                Block block;
                VariableInfo vi;
                public Parameter.Modifier mod;
-               public bool is_ref, is_out, prepared;
-               LocalTemporary temp;
+               public bool is_ref, is_out;
                
                public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
                {
@@ -3809,11 +3899,6 @@ namespace Mono.CSharp {
                }
                
                public override void Emit (EmitContext ec)
-               {
-                       Emit (ec, false);
-               }
-               
-               public void Emit (EmitContext ec, bool leave_copy)
                {
                        ILGenerator ig = ec.ig;
                        
@@ -3824,56 +3909,33 @@ namespace Mono.CSharp {
 
                        EmitLdArg (ig, arg_idx);
 
-                       if (is_ref) {
-                               if (prepared)
-                                       ec.ig.Emit (OpCodes.Dup);
-       
-                               //
-                               // If we are a reference, we loaded on the stack a pointer
-                               // Now lets load the real value
-                               //
-                               LoadFromPtr (ig, type);
-                       }
-                       
-                       if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
-                               
-                               if (is_ref) {
-                                       temp = new LocalTemporary (ec, type);
-                                       temp.Store (ec);
-                               }
-                       }
+                       if (!is_ref)
+                               return;
+
+                       //
+                       // If we are a reference, we loaded on the stack a pointer
+                       // Now lets load the real value
+                       //
+                       LoadFromPtr (ig, type);
                }
-               
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+
+               public void EmitAssign (EmitContext ec, Expression source)
                {
                        ILGenerator ig = ec.ig;
-                       int arg_idx = idx;
-                       
-                       prepared = prepare_for_load;
                        
+                       int arg_idx = idx;
+
                        if (!ec.IsStatic)
                                arg_idx++;
 
-                       if (is_ref && !prepared)
+                       if (is_ref)
                                EmitLdArg (ig, arg_idx);
                        
                        source.Emit (ec);
 
-                       if (leave_copy)
-                               ec.ig.Emit (OpCodes.Dup);
-                       
-                       if (is_ref) {
-                               if (leave_copy) {
-                                       temp = new LocalTemporary (ec, type);
-                                       temp.Store (ec);
-                               }
-                               
+                       if (is_ref)
                                StoreFromPtr (ig, type);
-                               
-                               if (temp != null)
-                                       temp.Emit (ec);
-                       } else {
+                       else {
                                if (arg_idx <= 255)
                                        ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
                                else
@@ -3923,12 +3985,6 @@ namespace Mono.CSharp {
                        this.ArgType = type;
                }
 
-               public Argument (Expression expr)
-               {
-                       this.Expr = expr;
-                       this.ArgType = AType.Expression;
-               }
-
                public Type Type {
                        get {
                                if (ArgType == AType.Ref || ArgType == AType.Out)
@@ -5025,24 +5081,14 @@ namespace Mono.CSharp {
                ///   emission of the arguments is known not to contain
                ///   a `params' field (for example in constructors or other routines
                ///   that keep their arguments in this structure)
-               ///   
-               ///   if `dup_args' is true, a copy of the arguments will be left
-               ///   on the stack. If `dup_args' is true, you can specify `this_arg'
-               ///   which will be duplicated before any other args. Only EmitCall
-               ///   should be using this interface.
                /// </summary>
-               public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
+               public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
                {
                        ParameterData pd;
                        if (mb != null)
                                pd = GetParameterData (mb);
                        else
                                pd = null;
-                       
-                       LocalTemporary [] temps = null;
-                       
-                       if (dup_args)
-                               temps = new LocalTemporary [arguments.Count];
 
                        //
                        // If we are calling a params method with no arguments, special case it
@@ -5079,18 +5125,6 @@ namespace Mono.CSharp {
                                }
                                            
                                a.Emit (ec);
-                               if (dup_args) {
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       (temps [i] = new LocalTemporary (ec, a.Type)).Store (ec);
-                               }
-                       }
-                       
-                       if (dup_args) {
-                               if (this_arg != null)
-                                       this_arg.Emit (ec);
-                               
-                               for (int i = 0; i < top; i ++)
-                                       temps [i].Emit (ec);
                        }
 
                        if (pd != null && pd.Count > top &&
@@ -5154,25 +5188,10 @@ namespace Mono.CSharp {
                public static void EmitCall (EmitContext ec, bool is_base,
                                             bool is_static, Expression instance_expr,
                                             MethodBase method, ArrayList Arguments, Location loc)
-               {
-                       EmitCall (ec, is_base, is_static, instance_expr, method, Arguments, loc, false, false);
-               }
-               
-               // `dup_args' leaves an extra copy of the arguments on the stack
-               // `omit_args' does not leave any arguments at all.
-               // So, basically, you could make one call with `dup_args' set to true,
-               // and then another with `omit_args' set to true, and the two calls
-               // would have the same set of arguments. However, each argument would
-               // only have been evaluated once.
-               public static void EmitCall (EmitContext ec, bool is_base,
-                                            bool is_static, Expression instance_expr,
-                                            MethodBase method, ArrayList Arguments, Location loc,
-                                            bool dup_args, bool omit_args)
                {
                        ILGenerator ig = ec.ig;
                        bool struct_call = false;
                        bool this_call = false;
-                       LocalTemporary this_arg = null;
 
                        Type decl_type = method.DeclaringType;
 
@@ -5212,28 +5231,26 @@ namespace Mono.CSharp {
                 return; 
                        
                        if (!is_static){
-                               this_call = instance_expr == null;
-                               if (decl_type.IsValueType || (!this_call && instance_expr.Type.IsValueType))
+                               if (decl_type.IsValueType)
                                        struct_call = true;
-                               
                                //
                                // If this is ourselves, push "this"
                                //
-                               if (!omit_args) {
-                               Type t = null;
-                               if (this_call) {
+                               if (instance_expr == null) {
+                                       this_call = true;
                                        ig.Emit (OpCodes.Ldarg_0);
-                                       t = decl_type;
                                } else {
                                        //
                                        // Push the instance expression
                                        //
-                                       if (instance_expr.Type.IsValueType) {
+                                       if (instance_expr.Type.IsValueType){
                                                //
                                                // Special case: calls to a function declared in a 
                                                // reference-type with a value-type argument need
-                                               // to have their value boxed.
-                                               if (decl_type.IsValueType) {
+                                               // to have their value boxed.  
+
+                                               struct_call = true;
+                                               if (decl_type.IsValueType){
                                                        //
                                                        // If the expression implements IMemoryLocation, then
                                                        // we can optimize and use AddressOf on the
@@ -5241,40 +5258,28 @@ namespace Mono.CSharp {
                                                        //
                                                        // If not we have to use some temporary storage for
                                                        // it.
-                                                       if (instance_expr is IMemoryLocation) {
+                                                       if (instance_expr is IMemoryLocation){
                                                                ((IMemoryLocation)instance_expr).
                                                                        AddressOf (ec, AddressOp.LoadStore);
-                                                       } else {
-                                                               LocalTemporary temp = new LocalTemporary (ec, instance_expr.Type);
+                                                       }
+                                                       else {
+                                                               Type t = instance_expr.Type;
+                                                               
                                                                instance_expr.Emit (ec);
-                                                               temp.Store (ec);
-                                                               temp.AddressOf (ec, AddressOp.Load);
+                                                               LocalBuilder temp = ig.DeclareLocal (t);
+                                                               ig.Emit (OpCodes.Stloc, temp);
+                                                               ig.Emit (OpCodes.Ldloca, temp);
                                                        }
-                                                       
-                                                       // avoid the overhead of doing this all the time.
-                                                       if (dup_args)
-                                                               t = TypeManager.GetReferenceType (instance_expr.Type);
                                                } else {
                                                        instance_expr.Emit (ec);
                                                        ig.Emit (OpCodes.Box, instance_expr.Type);
-                                                       t = TypeManager.object_type;
                                                } 
-                                       } else {
+                                       } else
                                                instance_expr.Emit (ec);
-                                               t = instance_expr.Type;
-                                       }
-                               }
-                               
-                               if (dup_args) {
-                                       this_arg = new LocalTemporary (ec, t);
-                                       ig.Emit (OpCodes.Dup);
-                                       this_arg.Store (ec);
-                               }
                                }
                        }
 
-                       if (!omit_args)
-                               EmitArguments (ec, method, Arguments, dup_args, this_arg);
+                       EmitArguments (ec, method, Arguments);
 
                        OpCode call_op;
                        if (is_static || struct_call || is_base || (this_call && !method.IsVirtual))
@@ -5636,7 +5641,7 @@ namespace Mono.CSharp {
                        }
 
                        if (method != null)
-                               Invocation.EmitArguments (ec, method, Arguments, false, null);
+                               Invocation.EmitArguments (ec, method, Arguments);
 
                        if (is_value_type){
                                if (method == null)
@@ -5683,7 +5688,7 @@ namespace Mono.CSharp {
                        IMemoryLocation ml = (IMemoryLocation) value_target;
                        ml.AddressOf (ec, AddressOp.Store);
                        if (method != null)
-                               Invocation.EmitArguments (ec, method, Arguments, false, null);
+                               Invocation.EmitArguments (ec, method, Arguments);
 
                        if (method == null)
                                ec.ig.Emit (OpCodes.Initobj, type);
@@ -5840,7 +5845,7 @@ namespace Mono.CSharp {
                                        Expression tmp = (Expression) o;
                                        tmp = tmp.Resolve (ec);
                                        if (tmp == null)
-                                               continue;
+                                               return false;
 
                                        // Console.WriteLine ("I got: " + tmp);
                                        // Handle initialization from vars, fields etc.
@@ -6368,14 +6373,9 @@ namespace Mono.CSharp {
 
                                                e.Emit (ec);
 
-                                               if (dims == 1) {
-                                                       bool is_stobj;
-                                                       OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj);
-                                                       if (is_stobj)
-                                                               ig.Emit (OpCodes.Stobj, etype);
-                                                       else
-                                                               ig.Emit (op);
-                                               } else 
+                                               if (dims == 1)
+                                                       ArrayAccess.EmitStoreOpcode (ig, array_element_type);
+                                               else 
                                                        ig.Emit (OpCodes.Call, set);
 
                                        }
@@ -6459,7 +6459,7 @@ namespace Mono.CSharp {
                                if (e is NullLiteral)
                                        v = null;
                                else {
-                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, array_element_type, out v))
+                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, out v))
                                                return null;
                                }
                                ret [i++] = v;
@@ -6562,36 +6562,28 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public void Emit (EmitContext ec, bool leave_copy)
+               public override void Emit (EmitContext ec)
                {
-                       Emit (ec);
-                       if (leave_copy)
-                               ec.ig.Emit (OpCodes.Dup);
+                       ILGenerator ig = ec.ig;
+
+                       ec.EmitThis ();
+                       if (ec.TypeContainer is Struct)
+                               ig.Emit (OpCodes.Ldobj, type);
                }
-               
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+
+               public void EmitAssign (EmitContext ec, Expression source)
                {
                        ILGenerator ig = ec.ig;
                        
                        if (ec.TypeContainer is Struct){
                                ec.EmitThis ();
                                source.Emit (ec);
-                               if (leave_copy)
-                                       ec.ig.Emit (OpCodes.Dup);
                                ig.Emit (OpCodes.Stobj, type);
                        } else {
-                               throw new Exception ("how did you get here");
+                               source.Emit (ec);
+                               ig.Emit (OpCodes.Starg, 0);
                        }
                }
-               
-               public override void Emit (EmitContext ec)
-               {
-                       ILGenerator ig = ec.ig;
-
-                       ec.EmitThis ();
-                       if (ec.TypeContainer is Struct)
-                               ig.Emit (OpCodes.Ldobj, type);
-               }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
@@ -6736,6 +6728,10 @@ namespace Mono.CSharp {
                                return null;
                        }
 
+                       if (typearg.IsPointer && !ec.InUnsafe){
+                               UnsafeError (loc);
+                               return null;
+                       }
                        CheckObsoleteAttribute (typearg);
 
                        type = TypeManager.type_type;
@@ -7084,7 +7080,7 @@ namespace Mono.CSharp {
                        Type expr_type = expr.Type;
                        if (expr is TypeExpr){
                                if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
-                                       Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", expr_type);
+                                       Report.Error_T (122, loc, expr_type);
                                        return null;
                                }
 
@@ -7370,7 +7366,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               Expression MakePointerAccess ()
+               Expression MakePointerAccess (EmitContext ec)
                {
                        Type t = Expr.Type;
 
@@ -7384,8 +7380,10 @@ namespace Mono.CSharp {
                        }
                        Expression p;
 
-                       p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc);
-                       return new Indirection (p, loc);
+                       p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc).Resolve (ec);
+                       if (p == null)
+                               return null;
+                       return new Indirection (p, loc).Resolve (ec);
                }
                
                public override Expression DoResolve (EmitContext ec)
@@ -7409,7 +7407,7 @@ namespace Mono.CSharp {
                        if (t.IsArray)
                                return (new ArrayAccess (this, loc)).Resolve (ec);
                        else if (t.IsPointer)
-                               return MakePointerAccess ();
+                               return MakePointerAccess (ec);
                        else
                                return (new IndexerAccess (this, loc)).Resolve (ec);
                }
@@ -7423,7 +7421,7 @@ namespace Mono.CSharp {
                        if (t.IsArray)
                                return (new ArrayAccess (this, loc)).ResolveLValue (ec, right_side);
                        else if (t.IsPointer)
-                               return MakePointerAccess ();
+                               return MakePointerAccess (ec);
                        else
                                return (new IndexerAccess (this, loc)).ResolveLValue (ec, right_side);
                }
@@ -7443,8 +7441,7 @@ namespace Mono.CSharp {
                //
                ElementAccess ea;
 
-               LocalTemporary temp;
-               bool prepared;
+               LocalTemporary [] cached_locations;
                
                public ArrayAccess (ElementAccess ea_data, Location l)
                {
@@ -7543,6 +7540,20 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldelem_Ref);
                }
 
+               /// <summary>
+               ///    Emits the right opcode to store an object of Type `t'
+               ///    from an array of T.  
+               /// </summary>
+               static public void EmitStoreOpcode (ILGenerator ig, Type t)
+               {
+                       bool is_stobj;
+                       OpCode op = GetStoreOpcode (t, out is_stobj);
+                       if (is_stobj)
+                               ig.Emit (OpCodes.Stobj, t);
+                       else
+                               ig.Emit (op);
+               }
+
                /// <summary>
                ///    Returns the right opcode to store an object of Type `t'
                ///    from an array of T.  
@@ -7634,111 +7645,101 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                        
-                       ea.Expr.Emit (ec);
-                       foreach (Argument a in ea.Arguments){
-                               Type argtype = a.Expr.Type;
+                       if (cached_locations == null){
+                               ea.Expr.Emit (ec);
+                               foreach (Argument a in ea.Arguments){
+                                       Type argtype = a.Expr.Type;
+                                       
+                                       a.Expr.Emit (ec);
+                                       
+                                       if (argtype == TypeManager.int64_type)
+                                               ig.Emit (OpCodes.Conv_Ovf_I);
+                                       else if (argtype == TypeManager.uint64_type)
+                                               ig.Emit (OpCodes.Conv_Ovf_I_Un);
+                               }
+                               return;
+                       }
+
+                       if (cached_locations [0] == null){
+                               cached_locations [0] = new LocalTemporary (ec, ea.Expr.Type);
+                               ea.Expr.Emit (ec);
+                               ig.Emit (OpCodes.Dup);
+                               cached_locations [0].Store (ec);
                                
-                               a.Expr.Emit (ec);
+                               int j = 1;
                                
-                               if (argtype == TypeManager.int64_type)
-                                       ig.Emit (OpCodes.Conv_Ovf_I);
-                               else if (argtype == TypeManager.uint64_type)
-                                       ig.Emit (OpCodes.Conv_Ovf_I_Un);
+                               foreach (Argument a in ea.Arguments){
+                                       Type argtype = a.Expr.Type;
+                                       
+                                       cached_locations [j] = new LocalTemporary (ec, TypeManager.intptr_type /* a.Expr.Type */);
+                                       a.Expr.Emit (ec);
+                                       if (argtype == TypeManager.int64_type)
+                                               ig.Emit (OpCodes.Conv_Ovf_I);
+                                       else if (argtype == TypeManager.uint64_type)
+                                               ig.Emit (OpCodes.Conv_Ovf_I_Un);
+
+                                       ig.Emit (OpCodes.Dup);
+                                       cached_locations [j].Store (ec);
+                                       j++;
+                               }
+                               return;
                        }
+
+                       foreach (LocalTemporary lt in cached_locations)
+                               lt.Emit (ec);
                }
 
-               public void Emit (EmitContext ec, bool leave_copy)
+               public new void CacheTemporaries (EmitContext ec)
+               {
+                       cached_locations = new LocalTemporary [ea.Arguments.Count + 1];
+               }
+               
+               public override void Emit (EmitContext ec)
                {
                        int rank = ea.Expr.Type.GetArrayRank ();
                        ILGenerator ig = ec.ig;
 
-                       if (!prepared) {
-                               LoadArrayAndArguments (ec);
-                               
-                               if (rank == 1)
-                                       EmitLoadOpcode (ig, type);
-                               else {
-                                       MethodInfo method;
-                                       
-                                       method = FetchGetMethod ();
-                                       ig.Emit (OpCodes.Call, method);
-                               }
-                       } else
-                               LoadFromPtr (ec.ig, this.type);
+                       LoadArrayAndArguments (ec);
                        
-                       if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
-                               temp = new LocalTemporary (ec, this.type);
-                               temp.Store (ec);
+                       if (rank == 1)
+                               EmitLoadOpcode (ig, type);
+                       else {
+                               MethodInfo method;
+                               
+                               method = FetchGetMethod ();
+                               ig.Emit (OpCodes.Call, method);
                        }
                }
-               
-               public override void Emit (EmitContext ec)
-               {
-                       Emit (ec, false);
-               }
 
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+               public void EmitAssign (EmitContext ec, Expression source)
                {
                        int rank = ea.Expr.Type.GetArrayRank ();
                        ILGenerator ig = ec.ig;
                        Type t = source.Type;
-                       prepared = prepare_for_load;
 
-                       if (prepare_for_load) {
-                               AddressOf (ec, AddressOp.LoadStore);
-                               ec.ig.Emit (OpCodes.Dup);
-                               source.Emit (ec);
-                               if (leave_copy) {
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, this.type);
-                                       temp.Store (ec);
-                               }
-                               StoreFromPtr (ec.ig, t);
-                               
-                               if (temp != null)
-                                       temp.Emit (ec);
-                               
-                               return;
-                       }
-                       
                        LoadArrayAndArguments (ec);
 
-                       if (rank == 1) {
-                               bool is_stobj;
-                               OpCode op = GetStoreOpcode (t, out is_stobj);
-                               //
-                               // The stobj opcode used by value types will need
-                               // an address on the stack, not really an array/array
-                               // pair
-                               //
-                               if (is_stobj)
+                       //
+                       // The stobj opcode used by value types will need
+                       // an address on the stack, not really an array/array
+                       // pair
+                       //
+                       if (rank == 1){
+                               if (t == TypeManager.enum_type || t == TypeManager.decimal_type ||
+                                   (t.IsSubclassOf (TypeManager.value_type) && !TypeManager.IsEnumType (t) && !TypeManager.IsBuiltinType (t)))
                                        ig.Emit (OpCodes.Ldelema, t);
-                               
-                               source.Emit (ec);
-                               if (leave_copy) {
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, this.type);
-                                       temp.Store (ec);
-                               }
-                               
-                               if (is_stobj)
-                                       ig.Emit (OpCodes.Stobj, t);
-                               else
-                                       ig.Emit (op);
-                       } else {
+                       }
+                       
+                       source.Emit (ec);
+
+                       if (rank == 1)
+                               EmitStoreOpcode (ig, t);
+                       else {
                                ModuleBuilder mb = CodeGen.Module.Builder;
                                int arg_count = ea.Arguments.Count;
                                Type [] args = new Type [arg_count + 1];
                                MethodInfo set;
                                
-                               source.Emit (ec);
-                               if (leave_copy) {
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, this.type);
-                                       temp.Store (ec);
-                               }
-                               
                                for (int i = 0; i < arg_count; i++){
                                        //args [i++] = a.Type;
                                        args [i] = TypeManager.int32_type;
@@ -7754,9 +7755,6 @@ namespace Mono.CSharp {
                                
                                ig.Emit (OpCodes.Call, set);
                        }
-                       
-                       if (temp != null)
-                               temp.Emit (ec);
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -8048,53 +8046,19 @@ namespace Mono.CSharp {
                        return this;
                }
                
-               bool prepared = false;
-               LocalTemporary temp;
-               
-               public void Emit (EmitContext ec, bool leave_copy)
+               public override void Emit (EmitContext ec)
                {
-                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc, prepared, false);
-                       if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
-                               temp = new LocalTemporary (ec, Type);
-                               temp.Store (ec);
-                       }
+                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc);
                }
-               
+
                //
                // source is ignored, because we already have a copy of it from the
                // LValue resolution and we have already constructed a pre-cached
                // version of the arguments (ea.set_arguments);
                //
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
-               {
-                       prepared = prepare_for_load;
-                       Argument a = (Argument) set_arguments [set_arguments.Count - 1];
-                       
-                       if (prepared) {
-                               source.Emit (ec);
-                               if (leave_copy) {
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, Type);
-                                       temp.Store (ec);
-                               }
-                       } else if (leave_copy) {
-                               temp = new LocalTemporary (ec, Type);
-                               source.Emit (ec);
-                               temp.Store (ec);
-                               a.Expr = temp;
-                       }
-                       
-                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc, false, prepared);
-                       
-                       if (temp != null)
-                               temp.Emit (ec);
-               }
-               
-               
-               public override void Emit (EmitContext ec)
+               public void EmitAssign (EmitContext ec, Expression source)
                {
-                       Emit (ec, false);
+                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc);
                }
        }
 
@@ -8471,7 +8435,8 @@ namespace Mono.CSharp {
                        Constant c = count as Constant;
                        // TODO: because we don't have property IsNegative
                        if (c != null && c.ConvertToUInt () == null) {
-                               Report.Error (247, loc, "Cannot use a negative size with stackalloc");
+                                // "Cannot use a negative size with stackalloc"
+                               Report.Error_T (247, loc);
                                return null;
                        }