1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic.Utils;
using System.Reflection.Emit;
#if SILVERLIGHT
using System.Core;
#endif
#if CLR2
namespace Microsoft.Scripting.Ast.Compiler {
#else
namespace System.Linq.Expressions.Compiler {
#endif
#if CLR2 || SILVERLIGHT
using ILGenerator = OffsetTrackingILGenerator;
#endif
/// <summary>
/// Contains compiler state corresponding to a LabelTarget
/// See also LabelScopeInfo.
/// </summary>
internal sealed class LabelInfo {
// The tree node representing this label
private readonly LabelTarget _node;
// The IL label, will be mutated if Node is redefined
private Label _label;
private bool _labelDefined;
internal Label Label {
get {
EnsureLabelAndValue();
return _label;
}
}
// The local that carries the label's value, if any
private LocalBuilder _value;
// The blocks where this label is defined. If it has more than one item,
// the blocks can't be jumped to except from a child block
private readonly Set<LabelScopeInfo> _definitions = new Set<LabelScopeInfo>();
// Blocks that jump to this block
private readonly List<LabelScopeInfo> _references = new List<LabelScopeInfo>();
// True if this label is the last thing in this block
// (meaning we can emit a direct return)
private readonly bool _canReturn;
// True if at least one jump is across blocks
// If we have any jump across blocks to this label, then the
// LabelTarget can only be defined in one place
private bool _acrossBlockJump;
// Until we have more information, default to a leave instruction,
// which always works. Note: leave spills the stack, so we need to
// ensure that StackSpiller has guarenteed us an empty stack at this
// point. Otherwise Leave and Branch are not equivalent
private OpCode _opCode = OpCodes.Leave;
private readonly ILGenerator _ilg;
internal LabelInfo(ILGenerator il, LabelTarget node, bool canReturn) {
_ilg = il;
_node = node;
_canReturn = canReturn;
}
internal bool CanReturn {
get { return _canReturn; }
}
/// <summary>
/// Indicates if it is legal to emit a "branch" instruction based on
/// currently available information. Call the Reference method before
/// using this property.
/// </summary>
internal bool CanBranch {
get { return _opCode != OpCodes.Leave; }
}
internal void Reference(LabelScopeInfo block) {
_references.Add(block);
if (_definitions.Count > 0) {
ValidateJump(block);
}
}
// Returns true if the label was successfully defined
// or false if the label is now ambiguous
internal void Define(LabelScopeInfo block) {
// Prevent the label from being shadowed, which enforces cleaner
// trees. Also we depend on this for simplicity (keeping only one
// active IL Label per LabelInfo)
for (LabelScopeInfo j = block; j != null; j = j.Parent) {
if (j.ContainsTarget(_node)) {
throw Error.LabelTargetAlreadyDefined(_node.Name);
}
}
_definitions.Add(block);
block.AddLabelInfo(_node, this);
// Once defined, validate all jumps
if (_definitions.Count == 1) {
foreach (var r in _references) {
ValidateJump(r);
}
} else {
// Was just redefined, if we had any across block jumps, they're
// now invalid
if (_acrossBlockJump) {
throw Error.AmbiguousJump(_node.Name);
}
// For local jumps, we need a new IL label
// This is okay because:
// 1. no across block jumps have been made or will be made
// 2. we don't allow the label to be shadowed
_labelDefined = false;
}
}
private void ValidateJump(LabelScopeInfo reference) {
// Assume we can do a ret/branch
_opCode = _canReturn ? OpCodes.Ret : OpCodes.Br;
// look for a simple jump out
for (LabelScopeInfo j = reference; j != null; j = j.Parent) {
if (_definitions.Contains(j)) {
// found it, jump is valid!
return;
}
if (j.Kind == LabelScopeKind.Finally ||
j.Kind == LabelScopeKind.Filter) {
break;
}
if (j.Kind == LabelScopeKind.Try ||
j.Kind == LabelScopeKind.Catch) {
_opCode = OpCodes.Leave;
}
}
_acrossBlockJump = true;
if (_node != null && _node.Type != typeof(void)) {
throw Error.NonLocalJumpWithValue(_node.Name);
}
if (_definitions.Count > 1) {
throw Error.AmbiguousJump(_node.Name);
}
// We didn't find an outward jump. Look for a jump across blocks
LabelScopeInfo def = _definitions.First();
LabelScopeInfo common = Helpers.CommonNode(def, reference, b => b.Parent);
// Assume we can do a ret/branch
_opCode = _canReturn ? OpCodes.Ret : OpCodes.Br;
// Validate that we aren't jumping across a finally
for (LabelScopeInfo j = reference; j != common; j = j.Parent) {
if (j.Kind == LabelScopeKind.Finally) {
throw Error.ControlCannotLeaveFinally();
}
if (j.Kind == LabelScopeKind.Filter) {
throw Error.ControlCannotLeaveFilterTest();
}
if (j.Kind == LabelScopeKind.Try ||
j.Kind == LabelScopeKind.Catch) {
_opCode = OpCodes.Leave;
}
}
// Valdiate that we aren't jumping into a catch or an expression
for (LabelScopeInfo j = def; j != common; j = j.Parent) {
if (!j.CanJumpInto) {
if (j.Kind == LabelScopeKind.Expression) {
throw Error.ControlCannotEnterExpression();
} else {
throw Error.ControlCannotEnterTry();
}
}
}
}
internal void ValidateFinish() {
// Make sure that if this label was jumped to, it is also defined
if (_references.Count > 0 && _definitions.Count == 0) {
throw Error.LabelTargetUndefined(_node.Name);
}
}
internal void EmitJump() {
// Return directly if we can
if (_opCode == OpCodes.Ret) {
_ilg.Emit(OpCodes.Ret);
} else {
StoreValue();
_ilg.Emit(_opCode, Label);
}
}
private void StoreValue() {
EnsureLabelAndValue();
if (_value != null) {
_ilg.Emit(OpCodes.Stloc, _value);
}
}
internal void Mark() {
if (_canReturn) {
// Don't mark return labels unless they were actually jumped to
// (returns are last so we know for sure if anyone jumped to it)
if (!_labelDefined) {
// We don't even need to emit the "ret" because
// LambdaCompiler does that for us.
return;
}
// Otherwise, emit something like:
// ret
// <marked label>:
// ldloc <value>
_ilg.Emit(OpCodes.Ret);
} else {
// For the normal case, we emit:
// stloc <value>
// <marked label>:
// ldloc <value>
StoreValue();
}
MarkWithEmptyStack();
}
// Like Mark, but assumes the stack is empty
internal void MarkWithEmptyStack() {
_ilg.MarkLabel(Label);
if (_value != null) {
// We always read the value from a local, because we don't know
// if there will be a "leave" instruction targeting it ("branch"
// preserves its stack, but "leave" empties the stack)
_ilg.Emit(OpCodes.Ldloc, _value);
}
}
private void EnsureLabelAndValue() {
if (!_labelDefined) {
_labelDefined = true;
_label = _ilg.DefineLabel();
if (_node != null && _node.Type != typeof(void)) {
_value = _ilg.DeclareLocal(_node.Type);
}
}
}
}
internal enum LabelScopeKind {
// any "statement like" node that can be jumped into
Statement,
// these correspond to the node of the same name
Block,
Switch,
Lambda,
Try,
// these correspond to the part of the try block we're in
Catch,
Finally,
Filter,
// the catch-all value for any other expression type
// (means we can't jump into it)
Expression,
}
//
// Tracks scoping information for LabelTargets. Logically corresponds to a
// "label scope". Even though we have arbitrary goto support, we still need
// to track what kinds of nodes that gotos are jumping through, both to
// emit property IL ("leave" out of a try block), and for validation, and
// to allow labels to be duplicated in the tree, as long as the jumps are
// considered "up only" jumps.
//
// We create one of these for every Expression that can be jumped into, as
// well as creating them for the first expression we can't jump into. The
// "Kind" property indicates what kind of scope this is.
//
internal sealed class LabelScopeInfo {
private Dictionary<LabelTarget, LabelInfo> Labels; // lazily allocated, we typically use this only once every 6th-7th block
internal readonly LabelScopeKind Kind;
internal readonly LabelScopeInfo Parent;
internal LabelScopeInfo(LabelScopeInfo parent, LabelScopeKind kind) {
Parent = parent;
Kind = kind;
}
/// <summary>
/// Returns true if we can jump into this node
/// </summary>
internal bool CanJumpInto {
get {
switch (Kind) {
case LabelScopeKind.Block:
case LabelScopeKind.Statement:
case LabelScopeKind.Switch:
case LabelScopeKind.Lambda:
return true;
}
return false;
}
}
internal bool ContainsTarget(LabelTarget target) {
if (Labels == null) {
return false;
}
return Labels.ContainsKey(target);
}
internal bool TryGetLabelInfo(LabelTarget target, out LabelInfo info) {
if (Labels == null) {
info = null;
return false;
}
return Labels.TryGetValue(target, out info);
}
internal void AddLabelInfo(LabelTarget target, LabelInfo info) {
Debug.Assert(CanJumpInto);
if (Labels == null) {
Labels = new Dictionary<LabelTarget, LabelInfo>();
}
Labels.Add(target, info);
}
}
}
|