Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / BranchLabel.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 using System;
17 using System.Diagnostics;
18 using System.Collections.Generic;
19 using Microsoft.Scripting.Utils;
20
21 namespace Microsoft.Scripting.Interpreter {
22     internal struct RuntimeLabel {
23         public readonly int Index;
24         public readonly int StackDepth;
25         public readonly int ContinuationStackDepth;
26
27         public RuntimeLabel(int index, int continuationStackDepth, int stackDepth) {
28             Index = index;
29             ContinuationStackDepth = continuationStackDepth;
30             StackDepth = stackDepth;
31         }
32
33         public override string ToString() {
34             return String.Format("->{0} C({1}) S({2})", Index, ContinuationStackDepth, StackDepth);
35         }
36     }
37
38     public sealed class BranchLabel {
39         internal const int UnknownIndex = Int32.MinValue;
40         internal const int UnknownDepth = Int32.MinValue;
41
42         internal int _labelIndex = UnknownIndex;
43         internal int _targetIndex = UnknownIndex;
44         internal int _stackDepth = UnknownDepth;
45         internal int _continuationStackDepth = UnknownDepth;
46
47         // Offsets of forward branching instructions targetting this label 
48         // that need to be updated after we emit the label.
49         private List<int> _forwardBranchFixups;
50
51         public BranchLabel() {
52         }
53
54         internal int LabelIndex {
55             get { return _labelIndex; }
56             set { _labelIndex = value; }
57         }
58
59         internal bool HasRuntimeLabel {
60             get { return _labelIndex != UnknownIndex; } 
61         }
62
63         internal int TargetIndex {
64             get { return _targetIndex; }
65         }
66
67         internal RuntimeLabel ToRuntimeLabel() {
68             Debug.Assert(_targetIndex != UnknownIndex && _stackDepth != UnknownDepth && _continuationStackDepth != UnknownDepth);
69             return new RuntimeLabel(_targetIndex, _continuationStackDepth, _stackDepth);
70         }
71
72         internal void Mark(InstructionList instructions) {
73             ContractUtils.Requires(_targetIndex == UnknownIndex && _stackDepth == UnknownDepth && _continuationStackDepth == UnknownDepth);
74
75             _stackDepth = instructions.CurrentStackDepth;
76             _continuationStackDepth = instructions.CurrentContinuationsDepth;
77             _targetIndex = instructions.Count;
78
79             if (_forwardBranchFixups != null) {
80                 foreach (var branchIndex in _forwardBranchFixups) {
81                     FixupBranch(instructions, branchIndex);
82                 }
83                 _forwardBranchFixups = null;
84             }
85         }
86
87         internal void AddBranch(InstructionList instructions, int branchIndex) {
88             Debug.Assert(((_targetIndex == UnknownIndex) == (_stackDepth == UnknownDepth)));
89             Debug.Assert(((_targetIndex == UnknownIndex) == (_continuationStackDepth == UnknownDepth)));
90
91             if (_targetIndex == UnknownIndex) {
92                 if (_forwardBranchFixups == null) {
93                     _forwardBranchFixups = new List<int>();
94                 }
95                 _forwardBranchFixups.Add(branchIndex);
96             } else {
97                 FixupBranch(instructions, branchIndex);
98             }
99         }
100
101         internal void FixupBranch(InstructionList instructions, int branchIndex) {
102             Debug.Assert(_targetIndex != UnknownIndex);
103             instructions.FixupBranch(branchIndex, _targetIndex - branchIndex);
104         }
105     }
106
107
108 }