Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / LightCompiler.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Apache License, Version 2.0, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Apache License, Version 2.0.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 #if FEATURE_CORE_DLR
17 using System.Linq.Expressions;
18 using Microsoft.Scripting.Ast;
19 #else
20 using Microsoft.Scripting.Ast;
21 #endif
22
23 using System;
24 using System.Linq;
25 using System.Collections.Generic;
26 using System.Diagnostics;
27 using System.Reflection;
28 using System.Runtime.CompilerServices;
29 #if FEATURE_REFEMIT
30 using System.Reflection.Emit;
31 #endif
32
33 using AstUtils = Microsoft.Scripting.Ast.Utils;
34 using Microsoft.Scripting.Utils;
35 using Microsoft.Scripting.Runtime;
36 using System.Security;
37
38 namespace Microsoft.Scripting.Interpreter {
39     public sealed class ExceptionHandler {
40         public readonly Type ExceptionType;
41         public readonly int StartIndex;
42         public readonly int EndIndex;
43         public readonly int LabelIndex;
44         public readonly int HandlerStartIndex;
45
46         public bool IsFault { get { return ExceptionType == null; } }
47
48         internal ExceptionHandler(int start, int end, int labelIndex, int handlerStartIndex, Type exceptionType) {
49             StartIndex = start;
50             EndIndex = end;
51             LabelIndex = labelIndex;
52             ExceptionType = exceptionType;
53             HandlerStartIndex = handlerStartIndex;
54         }
55
56         public bool Matches(Type exceptionType, int index) {
57             if (index >= StartIndex && index < EndIndex) {
58                 if (ExceptionType == null || ExceptionType.IsAssignableFrom(exceptionType)) {
59                     return true;
60                 }
61             }
62             return false;
63         }
64
65         public bool IsBetterThan(ExceptionHandler other) {
66             if (other == null) return true;
67
68             if (StartIndex == other.StartIndex && EndIndex == other.EndIndex) {
69                 return HandlerStartIndex < other.HandlerStartIndex;
70             }
71
72             if (StartIndex > other.StartIndex) {
73                 Debug.Assert(EndIndex <= other.EndIndex);
74                 return true;
75             } else if (EndIndex < other.EndIndex) {
76                 Debug.Assert(StartIndex == other.StartIndex);
77                 return true;
78             } else {
79                 return false;
80             }
81         }
82
83         internal bool IsInside(int index) {
84             return index >= StartIndex && index < EndIndex;
85         }
86
87         public override string ToString() {
88             return String.Format("{0} [{1}-{2}] [{3}->]",
89                 (IsFault ? "fault" : "catch(" + ExceptionType.Name + ")"),
90                 StartIndex, EndIndex,
91                 HandlerStartIndex
92             );
93         }
94     }
95
96     [Serializable]
97     public class DebugInfo {
98         // TODO: readonly
99
100         public int StartLine, EndLine;
101         public int Index;
102         public string FileName;
103         public bool IsClear;
104         private static readonly DebugInfoComparer _debugComparer = new DebugInfoComparer();
105
106         private class DebugInfoComparer : IComparer<DebugInfo> {
107             //We allow comparison between int and DebugInfo here
108             int IComparer<DebugInfo>.Compare(DebugInfo d1, DebugInfo d2) {
109                 if (d1.Index > d2.Index) return 1;
110                 else if (d1.Index == d2.Index) return 0;
111                 else return -1;
112             }
113         }
114         
115         public static DebugInfo GetMatchingDebugInfo(DebugInfo[] debugInfos, int index) {
116             //Create a faked DebugInfo to do the search
117             DebugInfo d = new DebugInfo { Index = index };
118
119             //to find the closest debug info before the current index
120
121             int i = Array.BinarySearch<DebugInfo>(debugInfos, d, _debugComparer);
122             if (i < 0) {
123                 //~i is the index for the first bigger element
124                 //if there is no bigger element, ~i is the length of the array
125                 i = ~i;
126                 if (i == 0) {
127                     return null;
128                 }
129                 //return the last one that is smaller
130                 i = i - 1;
131             }
132
133             return debugInfos[i];
134         }
135
136         public override string ToString() {
137             if (IsClear) {
138                 return String.Format("{0}: clear", Index);
139             } else {
140                 return String.Format("{0}: [{1}-{2}] '{3}'", Index, StartLine, EndLine, FileName);
141             }
142         }
143     }
144
145     // TODO:
146     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
147     [Serializable]
148     public struct InterpretedFrameInfo {
149         public readonly string MethodName;
150         
151         // TODO:
152         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
153         public readonly DebugInfo DebugInfo;
154
155         public InterpretedFrameInfo(string methodName, DebugInfo info) {
156             MethodName = methodName;
157             DebugInfo = info;
158         }
159
160         public override string ToString() {
161             return MethodName + (DebugInfo != null ? ": " + DebugInfo.ToString() : null);
162         }
163     }
164
165     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
166     public sealed class LightCompiler {
167         internal const int DefaultCompilationThreshold = 32;
168
169         // zero: sync compilation
170         private readonly int _compilationThreshold;
171
172         private readonly InstructionList _instructions;
173         private readonly LocalVariables _locals = new LocalVariables();
174
175         private readonly List<ExceptionHandler> _handlers = new List<ExceptionHandler>();
176         
177         private readonly List<DebugInfo> _debugInfos = new List<DebugInfo>();
178         private readonly HybridReferenceDictionary<LabelTarget, LabelInfo> _treeLabels = new HybridReferenceDictionary<LabelTarget, LabelInfo>();
179         private LabelScopeInfo _labelBlock = new LabelScopeInfo(null, LabelScopeKind.Lambda);
180
181         private readonly Stack<ParameterExpression> _exceptionForRethrowStack = new Stack<ParameterExpression>();
182
183         // Set to true to force compiliation of this lambda.
184         // This disables the interpreter for this lambda. We still need to
185         // walk it, however, to resolve variables closed over from the parent
186         // lambdas (because they may be interpreted).
187         private bool _forceCompile;
188
189         private readonly LightCompiler _parent;
190
191         private static LocalDefinition[] EmptyLocals = new LocalDefinition[0];
192
193         internal LightCompiler(int compilationThreshold) {
194             _instructions = new InstructionList();
195             _compilationThreshold = compilationThreshold < 0 ? DefaultCompilationThreshold : compilationThreshold;
196         }
197
198         private LightCompiler(LightCompiler parent)
199             : this(parent._compilationThreshold) {
200             _parent = parent;
201         }
202
203         public InstructionList Instructions {
204             get { return _instructions; }
205         }
206
207         public LocalVariables Locals {
208             get { return _locals; }
209         }
210
211         internal static Expression Unbox(Expression strongBoxExpression) {
212             return Expression.Field(strongBoxExpression, typeof(StrongBox<object>).GetDeclaredField("Value"));
213         }
214
215         internal LightDelegateCreator CompileTop(LambdaExpression node) {
216             foreach (var p in node.Parameters) {
217                 var local = _locals.DefineLocal(p, 0);
218                 _instructions.EmitInitializeParameter(local.Index);
219             }
220
221             Compile(node.Body);
222             
223             // pop the result of the last expression:
224             if (node.Body.Type != typeof(void) && node.ReturnType == typeof(void)) {
225                 _instructions.EmitPop();
226             }
227
228             Debug.Assert(_instructions.CurrentStackDepth == (node.ReturnType != typeof(void) ? 1 : 0));
229
230             return new LightDelegateCreator(MakeInterpreter(node.Name), node);
231         }
232
233         internal LightDelegateCreator CompileTop(LightLambdaExpression node) {
234             foreach (var p in node.Parameters) {
235                 var local = _locals.DefineLocal(p, 0);
236                 _instructions.EmitInitializeParameter(local.Index);
237             }
238
239             Compile(node.Body);
240
241             // pop the result of the last expression:
242             if (node.Body.Type != typeof(void) && node.ReturnType == typeof(void)) {
243                 _instructions.EmitPop();
244             }
245
246             Debug.Assert(_instructions.CurrentStackDepth == (node.ReturnType != typeof(void) ? 1 : 0));
247
248             return new LightDelegateCreator(MakeInterpreter(node.Name), node);
249         }
250
251         private Interpreter MakeInterpreter(string lambdaName) {
252             if (_forceCompile) {
253                 return null;
254             }
255
256             var handlers = _handlers.ToArray();
257             var debugInfos = _debugInfos.ToArray();
258
259             return new Interpreter(lambdaName, _locals, GetBranchMapping(), _instructions.ToArray(), handlers, debugInfos, _compilationThreshold);
260         }
261
262
263         private void CompileConstantExpression(Expression expr) {
264             var node = (ConstantExpression)expr;
265             _instructions.EmitLoad(node.Value, node.Type);
266         }
267
268         private void CompileDefaultExpression(Expression expr) {
269             CompileDefaultExpression(expr.Type);
270         }
271
272         private void CompileDefaultExpression(Type type) {
273             if (type != typeof(void)) {
274                 if (type.IsValueType()) {
275                     object value = ScriptingRuntimeHelpers.GetPrimitiveDefaultValue(type);
276                     if (value != null) {
277                         _instructions.EmitLoad(value);
278                     } else {
279                         _instructions.EmitDefaultValue(type);
280                     }
281                 } else {
282                     _instructions.EmitLoad(null);
283                 }
284             }
285         }
286
287         private LocalVariable EnsureAvailableForClosure(ParameterExpression expr) {
288             LocalVariable local;
289             if (_locals.TryGetLocalOrClosure(expr, out local)) {
290                 if (!local.InClosure && !local.IsBoxed) {
291                     _locals.Box(expr, _instructions);
292                 }
293                 return local;
294             } else if (_parent != null) {
295                 _parent.EnsureAvailableForClosure(expr);
296                 return _locals.AddClosureVariable(expr);
297             } else {
298                 throw new InvalidOperationException("unbound variable: " + expr);
299             }
300         }
301
302         private void EnsureVariable(ParameterExpression variable) {
303             if (!_locals.ContainsVariable(variable)) {
304                 EnsureAvailableForClosure(variable);
305             }
306         }
307
308         private LocalVariable ResolveLocal(ParameterExpression variable) {
309             LocalVariable local;
310             if (!_locals.TryGetLocalOrClosure(variable, out local)) {
311                 local = EnsureAvailableForClosure(variable);
312             }
313             return local;
314         }
315
316         public void CompileGetVariable(ParameterExpression variable) {
317             LocalVariable local = ResolveLocal(variable);
318
319             if (local.InClosure) {
320                 _instructions.EmitLoadLocalFromClosure(local.Index);
321             } else if (local.IsBoxed) {
322                 _instructions.EmitLoadLocalBoxed(local.Index);
323             } else {
324                 _instructions.EmitLoadLocal(local.Index);
325             }
326
327             _instructions.SetDebugCookie(variable.Name);
328         }
329
330         public void CompileGetBoxedVariable(ParameterExpression variable) {
331             LocalVariable local = ResolveLocal(variable);
332
333             if (local.InClosure) {
334                 _instructions.EmitLoadLocalFromClosureBoxed(local.Index);
335             } else {
336                 Debug.Assert(local.IsBoxed);
337                 _instructions.EmitLoadLocal(local.Index);
338             }
339
340             _instructions.SetDebugCookie(variable.Name);
341         }
342
343         public void CompileSetVariable(ParameterExpression variable, bool isVoid) {
344             LocalVariable local = ResolveLocal(variable);
345
346             if (local.InClosure) {
347                 if (isVoid) {
348                     _instructions.EmitStoreLocalToClosure(local.Index);
349                 } else {
350                     _instructions.EmitAssignLocalToClosure(local.Index);
351                 }
352             } else if (local.IsBoxed) {
353                 if (isVoid) {
354                     _instructions.EmitStoreLocalBoxed(local.Index);
355                 } else {
356                     _instructions.EmitAssignLocalBoxed(local.Index);
357                 }
358             } else {
359                 if (isVoid) {
360                     _instructions.EmitStoreLocal(local.Index);
361                 } else {
362                     _instructions.EmitAssignLocal(local.Index);
363                 }
364             }
365
366             _instructions.SetDebugCookie(variable.Name);
367         }
368
369         public void CompileParameterExpression(Expression expr) {
370             var node = (ParameterExpression)expr;
371             CompileGetVariable(node);
372         }
373
374         private void CompileBlockExpression(Expression expr, bool asVoid) {
375             var node = (BlockExpression)expr;
376             var end = CompileBlockStart(node);
377
378             var lastExpression = node.Expressions[node.Expressions.Count - 1];
379             Compile(lastExpression, asVoid);
380             CompileBlockEnd(end);
381         }
382
383         private LocalDefinition[] CompileBlockStart(BlockExpression node) {
384             var start = _instructions.Count;
385             
386             LocalDefinition[] locals;
387             var variables = node.Variables;
388             if (variables.Count != 0) {
389                 // TODO: basic flow analysis so we don't have to initialize all
390                 // variables.
391                 locals = new LocalDefinition[variables.Count];
392                 int localCnt = 0;
393                 foreach (var variable in variables) {
394                     var local = _locals.DefineLocal(variable, start);
395                     locals[localCnt++] = local;
396
397                     _instructions.EmitInitializeLocal(local.Index, variable.Type);
398                     _instructions.SetDebugCookie(variable.Name);
399                 }
400             } else {
401                 locals = EmptyLocals;
402             }
403
404             for (int i = 0; i < node.Expressions.Count - 1; i++) {
405                 CompileAsVoid(node.Expressions[i]);
406             }
407             return locals;
408         }
409
410         private void CompileBlockEnd(LocalDefinition[] locals) {
411             foreach (var local in locals) {
412                 _locals.UndefineLocal(local, _instructions.Count);
413             }
414         }
415
416         private void CompileIndexExpression(Expression expr) {
417             var index = (IndexExpression)expr;
418
419             // instance:
420             if (index.Object != null) {
421                 Compile(index.Object);
422             }
423
424             // indexes, byref args not allowed.
425             foreach (var arg in index.Arguments) {
426                 Compile(arg);
427             }
428
429             if (index.Indexer != null) {
430                 EmitCall(index.Indexer.GetGetMethod(true));
431             } else if (index.Arguments.Count != 1) {
432                 EmitCall(index.Object.Type.GetMethod("Get", BindingFlags.Public | BindingFlags.Instance));
433             } else {
434                 _instructions.EmitGetArrayItem(index.Object.Type);
435             }
436         }
437
438         private void CompileIndexAssignment(BinaryExpression node, bool asVoid) {
439             var index = (IndexExpression)node.Left;
440
441             if (!asVoid) {
442                 throw new NotImplementedException();
443             }
444
445             // instance:
446             if (index.Object != null) {
447                 Compile(index.Object);
448             }
449
450             // indexes, byref args not allowed.
451             foreach (var arg in index.Arguments) {
452                 Compile(arg);
453             }
454
455             // value:
456             Compile(node.Right);
457
458             if (index.Indexer != null) {
459                 EmitCall(index.Indexer.GetSetMethod(true));
460             } else if (index.Arguments.Count != 1) {
461                 EmitCall(index.Object.Type.GetMethod("Set", BindingFlags.Public | BindingFlags.Instance));
462             } else {
463                 _instructions.EmitSetArrayItem(index.Object.Type);
464             }
465         }
466
467         private void CompileMemberAssignment(BinaryExpression node, bool asVoid) {
468             var member = (MemberExpression)node.Left;
469
470             PropertyInfo pi = member.Member as PropertyInfo;
471             if (pi != null) {
472                 var method = pi.GetSetMethod(true);
473                 Compile(member.Expression);
474                 Compile(node.Right);
475
476                 int start = _instructions.Count;
477                 if (!asVoid) {
478                     LocalDefinition local = _locals.DefineLocal(Expression.Parameter(node.Right.Type), start);
479                     _instructions.EmitAssignLocal(local.Index);
480                     EmitCall(method);
481                     _instructions.EmitLoadLocal(local.Index);
482                     _locals.UndefineLocal(local, _instructions.Count);
483                 } else {
484                     EmitCall(method);
485                 }
486                 return;
487             }
488
489             FieldInfo fi = member.Member as FieldInfo;
490             if (fi != null) {
491                 if (member.Expression != null) {
492                     Compile(member.Expression);
493                 }
494                 Compile(node.Right);
495
496                 int start = _instructions.Count;
497                 if (!asVoid) {
498                     LocalDefinition local = _locals.DefineLocal(Expression.Parameter(node.Right.Type), start);
499                     _instructions.EmitAssignLocal(local.Index);
500                     _instructions.EmitStoreField(fi);
501                     _instructions.EmitLoadLocal(local.Index);
502                     _locals.UndefineLocal(local, _instructions.Count);
503                 } else {
504                     _instructions.EmitStoreField(fi);
505                 }
506                 return;
507             }
508
509             throw new NotImplementedException();
510         }
511
512         private void CompileVariableAssignment(BinaryExpression node, bool asVoid) {
513             this.Compile(node.Right);
514
515             var target = (ParameterExpression)node.Left;
516             CompileSetVariable(target, asVoid);
517         }
518
519         private void CompileAssignBinaryExpression(Expression expr, bool asVoid) {
520             var node = (BinaryExpression)expr;
521
522             switch (node.Left.NodeType) {
523                 case ExpressionType.Index:
524                     CompileIndexAssignment(node, asVoid); 
525                     break;
526
527                 case ExpressionType.MemberAccess:
528                     CompileMemberAssignment(node, asVoid); 
529                     break;
530
531                 case ExpressionType.Parameter:
532                 case ExpressionType.Extension:
533                     CompileVariableAssignment(node, asVoid); 
534                     break;
535
536                 default:
537                     throw new InvalidOperationException("Invalid lvalue for assignment: " + node.Left.NodeType);
538             }
539         }
540
541         private void CompileBinaryExpression(Expression expr) {
542             var node = (BinaryExpression)expr;
543
544             if (node.Method != null) {
545                 Compile(node.Left);
546                 Compile(node.Right);
547                 EmitCall(node.Method);
548             } else {
549                 switch (node.NodeType) {
550                     case ExpressionType.ArrayIndex:
551                         Debug.Assert(node.Right.Type == typeof(int));
552                         Compile(node.Left);
553                         Compile(node.Right);
554                         _instructions.EmitGetArrayItem(node.Left.Type);
555                         return;
556
557                     case ExpressionType.Add:
558                     case ExpressionType.AddChecked:
559                     case ExpressionType.Subtract:
560                     case ExpressionType.SubtractChecked:
561                     case ExpressionType.Multiply:
562                     case ExpressionType.MultiplyChecked:
563                     case ExpressionType.Divide:
564                     case ExpressionType.Modulo:
565                         CompileArithmetic(node.NodeType, node.Left, node.Right);
566                         return;
567
568                     case ExpressionType.Equal:
569                         CompileEqual(node.Left, node.Right, node.IsLiftedToNull);
570                         return;
571
572                     case ExpressionType.NotEqual:
573                         CompileNotEqual(node.Left, node.Right, node.IsLiftedToNull);
574                         return;
575
576                     case ExpressionType.LessThan:
577                     case ExpressionType.LessThanOrEqual:
578                     case ExpressionType.GreaterThan:
579                     case ExpressionType.GreaterThanOrEqual:
580                                                 CompileComparison(node.NodeType, node.Left, node.Right, node.IsLiftedToNull);
581                         return;
582
583                     case ExpressionType.LeftShift:
584                     case ExpressionType.RightShift:
585                         CompileShift(node.NodeType, node.Left, node.Right, node.IsLifted);
586                         return;
587
588                     case ExpressionType.And:
589                     case ExpressionType.Or:
590                     case ExpressionType.ExclusiveOr:
591                         CompileLogical(node.NodeType, node.Left, node.Right, node.IsLifted);
592                         return;
593
594                     default:
595                         throw new NotImplementedException(node.NodeType.ToString());
596                 }
597             }
598         }
599
600         private void CompileEqual(Expression left, Expression right, bool liftedResult) {
601             Debug.Assert(left.Type == right.Type || !left.Type.IsValueType() && !right.Type.IsValueType());
602             Compile(left);
603             Compile(right);
604             _instructions.EmitEqual(left.Type, liftedResult);
605         }
606
607         private void CompileNotEqual(Expression left, Expression right, bool liftedResult) {
608             Debug.Assert(left.Type == right.Type || !left.Type.IsValueType() && !right.Type.IsValueType());
609             Compile(left);
610             Compile(right);
611             _instructions.EmitNotEqual(left.Type, liftedResult);
612         }
613
614                 private void CompileComparison(ExpressionType nodeType, Expression left, Expression right, bool liftedResult) {
615             Debug.Assert(left.Type == right.Type && TypeUtils.IsNumeric(left.Type));
616
617             Compile(left);
618             Compile(right);
619             
620             switch (nodeType) {
621                 case ExpressionType.LessThan: _instructions.EmitLessThan(left.Type, liftedResult); break;
622                 case ExpressionType.LessThanOrEqual: _instructions.EmitLessThanOrEqual(left.Type, liftedResult); break;
623                 case ExpressionType.GreaterThan: _instructions.EmitGreaterThan(left.Type, liftedResult); break;
624                 case ExpressionType.GreaterThanOrEqual: _instructions.EmitGreaterThanOrEqual(left.Type, liftedResult); break;
625                 default: throw Assert.Unreachable;
626             }
627         }
628
629         private void CompileArithmetic(ExpressionType nodeType, Expression left, Expression right) {
630             Debug.Assert(left.Type == right.Type && TypeUtils.IsArithmetic(left.Type));
631             Compile(left);
632             Compile(right);
633             switch (nodeType) {
634                 case ExpressionType.Add: _instructions.EmitAdd(left.Type, false); break;
635                 case ExpressionType.AddChecked: _instructions.EmitAdd(left.Type, true); break;
636                 case ExpressionType.Subtract: _instructions.EmitSub(left.Type, false); break;
637                 case ExpressionType.SubtractChecked: _instructions.EmitSub(left.Type, true); break;
638                 case ExpressionType.Multiply: _instructions.EmitMul(left.Type, false); break;
639                 case ExpressionType.MultiplyChecked: _instructions.EmitMul(left.Type, true); break;
640                 case ExpressionType.Divide: _instructions.EmitDiv(left.Type); break;
641                 case ExpressionType.Modulo: _instructions.EmitMod(left.Type); break;
642                 default: throw Assert.Unreachable;
643             }
644         }
645
646         private void CompileShift(ExpressionType nodeType, Expression left, Expression right, bool lifted) {
647             Debug.Assert(right.Type == typeof (int) || right.Type == typeof (int?));
648             Compile(left);
649             Compile(right);
650             switch (nodeType) {
651                 case ExpressionType.LeftShift: _instructions.EmitShl(TypeUtils.GetNonNullableType (left.Type), lifted); break;
652                 case ExpressionType.RightShift: _instructions.EmitShr(TypeUtils.GetNonNullableType (left.Type), lifted); break;
653                 default: throw Assert.Unreachable;
654             }
655         }
656
657         private void CompileLogical(ExpressionType nodeType, Expression left, Expression right, bool lifted) {
658             Debug.Assert(left.Type == right.Type); // && TypeUtils.IsIntegerOrBool(left.Type));
659             Compile(left);
660             Compile(right);
661             switch (nodeType) {
662                 case ExpressionType.And: _instructions.EmitAnd(TypeUtils.GetNonNullableType (left.Type), lifted); break;
663                 case ExpressionType.Or: _instructions.EmitOr(TypeUtils.GetNonNullableType (left.Type), lifted); break;
664                 case ExpressionType.ExclusiveOr: _instructions.EmitExclusiveOr(TypeUtils.GetNonNullableType (left.Type), lifted); break;
665                 default: throw Assert.Unreachable;
666             }
667         }
668
669         private void CompileConvertUnaryExpression(Expression expr) {
670             var node = (UnaryExpression)expr;
671             if (node.Method != null) {
672                 Compile(node.Operand);
673
674                                 if (node.IsLifted)
675                                         throw new NotImplementedException ();
676
677                 // We should be able to ignore Int32ToObject
678                 if (node.Method != Runtime.ScriptingRuntimeHelpers.Int32ToObjectMethod) {
679                     EmitCall(node.Method);
680                 }
681             } else if (node.Type == typeof(void)) {
682                 CompileAsVoid(node.Operand);
683             } else {
684                 Compile(node.Operand);
685                 CompileConvertToType(node.Operand.Type, node.Type, node.NodeType == ExpressionType.ConvertChecked);
686             }
687         }
688
689         private void CompileConvertToType(Type typeFrom, Type typeTo, bool isChecked) {
690             Debug.Assert(typeFrom != typeof(void) && typeTo != typeof(void));
691
692             if (TypeUtils.AreEquivalent(typeTo, typeFrom)) {
693                 return;
694             }
695
696             if (TypeUtils.IsNullableType (typeTo)) {
697                 typeFrom = TypeUtils.GetNonNullableType (typeFrom);
698                 typeTo = TypeUtils.GetNonNullableType (typeTo);
699
700                 var nullValue = _instructions.MakeLabel();
701                 var end = _instructions.MakeLabel();
702
703                 _instructions.EmitDup ();
704                 _instructions.EmitBranchNull(nullValue);
705                 CompileConvertToType (typeFrom, typeTo, isChecked);
706                 _instructions.EmitWrap (typeTo);
707                 _instructions.EmitBranch (end);                
708                 _instructions.MarkLabel(nullValue);
709                 _instructions.EmitDup (); // Keep null on the stack
710                 _instructions.MarkLabel(end);
711                 return;
712             }
713
714             if (TypeUtils.IsNullableType (typeFrom)) {
715                 if (typeTo.IsClass)
716                     return;
717
718                 // TODO: should throw same exception as (int)(int?)null
719                 throw new NotImplementedException ();
720             }
721
722             TypeCode from = typeFrom.GetTypeCode();
723             TypeCode to = typeTo.GetTypeCode();
724             if (TypeUtils.IsNumeric(from) && TypeUtils.IsNumeric(to)) {
725                 if (isChecked) {
726                     _instructions.EmitNumericConvertChecked(from, to);
727                 } else {
728                     _instructions.EmitNumericConvertUnchecked(from, to);
729                 }
730                 return;
731             }
732
733             // TODO: Conversions to a super-class or implemented interfaces are no-op. 
734             // A conversion to a non-implemented interface or an unrelated class, etc. should fail.
735             return;
736         }
737
738         private void CompileNegateExpression(UnaryExpression node, bool @checked, bool lifted) {
739             Compile(node.Operand);
740             _instructions.EmitNegate(TypeUtils.GetNonNullableType (node.Type), @checked, lifted);
741         }
742
743         private void CompileNotExpression(UnaryExpression node, bool lifted) {
744             Compile(node.Operand);
745             _instructions.EmitNot(TypeUtils.GetNonNullableType (node.Type), lifted);
746         }
747
748         private void CompileUnaryExpression(Expression expr) {
749             var node = (UnaryExpression)expr;
750             
751             if (node.Method != null) {
752                 Compile(node.Operand);
753                 EmitCall(node.Method);
754             } else {
755                 switch (node.NodeType) {
756                     case ExpressionType.ArrayLength:
757                         Compile(node.Operand);
758                         _instructions.EmitGetArrayLength (node.Type);
759                         return;
760                     case ExpressionType.Negate:
761                         CompileNegateExpression(node, false, node.IsLifted);
762                         return;
763                     case ExpressionType.NegateChecked:
764                         CompileNegateExpression(node, true, node.IsLifted);
765                         return;                    
766                     case ExpressionType.Not:
767                         CompileNotExpression(node, node.IsLifted);
768                         return;
769                     case ExpressionType.UnaryPlus:
770                         // unary plus is a nop:
771                         Compile(node.Operand);                    
772                         return;
773                     case ExpressionType.TypeAs:
774                         CompileTypeAsExpression(node);
775                         return;
776                     default:
777                         throw new NotImplementedException(node.NodeType.ToString());
778                 }
779             }
780         }
781
782         private void CompileAndAlsoBinaryExpression(Expression expr) {
783             CompileLogicalBinaryExpression(expr, true);
784         }
785
786         private void CompileOrElseBinaryExpression(Expression expr) {
787             CompileLogicalBinaryExpression(expr, false);
788         }
789
790         private void CompileLogicalBinaryExpression(Expression expr, bool andAlso) {
791             var node = (BinaryExpression)expr;
792             if (node.Method != null) {
793                 throw new NotImplementedException();
794             }
795
796             Debug.Assert(node.Left.Type == node.Right.Type);
797
798             if (node.Left.Type == typeof(bool)) {
799                 var elseLabel = _instructions.MakeLabel();
800                 var endLabel = _instructions.MakeLabel();
801                 Compile(node.Left);
802                 if (andAlso) {
803                     _instructions.EmitBranchFalse(elseLabel);
804                 } else {
805                     _instructions.EmitBranchTrue(elseLabel);
806                 }
807                 Compile(node.Right);
808                 _instructions.EmitBranch(endLabel, false, true);
809                 _instructions.MarkLabel(elseLabel);
810                 _instructions.EmitLoad(!andAlso);
811                 _instructions.MarkLabel(endLabel);
812                 return;
813             }
814
815             Debug.Assert(node.Left.Type == typeof(bool?));
816             throw new NotImplementedException();
817         }
818
819         private void CompileConditionalExpression(Expression expr, bool asVoid) {
820             var node = (ConditionalExpression)expr;
821             Compile(node.Test);
822
823             if (node.IfTrue == AstUtils.Empty()) {
824                 var endOfFalse = _instructions.MakeLabel();
825                 _instructions.EmitBranchTrue(endOfFalse);
826                 Compile(node.IfFalse, asVoid);
827                 _instructions.MarkLabel(endOfFalse);
828             } else {
829                 var endOfTrue = _instructions.MakeLabel();
830                 _instructions.EmitBranchFalse(endOfTrue);
831                 Compile(node.IfTrue, asVoid);
832
833                 if (node.IfFalse != AstUtils.Empty()) {
834                     var endOfFalse = _instructions.MakeLabel();
835                     _instructions.EmitBranch(endOfFalse, false, !asVoid);
836                     _instructions.MarkLabel(endOfTrue);
837                     Compile(node.IfFalse, asVoid);
838                     _instructions.MarkLabel(endOfFalse);
839                 } else {
840                     _instructions.MarkLabel(endOfTrue);
841                 }
842             }
843         }
844
845         #region Loops
846
847         private void CompileLoopExpression(Expression expr) {
848             var node = (LoopExpression)expr;
849             var enterLoop = new EnterLoopInstruction(node, _locals, _compilationThreshold, _instructions.Count);
850
851             PushLabelBlock(LabelScopeKind.Statement);
852             LabelInfo breakLabel = DefineLabel(node.BreakLabel);
853             LabelInfo continueLabel = DefineLabel(node.ContinueLabel);
854
855             _instructions.MarkLabel(continueLabel.GetLabel(this));
856
857             // emit loop body:
858             _instructions.Emit(enterLoop);
859             CompileAsVoid(node.Body);
860
861             // emit loop branch:
862             _instructions.EmitBranch(continueLabel.GetLabel(this), expr.Type != typeof(void), false);
863
864             _instructions.MarkLabel(breakLabel.GetLabel(this));
865
866             PopLabelBlock(LabelScopeKind.Statement);
867
868             enterLoop.FinishLoop(_instructions.Count);
869         }
870
871         #endregion
872
873         private void CompileSwitchExpression(Expression expr) {
874             var node = (SwitchExpression)expr;
875
876             // Currently only supports int test values, with no method
877             if (node.SwitchValue.Type != typeof(int) || node.Comparison != null) {
878                 throw new NotImplementedException();
879             }
880
881             // Test values must be constant
882             if (!node.Cases.All(c => c.TestValues.All(t => t is ConstantExpression))) {
883                 throw new NotImplementedException();
884             }
885             LabelInfo end = DefineLabel(null);
886             bool hasValue = node.Type != typeof(void);
887
888             Compile(node.SwitchValue);
889             var caseDict = new Dictionary<int, int>();
890             int switchIndex = _instructions.Count;
891             _instructions.EmitSwitch(caseDict);
892
893             if (node.DefaultBody != null) {
894                 Compile(node.DefaultBody);
895             } else {
896                 Debug.Assert(!hasValue);
897             }
898             _instructions.EmitBranch(end.GetLabel(this), false, hasValue);
899
900             for (int i = 0; i < node.Cases.Count; i++) {
901                 var switchCase = node.Cases[i];
902
903                 int caseOffset = _instructions.Count - switchIndex;
904                 foreach (ConstantExpression testValue in switchCase.TestValues) {
905                     caseDict[(int)testValue.Value] = caseOffset;
906                 }
907
908                 Compile(switchCase.Body);
909
910                 if (i < node.Cases.Count - 1) {
911                     _instructions.EmitBranch(end.GetLabel(this), false, hasValue);
912                 }
913             }
914
915             _instructions.MarkLabel(end.GetLabel(this));
916         }
917
918         private void CompileLabelExpression(Expression expr) {
919             var node = (LabelExpression)expr;
920
921             // If we're an immediate child of a block, our label will already
922             // be defined. If not, we need to define our own block so this
923             // label isn't exposed except to its own child expression.
924             LabelInfo label = null;
925
926             if (_labelBlock.Kind == LabelScopeKind.Block) {
927                 _labelBlock.TryGetLabelInfo(node.Target, out label);
928
929                 // We're in a block but didn't find our label, try switch
930                 if (label == null && _labelBlock.Parent.Kind == LabelScopeKind.Switch) {
931                     _labelBlock.Parent.TryGetLabelInfo(node.Target, out label);
932                 }
933
934                 // if we're in a switch or block, we should've found the label
935                 Debug.Assert(label != null);
936             }
937
938             if (label == null) {
939                 label = DefineLabel(node.Target);
940             }
941
942             if (node.DefaultValue != null) {
943                 if (node.Target.Type == typeof(void)) {
944                     CompileAsVoid(node.DefaultValue);
945                 } else {
946                     Compile(node.DefaultValue);
947                 }
948             }
949
950             _instructions.MarkLabel(label.GetLabel(this));
951         }
952
953         private void CompileGotoExpression(Expression expr) {
954             var node = (GotoExpression)expr;
955             var labelInfo = ReferenceLabel(node.Target);
956
957             if (node.Value != null) {
958                 Compile(node.Value);
959             }
960
961             _instructions.EmitGoto(labelInfo.GetLabel(this), node.Type != typeof(void), node.Value != null && node.Value.Type != typeof(void));
962         }
963
964         public BranchLabel GetBranchLabel(LabelTarget target) {
965             return ReferenceLabel(target).GetLabel(this);
966         }
967
968         public void PushLabelBlock(LabelScopeKind type) {
969             _labelBlock = new LabelScopeInfo(_labelBlock, type);
970         }
971
972         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "kind")]
973         public void PopLabelBlock(LabelScopeKind kind) {
974             Debug.Assert(_labelBlock != null && _labelBlock.Kind == kind);
975             _labelBlock = _labelBlock.Parent;
976         }
977
978         private LabelInfo EnsureLabel(LabelTarget node) {
979             LabelInfo result;
980             if (!_treeLabels.TryGetValue(node, out result)) {
981                 _treeLabels[node] = result = new LabelInfo(node);
982             }
983             return result;
984         }
985
986         private LabelInfo ReferenceLabel(LabelTarget node) {
987             LabelInfo result = EnsureLabel(node);
988             result.Reference(_labelBlock);
989             return result;
990         }
991
992         internal LabelInfo DefineLabel(LabelTarget node) {
993             if (node == null) {
994                 return new LabelInfo(null);
995             }
996             LabelInfo result = EnsureLabel(node);
997             result.Define(_labelBlock);
998             return result;
999         }
1000
1001         private bool TryPushLabelBlock(Expression node) {
1002             // Anything that is "statement-like" -- e.g. has no associated
1003             // stack state can be jumped into, with the exception of try-blocks
1004             // We indicate this by a "Block"
1005             // 
1006             // Otherwise, we push an "Expression" to indicate that it can't be
1007             // jumped into
1008             switch (node.NodeType) {
1009                 default:
1010                     if (_labelBlock.Kind != LabelScopeKind.Expression) {
1011                         PushLabelBlock(LabelScopeKind.Expression);
1012                         return true;
1013                     }
1014                     return false;
1015                 case ExpressionType.Label:
1016                     // LabelExpression is a bit special, if it's directly in a
1017                     // block it becomes associate with the block's scope. Same
1018                     // thing if it's in a switch case body.
1019                     if (_labelBlock.Kind == LabelScopeKind.Block) {
1020                         var label = ((LabelExpression)node).Target;
1021                         if (_labelBlock.ContainsTarget(label)) {
1022                             return false;
1023                         }
1024                         if (_labelBlock.Parent.Kind == LabelScopeKind.Switch &&
1025                             _labelBlock.Parent.ContainsTarget(label)) {
1026                             return false;
1027                         }
1028                     }
1029                     PushLabelBlock(LabelScopeKind.Statement);
1030                     return true;
1031                 case ExpressionType.Block:
1032                     PushLabelBlock(LabelScopeKind.Block);
1033                     // Labels defined immediately in the block are valid for
1034                     // the whole block.
1035                     if (_labelBlock.Parent.Kind != LabelScopeKind.Switch) {
1036                         DefineBlockLabels(node);
1037                     }
1038                     return true;
1039                 case ExpressionType.Switch:
1040                     PushLabelBlock(LabelScopeKind.Switch);
1041                     // Define labels inside of the switch cases so theyare in
1042                     // scope for the whole switch. This allows "goto case" and
1043                     // "goto default" to be considered as local jumps.
1044                     var @switch = (SwitchExpression)node;
1045                     foreach (SwitchCase c in @switch.Cases) {
1046                         DefineBlockLabels(c.Body);
1047                     }
1048                     DefineBlockLabels(@switch.DefaultBody);
1049                     return true;
1050
1051                 // Remove this when Convert(Void) goes away.
1052                 case ExpressionType.Convert:
1053                     if (node.Type != typeof(void)) {
1054                         // treat it as an expression
1055                         goto default;
1056                     }
1057                     PushLabelBlock(LabelScopeKind.Statement);
1058                     return true;
1059
1060                 case ExpressionType.Conditional:
1061                 case ExpressionType.Loop:
1062                 case ExpressionType.Goto:
1063                     PushLabelBlock(LabelScopeKind.Statement);
1064                     return true;
1065             }
1066         }
1067
1068         private void DefineBlockLabels(Expression node) {
1069             var block = node as BlockExpression;
1070             if (block == null) {
1071                 return;
1072             }
1073
1074             for (int i = 0, n = block.Expressions.Count; i < n; i++) {
1075                 Expression e = block.Expressions[i];
1076
1077                 var label = e as LabelExpression;
1078                 if (label != null) {
1079                     DefineLabel(label.Target);
1080                 }
1081             }
1082         }
1083
1084         private HybridReferenceDictionary<LabelTarget, BranchLabel> GetBranchMapping() {
1085             var newLabelMapping = new HybridReferenceDictionary<LabelTarget, BranchLabel>(_treeLabels.Count);
1086             foreach (var kvp in _treeLabels) {
1087                 newLabelMapping[kvp.Key] = kvp.Value.GetLabel(this);
1088             }
1089             return newLabelMapping;
1090         }
1091
1092         private void CompileThrowUnaryExpression(Expression expr, bool asVoid) {
1093             var node = (UnaryExpression)expr;
1094
1095             if (node.Operand == null) {
1096                 CompileParameterExpression(_exceptionForRethrowStack.Peek());
1097                 if (asVoid) {
1098                     _instructions.EmitRethrowVoid();
1099                 } else {
1100                     _instructions.EmitRethrow();
1101                 }
1102             } else {
1103                 Compile(node.Operand);
1104                 if (asVoid) {
1105                     _instructions.EmitThrowVoid();
1106                 } else {
1107                     _instructions.EmitThrow();
1108                 }
1109             }
1110
1111         }
1112
1113         // TODO: remove (replace by true fault support)
1114         private bool EndsWithRethrow(Expression expr) {
1115             if (expr.NodeType == ExpressionType.Throw) {
1116                 var node = (UnaryExpression)expr;
1117                 return node.Operand == null;
1118             }
1119
1120             BlockExpression block = expr as BlockExpression;
1121             if (block != null) {
1122                 return EndsWithRethrow(block.Expressions[block.Expressions.Count - 1]);
1123             }
1124             return false;
1125         }
1126
1127
1128         // TODO: remove (replace by true fault support)
1129         private void CompileAsVoidRemoveRethrow(Expression expr) {
1130             int stackDepth = _instructions.CurrentStackDepth;
1131
1132             if (expr.NodeType == ExpressionType.Throw) {
1133                 Debug.Assert(((UnaryExpression)expr).Operand == null);
1134                 return;
1135             }
1136
1137             var node = (BlockExpression)expr;
1138             var end = CompileBlockStart(node);
1139
1140             CompileAsVoidRemoveRethrow(node.Expressions[node.Expressions.Count - 1]);
1141
1142             Debug.Assert(stackDepth == _instructions.CurrentStackDepth);
1143
1144             CompileBlockEnd(end);
1145         }
1146
1147         private void CompileTryExpression(Expression expr) {
1148             var node = (TryExpression)expr;
1149
1150             BranchLabel end = _instructions.MakeLabel();
1151             BranchLabel gotoEnd = _instructions.MakeLabel();
1152
1153             int tryStart = _instructions.Count;
1154
1155             BranchLabel startOfFinally = null;
1156             if (node.Finally != null) {
1157                 startOfFinally = _instructions.MakeLabel();
1158                 _instructions.EmitEnterTryFinally(startOfFinally);
1159             }
1160
1161             PushLabelBlock(LabelScopeKind.Try);
1162             Compile(node.Body);
1163
1164             bool hasValue = node.Body.Type != typeof(void);
1165             int tryEnd = _instructions.Count;
1166
1167             // handlers jump here:
1168             _instructions.MarkLabel(gotoEnd);
1169             _instructions.EmitGoto(end, hasValue, hasValue);
1170             
1171             // keep the result on the stack:     
1172             if (node.Handlers.Count > 0) {
1173                 // TODO: emulates faults (replace by true fault support)
1174                 if (node.Finally == null && node.Handlers.Count == 1) {
1175                     var handler = node.Handlers[0];
1176                     if (handler.Filter == null && handler.Test == typeof(Exception) && handler.Variable == null) {
1177                         if (EndsWithRethrow(handler.Body)) {
1178                             if (hasValue) {
1179                                 _instructions.EmitEnterExceptionHandlerNonVoid();
1180                             } else {
1181                                 _instructions.EmitEnterExceptionHandlerVoid();
1182                             }
1183
1184                             // at this point the stack balance is prepared for the hidden exception variable:
1185                             int handlerLabel = _instructions.MarkRuntimeLabel();
1186                             int handlerStart = _instructions.Count;
1187
1188                             CompileAsVoidRemoveRethrow(handler.Body);
1189                             _instructions.EmitLeaveFault(hasValue);
1190                             _instructions.MarkLabel(end);
1191
1192                             _handlers.Add(new ExceptionHandler(tryStart, tryEnd, handlerLabel, handlerStart, null));
1193                             PopLabelBlock(LabelScopeKind.Try);
1194                             return;
1195                         }
1196                     }
1197                 }
1198
1199                 foreach (var handler in node.Handlers) {
1200                     PushLabelBlock(LabelScopeKind.Catch);
1201
1202                     if (handler.Filter != null) {
1203                         //PushLabelBlock(LabelScopeKind.Filter);
1204                         throw new NotImplementedException();
1205                         //PopLabelBlock(LabelScopeKind.Filter);
1206                     }
1207
1208                     var parameter = handler.Variable ?? Expression.Parameter(handler.Test);
1209
1210                     var local = _locals.DefineLocal(parameter, _instructions.Count);
1211                     _exceptionForRethrowStack.Push(parameter);
1212
1213                     // add a stack balancing nop instruction (exception handling pushes the current exception):
1214                     if (hasValue) {
1215                         _instructions.EmitEnterExceptionHandlerNonVoid();
1216                     } else {
1217                         _instructions.EmitEnterExceptionHandlerVoid();
1218                     }
1219
1220                     // at this point the stack balance is prepared for the hidden exception variable:
1221                     int handlerLabel = _instructions.MarkRuntimeLabel();
1222                     int handlerStart = _instructions.Count;
1223
1224                     CompileSetVariable(parameter, true);
1225                     Compile(handler.Body);
1226
1227                     _exceptionForRethrowStack.Pop();
1228
1229                     // keep the value of the body on the stack:
1230                     Debug.Assert(hasValue == (handler.Body.Type != typeof(void)));
1231                     _instructions.EmitLeaveExceptionHandler(hasValue, gotoEnd);
1232
1233                     _handlers.Add(new ExceptionHandler(tryStart, tryEnd, handlerLabel, handlerStart, handler.Test));
1234
1235                     PopLabelBlock(LabelScopeKind.Catch);
1236                 
1237                     _locals.UndefineLocal(local, _instructions.Count);
1238                 }
1239
1240                 if (node.Fault != null) {
1241                     throw new NotImplementedException();
1242                 }
1243             }
1244             
1245             if (node.Finally != null) {
1246                 PushLabelBlock(LabelScopeKind.Finally);
1247
1248                 _instructions.MarkLabel(startOfFinally);
1249                 _instructions.EmitEnterFinally();
1250                 CompileAsVoid(node.Finally);
1251                 _instructions.EmitLeaveFinally();
1252
1253                 PopLabelBlock(LabelScopeKind.Finally);
1254             }
1255
1256             _instructions.MarkLabel(end);
1257
1258             PopLabelBlock(LabelScopeKind.Try);
1259         }
1260
1261         private void CompileDynamicExpression(Expression expr) {
1262             var node = (DynamicExpression)expr;
1263
1264             foreach (var arg in node.Arguments) {
1265                 Compile(arg);
1266             }
1267
1268             _instructions.EmitDynamic(node.DelegateType, node.Binder);
1269         }
1270
1271         private void CompileMethodCallExpression(Expression expr) {
1272             var node = (MethodCallExpression)expr;
1273             
1274             var parameters = node.Method.GetParameters();
1275
1276             // TODO:
1277             // Support pass by reference.
1278             // Note that LoopCompiler needs to be updated too.
1279
1280             // force compilation for now for ref types
1281             // also could be a mutable value type, Delegate.CreateDelegate and MethodInfo.Invoke both can't handle this, we
1282             // need to generate code.
1283             if (!CollectionUtils.TrueForAll(parameters, (p) => !p.ParameterType.IsByRef) ||
1284                 (!node.Method.IsStatic && node.Method.DeclaringType.IsValueType() && !node.Method.DeclaringType.IsPrimitive())) {
1285 #if MONO_INTERPRETER
1286                 throw new NotImplementedException ("Interpreter of ref types");
1287 #else
1288                 _forceCompile = true;
1289 #endif
1290             }
1291
1292             // CF bug workaround
1293             // TODO: can we do better if the delegate targets LightLambda.Run* method?
1294             if (PlatformAdaptationLayer.IsCompactFramework && 
1295                 node.Method.Name == "Invoke" && typeof(Delegate).IsAssignableFrom(node.Object.Type) && !node.Method.IsStatic) {
1296                     
1297                 Compile(
1298                     AstUtils.Convert(
1299                         Expression.Call(
1300                             node.Object,
1301                             node.Object.Type.GetMethod("DynamicInvoke"),
1302                             Expression.NewArrayInit(typeof(object), node.Arguments.Map((e) => AstUtils.Convert(e, typeof(object))))
1303                         ),
1304                         node.Type
1305                     )
1306                 );
1307
1308             } else {
1309                 if (!node.Method.IsStatic) {
1310                     Compile(node.Object);
1311                 }
1312
1313                 foreach (var arg in node.Arguments) {
1314                     Compile(arg);
1315                 }
1316
1317                 EmitCall(node.Method, parameters);
1318             }
1319         }
1320
1321         public void EmitCall(MethodInfo method) {
1322             EmitCall(method, method.GetParameters());
1323         }
1324
1325         public void EmitCall(MethodInfo method, ParameterInfo[] parameters) {
1326             Instruction instruction;
1327
1328             try {
1329                 instruction = CallInstruction.Create(method, parameters);
1330             } catch (SecurityException) {
1331                 _forceCompile = true;
1332                 
1333                 _instructions.Emit(new PopNInstruction((method.IsStatic ? 0 : 1) + parameters.Length));
1334                 if (method.ReturnType != typeof(void)) {
1335                     _instructions.EmitLoad(null);
1336                 }
1337
1338                 return;
1339             }
1340
1341             _instructions.Emit(instruction);
1342         }
1343
1344         private void CompileNewExpression(Expression expr) {
1345             var node = (NewExpression)expr;
1346
1347             if (node.Constructor != null) {
1348                 var parameters = node.Constructor.GetParameters();
1349                 if (!CollectionUtils.TrueForAll(parameters, (p) => !p.ParameterType.IsByRef)
1350 #if FEATURE_LCG
1351                      || node.Constructor.DeclaringType == typeof(DynamicMethod)
1352 #endif
1353                 ) {
1354                     _forceCompile = true;
1355                 }
1356             }
1357
1358             if (node.Constructor != null) {
1359                 foreach (var arg in node.Arguments) {
1360                     this.Compile(arg);
1361                 }
1362                 _instructions.EmitNew(node.Constructor);
1363             } else {
1364                 Debug.Assert(expr.Type.IsValueType());
1365                 _instructions.EmitDefaultValue(node.Type);
1366             }
1367         }
1368
1369         private void CompileMemberExpression(Expression expr) {
1370             var node = (MemberExpression)expr;
1371
1372             var member = node.Member;
1373             FieldInfo fi = member as FieldInfo;
1374             if (fi != null) {
1375                 if (fi.IsLiteral) {
1376                     _instructions.EmitLoad(fi.GetRawConstantValue(), fi.FieldType);
1377                 } else if (fi.IsStatic) {
1378                     if (fi.IsInitOnly) {
1379                         _instructions.EmitLoad(fi.GetValue(null), fi.FieldType);
1380                     } else {
1381                         _instructions.EmitLoadField(fi);
1382                     }
1383                 } else {
1384                     Compile(node.Expression);
1385                     _instructions.EmitLoadField(fi);
1386                 }
1387                 return;
1388             }
1389
1390             PropertyInfo pi = member as PropertyInfo;
1391             if (pi != null) {
1392                 var method = pi.GetGetMethod(true);
1393                 if (node.Expression != null) {
1394                     Compile(node.Expression);
1395                 }
1396                 EmitCall(method);
1397                 return;
1398             }
1399
1400
1401             throw new System.NotImplementedException();
1402         }
1403
1404         private void CompileNewArrayExpression(Expression expr) {
1405             var node = (NewArrayExpression)expr;
1406
1407             foreach (var arg in node.Expressions) {
1408                 Compile(arg);
1409             }
1410
1411             Type elementType = node.Type.GetElementType();
1412             int rank = node.Expressions.Count;
1413
1414             if (node.NodeType == ExpressionType.NewArrayInit) {
1415                 _instructions.EmitNewArrayInit(elementType, rank);
1416             } else if (node.NodeType == ExpressionType.NewArrayBounds) {
1417                 if (rank == 1) {
1418                     _instructions.EmitNewArray(elementType);
1419                 } else {
1420                     _instructions.EmitNewArrayBounds(elementType, rank);
1421                 }
1422             } else {
1423                 throw new System.NotImplementedException();
1424             }
1425         }
1426
1427         private void CompileExtensionExpression(Expression expr) {
1428             var instructionProvider = expr as IInstructionProvider;
1429             if (instructionProvider != null) {
1430                 instructionProvider.AddInstructions(this);
1431                 return;
1432             }
1433
1434             if (expr.CanReduce) {
1435                 Compile(expr.Reduce());
1436             } else {
1437                 throw new System.NotImplementedException();
1438             }
1439         }
1440
1441
1442         private void CompileDebugInfoExpression(Expression expr) {
1443             var node = (DebugInfoExpression)expr;
1444             int start = _instructions.Count;
1445             var info = new DebugInfo()
1446             {
1447                 Index = start,
1448                 FileName = node.Document.FileName,
1449                 StartLine = node.StartLine,
1450                 EndLine = node.EndLine,
1451                 IsClear = node.IsClear
1452             };
1453             _debugInfos.Add(info);
1454         }
1455
1456         private void CompileRuntimeVariablesExpression(Expression expr) {
1457             // Generates IRuntimeVariables for all requested variables
1458             var node = (RuntimeVariablesExpression)expr;
1459             foreach (var variable in node.Variables) {
1460                 EnsureAvailableForClosure(variable);
1461                 CompileGetBoxedVariable(variable);
1462             }
1463
1464             _instructions.EmitNewRuntimeVariables(node.Variables.Count);
1465         }
1466
1467
1468         private void CompileLambdaExpression(Expression expr) {
1469             var node = (LambdaExpression)expr;
1470             var compiler = new LightCompiler(this);
1471             var creator = compiler.CompileTop(node);
1472
1473             if (compiler._locals.ClosureVariables != null) {
1474                 foreach (ParameterExpression variable in compiler._locals.ClosureVariables.Keys) {
1475                     CompileGetBoxedVariable(variable);
1476                 }
1477             }
1478             _instructions.EmitCreateDelegate(creator);
1479         }
1480
1481         private void CompileCoalesceBinaryExpression(Expression expr) {
1482             var node = (BinaryExpression)expr;
1483
1484             if (TypeUtils.IsNullableType(node.Left.Type)) {
1485                 throw new NotImplementedException();
1486             } else if (node.Conversion != null) {
1487                 throw new NotImplementedException();
1488             } else {
1489                 var leftNotNull = _instructions.MakeLabel();
1490                 Compile(node.Left);
1491                 _instructions.EmitCoalescingBranch(leftNotNull);
1492                 _instructions.EmitPop();
1493                 Compile(node.Right);
1494                 _instructions.MarkLabel(leftNotNull);
1495             }
1496         }
1497
1498         private void CompileInvocationExpression(Expression expr) {
1499             var node = (InvocationExpression)expr;
1500
1501             // TODO: LambdaOperand optimization (see compiler)
1502             if (typeof(LambdaExpression).IsAssignableFrom(node.Expression.Type)) {
1503                 throw new System.NotImplementedException();
1504             }
1505
1506             // TODO: do not create a new Call Expression
1507             if (PlatformAdaptationLayer.IsCompactFramework) {
1508                 // Workaround for a bug in Compact Framework
1509                 Compile(
1510                     AstUtils.Convert(
1511                         Expression.Call(
1512                             node.Expression,
1513                             node.Expression.Type.GetMethod("DynamicInvoke"),
1514                             Expression.NewArrayInit(typeof(object), node.Arguments.Map((e) => AstUtils.Convert(e, typeof(object))))
1515                         ),
1516                         node.Type
1517                     )
1518                 );
1519             } else {
1520                 CompileMethodCallExpression(Expression.Call(node.Expression, node.Expression.Type.GetMethod("Invoke"), node.Arguments));
1521             }
1522         }
1523
1524         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "expr")]
1525         private void CompileListInitExpression(Expression expr) {
1526             throw new System.NotImplementedException();
1527         }
1528
1529         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "expr")]
1530         private void CompileMemberInitExpression(Expression expr) {
1531             throw new System.NotImplementedException();
1532         }
1533
1534         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "expr")]
1535         private void CompileQuoteUnaryExpression(Expression expr) {
1536             throw new System.NotImplementedException();
1537         }
1538
1539         private void CompileUnboxUnaryExpression(Expression expr) {
1540             var node = (UnaryExpression)expr;
1541             // unboxing is a nop:
1542             Compile(node.Operand);
1543         }
1544
1545         private void CompileTypeEqualExpression(Expression expr) {
1546             Debug.Assert(expr.NodeType == ExpressionType.TypeEqual);
1547             var node = (TypeBinaryExpression)expr;
1548
1549             Compile(node.Expression);
1550             _instructions.EmitLoad(node.TypeOperand);
1551             _instructions.EmitTypeEquals();
1552         }
1553
1554         private void CompileTypeAsExpression(UnaryExpression node) {
1555             Compile(node.Operand);
1556             _instructions.EmitTypeAs(node.Type);
1557         }
1558
1559         private void CompileTypeIsExpression(Expression expr) {
1560             Debug.Assert(expr.NodeType == ExpressionType.TypeIs);
1561             var node = (TypeBinaryExpression)expr;
1562
1563             Compile(node.Expression);
1564             if (node.Expression.Type == typeof (void)) {
1565                 _instructions.Emit (InstructionFactory<bool>.Factory.DefaultValue ());
1566                 return;
1567             }
1568
1569             // use TypeEqual for sealed types:
1570             if (node.TypeOperand.IsSealed()) {
1571                 _instructions.EmitLoad(node.TypeOperand);
1572                 _instructions.EmitTypeEquals();
1573             } else {
1574                 _instructions.EmitTypeIs(node.TypeOperand);
1575             }
1576         }
1577
1578         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "expr")]
1579         private void CompileReducibleExpression(Expression expr) {
1580             throw new System.NotImplementedException();
1581         }
1582
1583         internal void Compile(Expression expr, bool asVoid) {
1584             if (asVoid) {
1585                 CompileAsVoid(expr);
1586             } else {
1587                 Compile(expr);
1588             }
1589         }
1590
1591         internal void CompileAsVoid(Expression expr) {
1592             bool pushLabelBlock = TryPushLabelBlock(expr);
1593             int startingStackDepth = _instructions.CurrentStackDepth;
1594             switch (expr.NodeType) {
1595                 case ExpressionType.Assign:
1596                     CompileAssignBinaryExpression(expr, true);
1597                     break;
1598
1599                 case ExpressionType.Block:
1600                     CompileBlockExpression(expr, true);
1601                     break;
1602
1603                 case ExpressionType.Throw:
1604                     CompileThrowUnaryExpression(expr, true);
1605                     break;
1606
1607                 case ExpressionType.Constant:
1608                 case ExpressionType.Default:
1609                 case ExpressionType.Parameter:
1610                     // no-op
1611                     break;
1612
1613                 default:
1614                     CompileNoLabelPush(expr);
1615                     if (expr.Type != typeof(void)) {
1616                         _instructions.EmitPop();
1617                     }
1618                     break;
1619             }
1620             Debug.Assert(_instructions.CurrentStackDepth == startingStackDepth);
1621             if (pushLabelBlock) {
1622                 PopLabelBlock(_labelBlock.Kind);
1623             }
1624         }
1625
1626         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
1627         private void CompileNoLabelPush(Expression expr) {
1628             int startingStackDepth = _instructions.CurrentStackDepth;
1629             switch (expr.NodeType) {
1630                 case ExpressionType.Add: CompileBinaryExpression(expr); break;
1631                 case ExpressionType.AddChecked: CompileBinaryExpression(expr); break;
1632                 case ExpressionType.And: CompileBinaryExpression(expr); break;
1633                 case ExpressionType.AndAlso: CompileAndAlsoBinaryExpression(expr); break;
1634                 case ExpressionType.ArrayLength: CompileUnaryExpression(expr); break;
1635                 case ExpressionType.ArrayIndex: CompileBinaryExpression(expr); break;
1636                 case ExpressionType.Call: CompileMethodCallExpression(expr); break;
1637                 case ExpressionType.Coalesce: CompileCoalesceBinaryExpression(expr); break;
1638                 case ExpressionType.Conditional: CompileConditionalExpression(expr, expr.Type == typeof(void)); break;
1639                 case ExpressionType.Constant: CompileConstantExpression(expr); break;
1640                 case ExpressionType.Convert: CompileConvertUnaryExpression(expr); break;
1641                 case ExpressionType.ConvertChecked: CompileConvertUnaryExpression(expr); break;
1642                 case ExpressionType.Divide: CompileBinaryExpression(expr); break;
1643                 case ExpressionType.Equal: CompileBinaryExpression(expr); break;
1644                 case ExpressionType.ExclusiveOr: CompileBinaryExpression(expr); break;
1645                 case ExpressionType.GreaterThan: CompileBinaryExpression(expr); break;
1646                 case ExpressionType.GreaterThanOrEqual: CompileBinaryExpression(expr); break;
1647                 case ExpressionType.Invoke: CompileInvocationExpression(expr); break;
1648                 case ExpressionType.Lambda: CompileLambdaExpression(expr); break;
1649                 case ExpressionType.LeftShift: CompileBinaryExpression(expr); break;
1650                 case ExpressionType.LessThan: CompileBinaryExpression(expr); break;
1651                 case ExpressionType.LessThanOrEqual: CompileBinaryExpression(expr); break;
1652                 case ExpressionType.ListInit: CompileListInitExpression(expr); break;
1653                 case ExpressionType.MemberAccess: CompileMemberExpression(expr); break;
1654                 case ExpressionType.MemberInit: CompileMemberInitExpression(expr); break;
1655                 case ExpressionType.Modulo: CompileBinaryExpression(expr); break;
1656                 case ExpressionType.Multiply: CompileBinaryExpression(expr); break;
1657                 case ExpressionType.MultiplyChecked: CompileBinaryExpression(expr); break;
1658                 case ExpressionType.Negate: CompileUnaryExpression(expr); break;
1659                 case ExpressionType.UnaryPlus: CompileUnaryExpression(expr); break;
1660                 case ExpressionType.NegateChecked: CompileUnaryExpression(expr); break;
1661                 case ExpressionType.New: CompileNewExpression(expr); break;
1662                 case ExpressionType.NewArrayInit: CompileNewArrayExpression(expr); break;
1663                 case ExpressionType.NewArrayBounds: CompileNewArrayExpression(expr); break;
1664                 case ExpressionType.Not: CompileUnaryExpression(expr); break;
1665                 case ExpressionType.NotEqual: CompileBinaryExpression(expr); break;
1666                 case ExpressionType.Or: CompileBinaryExpression(expr); break;
1667                 case ExpressionType.OrElse: CompileOrElseBinaryExpression(expr); break;
1668                 case ExpressionType.Parameter: CompileParameterExpression(expr); break;
1669                 case ExpressionType.Power: CompileBinaryExpression(expr); break;
1670                 case ExpressionType.Quote: CompileQuoteUnaryExpression(expr); break;
1671                 case ExpressionType.RightShift: CompileBinaryExpression(expr); break;
1672                 case ExpressionType.Subtract: CompileBinaryExpression(expr); break;
1673                 case ExpressionType.SubtractChecked: CompileBinaryExpression(expr); break;
1674                 case ExpressionType.TypeAs: CompileUnaryExpression(expr); break;
1675                 case ExpressionType.TypeIs: CompileTypeIsExpression(expr); break;
1676                 case ExpressionType.Assign: CompileAssignBinaryExpression(expr, expr.Type == typeof(void)); break;
1677                 case ExpressionType.Block: CompileBlockExpression(expr, expr.Type == typeof(void)); break;
1678                 case ExpressionType.DebugInfo: CompileDebugInfoExpression(expr); break;
1679                 case ExpressionType.Decrement: CompileUnaryExpression(expr); break;
1680                 case ExpressionType.Dynamic: CompileDynamicExpression(expr); break;
1681                 case ExpressionType.Default: CompileDefaultExpression(expr); break;
1682                 case ExpressionType.Extension: CompileExtensionExpression(expr); break;
1683                 case ExpressionType.Goto: CompileGotoExpression(expr); break;
1684                 case ExpressionType.Increment: CompileUnaryExpression(expr); break;
1685                 case ExpressionType.Index: CompileIndexExpression(expr); break;
1686                 case ExpressionType.Label: CompileLabelExpression(expr); break;
1687                 case ExpressionType.RuntimeVariables: CompileRuntimeVariablesExpression(expr); break;
1688                 case ExpressionType.Loop: CompileLoopExpression(expr); break;
1689                 case ExpressionType.Switch: CompileSwitchExpression(expr); break;
1690                 case ExpressionType.Throw: CompileThrowUnaryExpression(expr, expr.Type == typeof(void)); break;
1691                 case ExpressionType.Try: CompileTryExpression(expr); break;
1692                 case ExpressionType.Unbox: CompileUnboxUnaryExpression(expr); break;
1693                 case ExpressionType.TypeEqual: CompileTypeEqualExpression(expr); break;
1694                 case ExpressionType.OnesComplement: CompileUnaryExpression(expr); break;
1695                 case ExpressionType.IsTrue: CompileUnaryExpression(expr); break;
1696                 case ExpressionType.IsFalse: CompileUnaryExpression(expr); break;
1697                 case ExpressionType.AddAssign:
1698                 case ExpressionType.AndAssign:
1699                 case ExpressionType.DivideAssign:
1700                 case ExpressionType.ExclusiveOrAssign:
1701                 case ExpressionType.LeftShiftAssign:
1702                 case ExpressionType.ModuloAssign:
1703                 case ExpressionType.MultiplyAssign:
1704                 case ExpressionType.OrAssign:
1705                 case ExpressionType.PowerAssign:
1706                 case ExpressionType.RightShiftAssign:
1707                 case ExpressionType.SubtractAssign:
1708                 case ExpressionType.AddAssignChecked:
1709                 case ExpressionType.MultiplyAssignChecked:
1710                 case ExpressionType.SubtractAssignChecked:
1711                 case ExpressionType.PreIncrementAssign:
1712                 case ExpressionType.PreDecrementAssign:
1713                 case ExpressionType.PostIncrementAssign:
1714                 case ExpressionType.PostDecrementAssign:
1715                     CompileReducibleExpression(expr); break;
1716                 default: throw Assert.Unreachable;
1717             };
1718             Debug.Assert(_instructions.CurrentStackDepth == startingStackDepth + (expr.Type == typeof(void) ? 0 : 1));
1719         }
1720
1721         public void Compile(Expression expr) {
1722             bool pushLabelBlock = TryPushLabelBlock(expr);
1723             CompileNoLabelPush(expr);
1724             if (pushLabelBlock) {
1725                 PopLabelBlock(_labelBlock.Kind);
1726             }
1727         }
1728
1729     }
1730 }