File size: 9,890 Bytes
8c763fb | 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 | /* ****************************************************************************
*
* 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.
*
*
* ***************************************************************************/
#if !CLR2
using System.Globalization;
using System.Linq.Expressions;
#else
using Microsoft.Scripting.Ast;
#endif
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace System.Management.Automation.Interpreter
{
internal sealed class LocalVariable
{
private const int IsBoxedFlag = 1;
private const int InClosureFlag = 2;
public readonly int Index;
private int _flags;
public bool IsBoxed
{
get
{
return (_flags & IsBoxedFlag) != 0;
}
set
{
if (value)
{
_flags |= IsBoxedFlag;
}
else
{
_flags &= ~IsBoxedFlag;
}
}
}
public bool InClosure
{
get { return (_flags & InClosureFlag) != 0; }
}
public bool InClosureOrBoxed
{
get { return InClosure || IsBoxed; }
}
internal LocalVariable(int index, bool closure, bool boxed)
{
Index = index;
_flags = (closure ? InClosureFlag : 0) | (boxed ? IsBoxedFlag : 0);
}
internal Expression LoadFromArray(Expression frameData, Expression closure)
{
Expression result = Expression.ArrayAccess(InClosure ? closure : frameData, Expression.Constant(Index));
return IsBoxed ? Expression.Convert(result, typeof(StrongBox<object>)) : result;
}
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "{0}: {1} {2}", Index, IsBoxed ? "boxed" : null, InClosure ? "in closure" : null);
}
}
internal readonly struct LocalDefinition
{
private readonly int _index;
private readonly ParameterExpression _parameter;
internal LocalDefinition(int localIndex, ParameterExpression parameter)
{
_index = localIndex;
_parameter = parameter;
}
public int Index
{
get
{
return _index;
}
}
public ParameterExpression Parameter
{
get
{
return _parameter;
}
}
public override bool Equals(object obj)
{
if (obj is LocalDefinition)
{
LocalDefinition other = (LocalDefinition)obj;
return other.Index == Index && other.Parameter == Parameter;
}
return false;
}
public override int GetHashCode()
{
if (_parameter == null)
{
return 0;
}
return _parameter.GetHashCode() ^ _index.GetHashCode();
}
public static bool operator ==(LocalDefinition self, LocalDefinition other)
{
return self.Index == other.Index && self.Parameter == other.Parameter;
}
public static bool operator !=(LocalDefinition self, LocalDefinition other)
{
return self.Index != other.Index || self.Parameter != other.Parameter;
}
}
internal sealed class LocalVariables
{
private readonly HybridReferenceDictionary<ParameterExpression, VariableScope> _variables = new HybridReferenceDictionary<ParameterExpression, VariableScope>();
private Dictionary<ParameterExpression, LocalVariable> _closureVariables;
private int _localCount, _maxLocalCount;
internal LocalVariables()
{
}
public LocalDefinition DefineLocal(ParameterExpression variable, int start)
{
// ContractUtils.RequiresNotNull(variable, "variable");
// ContractUtils.Requires(start >= 0, "start", "start must be positive");
LocalVariable result = new LocalVariable(_localCount++, false, false);
_maxLocalCount = System.Math.Max(_localCount, _maxLocalCount);
VariableScope existing, newScope;
if (_variables.TryGetValue(variable, out existing))
{
newScope = new VariableScope(result, start, existing);
existing.ChildScopes ??= new List<VariableScope>();
existing.ChildScopes.Add(newScope);
}
else
{
newScope = new VariableScope(result, start, null);
}
_variables[variable] = newScope;
return new LocalDefinition(result.Index, variable);
}
public void UndefineLocal(LocalDefinition definition, int end)
{
var scope = _variables[definition.Parameter];
scope.Stop = end;
if (scope.Parent != null)
{
_variables[definition.Parameter] = scope.Parent;
}
else
{
_variables.Remove(definition.Parameter);
}
_localCount--;
}
internal void Box(ParameterExpression variable, InstructionList instructions)
{
var scope = _variables[variable];
LocalVariable local = scope.Variable;
Debug.Assert(!local.IsBoxed && !local.InClosure);
_variables[variable].Variable.IsBoxed = true;
int curChild = 0;
for (int i = scope.Start; i < scope.Stop && i < instructions.Count; i++)
{
if (scope.ChildScopes != null && scope.ChildScopes[curChild].Start == i)
{
// skip boxing in the child scope
var child = scope.ChildScopes[curChild];
i = child.Stop;
curChild++;
continue;
}
instructions.SwitchToBoxed(local.Index, i);
}
}
public int LocalCount
{
get { return _maxLocalCount; }
}
public int GetOrDefineLocal(ParameterExpression var)
{
int index = GetLocalIndex(var);
if (index == -1)
{
return DefineLocal(var, 0).Index;
}
return index;
}
public int GetLocalIndex(ParameterExpression var)
{
VariableScope loc;
return _variables.TryGetValue(var, out loc) ? loc.Variable.Index : -1;
}
public bool TryGetLocalOrClosure(ParameterExpression var, out LocalVariable local)
{
VariableScope scope;
if (_variables.TryGetValue(var, out scope))
{
local = scope.Variable;
return true;
}
if (_closureVariables != null && _closureVariables.TryGetValue(var, out local))
{
return true;
}
local = null;
return false;
}
/// <summary>
/// Gets a copy of the local variables which are defined in the current scope.
/// </summary>
/// <returns></returns>
internal Dictionary<ParameterExpression, LocalVariable> CopyLocals()
{
var res = new Dictionary<ParameterExpression, LocalVariable>(_variables.Count);
foreach (var keyValue in _variables)
{
res[keyValue.Key] = keyValue.Value.Variable;
}
return res;
}
/// <summary>
/// Checks to see if the given variable is defined within the current local scope.
/// </summary>
internal bool ContainsVariable(ParameterExpression variable)
{
return _variables.ContainsKey(variable);
}
/// <summary>
/// Gets the variables which are defined in an outer scope and available within the current scope.
/// </summary>
internal Dictionary<ParameterExpression, LocalVariable> ClosureVariables
{
get
{
return _closureVariables;
}
}
internal LocalVariable AddClosureVariable(ParameterExpression variable)
{
_closureVariables ??= new Dictionary<ParameterExpression, LocalVariable>();
LocalVariable result = new LocalVariable(_closureVariables.Count, true, false);
_closureVariables.Add(variable, result);
return result;
}
/// <summary>
/// Tracks where a variable is defined and what range of instructions it's used in.
/// </summary>
private sealed class VariableScope
{
public readonly int Start;
public int Stop = Int32.MaxValue;
public readonly LocalVariable Variable;
public readonly VariableScope Parent;
public List<VariableScope> ChildScopes;
public VariableScope(LocalVariable variable, int start, VariableScope parent)
{
Variable = variable;
Start = start;
Parent = parent;
}
}
}
}
|