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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System.Collections.Generic;
using System.Runtime;
using System.Threading;
enum OpcodeID
{
NoOp,
SubExpr,
// Flow opcodes
Branch,
JumpIfNot,
Filter,
Function,
XsltFunction,
XsltInternalFunction,
Cast,
QueryTree,
BlockEnd,
SubRoutine,
// Set Opcodes
Ordinal,
LiteralOrdinal,
Empty,
Union,
Merge,
// Boolean opcodes
ApplyBoolean,
StartBoolean,
EndBoolean,
// Relational opcodes
Relation,
StringEquals,
NumberEquals,
StringEqualsBranch,
NumberEqualsBranch,
NumberRelation,
NumberInterval,
NumberIntervalBranch,
// Select/Node Operators
Select,
InitialSelect,
SelectRoot,
// Stack operators
PushXsltVariable,
PushBool,
PushString,
PushDouble,
PushContextNode,
PushNodeSequence,
PushPosition,
PopSequenceToValueStack,
PopSequenceToSequenceStack,
PopContextNodes,
PushContextCopy,
PopValueFrame,
// Math opcode
Plus,
Minus,
Multiply,
Divide,
Mod,
Negate,
// Specialized String operators
StringPrefix,
StringPrefixBranch,
// Results
MatchAlways,
MatchResult,
MatchFilterResult,
MatchMultipleResult,
MatchSingleFx,
QuerySingleFx,
QueryResult,
QueryMultipleResult
}
enum OpcodeFlags
{
None = 0x00000000,
Single = 0x00000001,
Multiple = 0x00000002,
Branch = 0x00000004,
Result = 0x00000008,
Jump = 0x00000010,
Literal = 0x00000020,
Select = 0x00000040,
Deleted = 0x00000080,
InConditional = 0x00000100,
NoContextCopy = 0x00000200,
InitialSelect = 0x00000400,
CompressableSelect = 0x00000800,
Fx = 0x00001000
}
abstract class Opcode
{
protected OpcodeFlags flags;
protected Opcode next;
OpcodeID opcodeID;
protected Opcode prev;
#if DEBUG
// debugging aid. Because C# references do not have displayble numeric values, hard to deduce the
// graph structure to see what opcode is connected to what
static long nextUniqueId = 0;
internal long uniqueID;
#endif
internal Opcode(OpcodeID id)
{
this.opcodeID = id;
this.flags = OpcodeFlags.Single;
#if DEBUG
this.uniqueID = Opcode.NextUniqueId();
#endif
}
internal OpcodeFlags Flags
{
get
{
return this.flags;
}
set
{
this.flags = value;
}
}
internal OpcodeID ID
{
get
{
return this.opcodeID;
}
}
internal Opcode Next
{
get
{
return this.next;
}
set
{
this.next = value;
}
}
internal Opcode Prev
{
get
{
return this.prev;
}
set
{
this.prev = value;
}
}
#if DEBUG
static long NextUniqueId()
{
return Interlocked.Increment(ref Opcode.nextUniqueId);
}
#endif
internal virtual void Add(Opcode op)
{
Fx.Assert(null != op, "");
Fx.Assert(null != this.prev, "");
// Create a branch that will include both this and the new opcode
this.prev.AddBranch(op);
}
internal virtual void AddBranch(Opcode opcode)
{
Fx.Assert(null != opcode, "");
// Replace what follows this opcode with a branch containing .next and the new opcode
// If this opcode is a conditional, then since the tree structure is about to change, conditional
// reachability for everything that follows is about to change.
// 1. Remove .next from the conditional's AlwaysBranch Table.
// 2. Create the new branch structure.
// 3. The branch, once in the tree, will fix up all conditional jumps
Opcode next = this.next;
if (this.TestFlag(OpcodeFlags.InConditional))
{
this.DelinkFromConditional(next);
}
BranchOpcode branch = new BranchOpcode();
this.next = null;
this.Attach(branch);
if (null != next)
{
Fx.Assert(OpcodeID.Branch != next.ID, "");
branch.Add(next);
}
branch.Add(opcode);
}
internal void Attach(Opcode op)
{
Fx.Assert(null == this.next, "");
this.next = op;
op.prev = this;
}
internal virtual void CollectXPathFilters(ICollection<MessageFilter> filters)
{
if (this.next != null)
{
this.next.CollectXPathFilters(filters);
}
}
internal virtual bool IsEquivalentForAdd(Opcode opcode)
{
return (this.ID == opcode.ID);
}
internal bool IsMultipleResult()
{
return ((this.flags & (OpcodeFlags.Result | OpcodeFlags.Multiple)) ==
(OpcodeFlags.Result | OpcodeFlags.Multiple));
}
internal virtual void DelinkFromConditional(Opcode child)
{
Fx.Assert(this.TestFlag(OpcodeFlags.InConditional), "");
if (this.TestFlag(OpcodeFlags.InConditional))
{
((QueryConditionalBranchOpcode)this.prev).RemoveAlwaysBranch(child);
}
}
internal Opcode DetachChild()
{
Opcode child = this.next;
if (child != null)
{
if (this.IsInConditional())
{
this.DelinkFromConditional(child);
}
}
this.next = null;
child.prev = null;
return child;
}
internal void DetachFromParent()
{
Opcode parent = this.prev;
if (parent != null)
{
parent.DetachChild();
}
}
internal virtual bool Equals(Opcode op)
{
return (op.ID == this.ID);
}
internal virtual Opcode Eval(ProcessingContext context)
{
return this.next;
}
internal virtual Opcode Eval(NodeSequence sequence, SeekableXPathNavigator node)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new QueryProcessingException(QueryProcessingError.Unexpected));
}
internal virtual Opcode EvalSpecial(ProcessingContext context)
{
return this.Eval(context);
}
internal virtual bool IsInConditional()
{
return this.TestFlag(OpcodeFlags.InConditional);
}
internal bool IsReachableFromConditional()
{
return (null != this.prev && this.prev.IsInConditional());
}
internal virtual Opcode Locate(Opcode opcode)
{
Fx.Assert(null != opcode, "");
if (null != this.next && this.next.Equals(opcode))
{
return this.next;
}
return null;
}
internal virtual void LinkToConditional(Opcode child)
{
Fx.Assert(this.TestFlag(OpcodeFlags.InConditional), "");
if (this.TestFlag(OpcodeFlags.InConditional))
{
((QueryConditionalBranchOpcode)this.prev).AddAlwaysBranch(this, child);
}
}
internal virtual void Remove()
{
if (null == this.next)
{
Opcode prevOpcode = this.prev;
if (null != prevOpcode)
{
prevOpcode.RemoveChild(this);
prevOpcode.Remove();
}
}
}
internal virtual void RemoveChild(Opcode opcode)
{
Fx.Assert(null != opcode, "");
Fx.Assert(this.next == opcode, "");
if (this.IsInConditional())
{
this.DelinkFromConditional(opcode);
}
opcode.prev = null;
this.next = null;
opcode.Flags |= OpcodeFlags.Deleted;
}
internal virtual void Replace(Opcode replace, Opcode with)
{
Fx.Assert(null != replace && null != with, "");
if (this.next == replace)
{
bool isConditional = this.IsInConditional();
if (isConditional)
{
this.DelinkFromConditional(this.next);
}
this.next.prev = null;
this.next = with;
with.prev = this;
if (isConditional)
{
this.LinkToConditional(with);
}
}
}
internal bool TestFlag(OpcodeFlags flag)
{
return (0 != (this.flags & flag));
}
#if DEBUG_FILTER
public override string ToString()
{
#if DEBUG
return string.Format("{0}(#{1})", this.opcodeID.ToString(), this.uniqueID);
#else
return this.opcodeID.ToString();
#endif
}
#endif
internal virtual void Trim()
{
if (this.next != null)
{
this.next.Trim();
}
}
}
struct OpcodeBlock
{
Opcode first;
Opcode last;
internal OpcodeBlock(Opcode first)
{
this.first = first;
this.first.Prev = null;
for (this.last = this.first; this.last.Next != null; this.last = this.last.Next);
}
#if FILTEROPTIMIZER
internal OpcodeBlock(Opcode first, Opcode last)
{
this.first = first;
this.first.Prev = null;
this.last = last;
this.Last.Next = null;
}
#endif
internal Opcode First
{
get
{
return this.first;
}
}
internal Opcode Last
{
get
{
return this.last;
}
}
internal void Append(Opcode opcode)
{
Fx.Assert(null != opcode, "");
if (null == this.last)
{
this.first = opcode;
this.last = opcode;
}
else
{
this.last.Attach(opcode);
opcode.Next = null;
this.last = opcode;
}
}
internal void Append(OpcodeBlock block)
{
if (null == this.last)
{
this.first = block.first;
this.last = block.last;
}
else
{
this.last.Attach(block.first);
this.last = block.last;
}
}
internal void DetachLast()
{
if (null == this.last)
{
return;
}
Opcode newLast = this.last.Prev;
this.last.Prev = null;
this.last = newLast;
if (null != this.last)
{
this.last.Next = null;
}
}
}
class OpcodeList
{
QueryBuffer<Opcode> opcodes;
public OpcodeList(int capacity)
{
this.opcodes = new QueryBuffer<Opcode>(capacity);
}
public int Count
{
get
{
return this.opcodes.count;
}
}
public Opcode this[int index]
{
get
{
return this.opcodes[index];
}
set
{
this.opcodes[index] = value;
}
}
public void Add(Opcode opcode)
{
this.opcodes.Add(opcode);
}
public int IndexOf(Opcode opcode)
{
return this.opcodes.IndexOf(opcode);
}
public void Remove(Opcode opcode)
{
this.opcodes.Remove(opcode);
}
public void Trim()
{
this.opcodes.TrimToCount();
}
}
}
|