1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using System.Diagnostics;
18 using System.Collections.Generic;
19 using Microsoft.Scripting.Utils;
21 namespace Microsoft.Scripting.Interpreter {
22 internal struct RuntimeLabel {
23 public readonly int Index;
24 public readonly int StackDepth;
25 public readonly int ContinuationStackDepth;
27 public RuntimeLabel(int index, int continuationStackDepth, int stackDepth) {
29 ContinuationStackDepth = continuationStackDepth;
30 StackDepth = stackDepth;
33 public override string ToString() {
34 return String.Format("->{0} C({1}) S({2})", Index, ContinuationStackDepth, StackDepth);
38 public sealed class BranchLabel {
39 internal const int UnknownIndex = Int32.MinValue;
40 internal const int UnknownDepth = Int32.MinValue;
42 internal int _labelIndex = UnknownIndex;
43 internal int _targetIndex = UnknownIndex;
44 internal int _stackDepth = UnknownDepth;
45 internal int _continuationStackDepth = UnknownDepth;
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;
51 public BranchLabel() {
54 internal int LabelIndex {
55 get { return _labelIndex; }
56 set { _labelIndex = value; }
59 internal bool HasRuntimeLabel {
60 get { return _labelIndex != UnknownIndex; }
63 internal int TargetIndex {
64 get { return _targetIndex; }
67 internal RuntimeLabel ToRuntimeLabel() {
68 Debug.Assert(_targetIndex != UnknownIndex && _stackDepth != UnknownDepth && _continuationStackDepth != UnknownDepth);
69 return new RuntimeLabel(_targetIndex, _continuationStackDepth, _stackDepth);
72 internal void Mark(InstructionList instructions) {
73 ContractUtils.Requires(_targetIndex == UnknownIndex && _stackDepth == UnknownDepth && _continuationStackDepth == UnknownDepth);
75 _stackDepth = instructions.CurrentStackDepth;
76 _continuationStackDepth = instructions.CurrentContinuationsDepth;
77 _targetIndex = instructions.Count;
79 if (_forwardBranchFixups != null) {
80 foreach (var branchIndex in _forwardBranchFixups) {
81 FixupBranch(instructions, branchIndex);
83 _forwardBranchFixups = null;
87 internal void AddBranch(InstructionList instructions, int branchIndex) {
88 Debug.Assert(((_targetIndex == UnknownIndex) == (_stackDepth == UnknownDepth)));
89 Debug.Assert(((_targetIndex == UnknownIndex) == (_continuationStackDepth == UnknownDepth)));
91 if (_targetIndex == UnknownIndex) {
92 if (_forwardBranchFixups == null) {
93 _forwardBranchFixups = new List<int>();
95 _forwardBranchFixups.Add(branchIndex);
97 FixupBranch(instructions, branchIndex);
101 internal void FixupBranch(InstructionList instructions, int branchIndex) {
102 Debug.Assert(_targetIndex != UnknownIndex);
103 instructions.FixupBranch(branchIndex, _targetIndex - branchIndex);