QueryOpcode.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Collections.Generic;
  7. using System.Runtime;
  8. using System.Threading;
  9. enum OpcodeID
  10. {
  11. NoOp,
  12. SubExpr,
  13. // Flow opcodes
  14. Branch,
  15. JumpIfNot,
  16. Filter,
  17. Function,
  18. XsltFunction,
  19. XsltInternalFunction,
  20. Cast,
  21. QueryTree,
  22. BlockEnd,
  23. SubRoutine,
  24. // Set Opcodes
  25. Ordinal,
  26. LiteralOrdinal,
  27. Empty,
  28. Union,
  29. Merge,
  30. // Boolean opcodes
  31. ApplyBoolean,
  32. StartBoolean,
  33. EndBoolean,
  34. // Relational opcodes
  35. Relation,
  36. StringEquals,
  37. NumberEquals,
  38. StringEqualsBranch,
  39. NumberEqualsBranch,
  40. NumberRelation,
  41. NumberInterval,
  42. NumberIntervalBranch,
  43. // Select/Node Operators
  44. Select,
  45. InitialSelect,
  46. SelectRoot,
  47. // Stack operators
  48. PushXsltVariable,
  49. PushBool,
  50. PushString,
  51. PushDouble,
  52. PushContextNode,
  53. PushNodeSequence,
  54. PushPosition,
  55. PopSequenceToValueStack,
  56. PopSequenceToSequenceStack,
  57. PopContextNodes,
  58. PushContextCopy,
  59. PopValueFrame,
  60. // Math opcode
  61. Plus,
  62. Minus,
  63. Multiply,
  64. Divide,
  65. Mod,
  66. Negate,
  67. // Specialized String operators
  68. StringPrefix,
  69. StringPrefixBranch,
  70. // Results
  71. MatchAlways,
  72. MatchResult,
  73. MatchFilterResult,
  74. MatchMultipleResult,
  75. MatchSingleFx,
  76. QuerySingleFx,
  77. QueryResult,
  78. QueryMultipleResult
  79. }
  80. enum OpcodeFlags
  81. {
  82. None = 0x00000000,
  83. Single = 0x00000001,
  84. Multiple = 0x00000002,
  85. Branch = 0x00000004,
  86. Result = 0x00000008,
  87. Jump = 0x00000010,
  88. Literal = 0x00000020,
  89. Select = 0x00000040,
  90. Deleted = 0x00000080,
  91. InConditional = 0x00000100,
  92. NoContextCopy = 0x00000200,
  93. InitialSelect = 0x00000400,
  94. CompressableSelect = 0x00000800,
  95. Fx = 0x00001000
  96. }
  97. abstract class Opcode
  98. {
  99. protected OpcodeFlags flags;
  100. protected Opcode next;
  101. OpcodeID opcodeID;
  102. protected Opcode prev;
  103. #if DEBUG
  104. // debugging aid. Because C# references do not have displayble numeric values, hard to deduce the
  105. // graph structure to see what opcode is connected to what
  106. static long nextUniqueId = 0;
  107. internal long uniqueID;
  108. #endif
  109. internal Opcode(OpcodeID id)
  110. {
  111. this.opcodeID = id;
  112. this.flags = OpcodeFlags.Single;
  113. #if DEBUG
  114. this.uniqueID = Opcode.NextUniqueId();
  115. #endif
  116. }
  117. internal OpcodeFlags Flags
  118. {
  119. get
  120. {
  121. return this.flags;
  122. }
  123. set
  124. {
  125. this.flags = value;
  126. }
  127. }
  128. internal OpcodeID ID
  129. {
  130. get
  131. {
  132. return this.opcodeID;
  133. }
  134. }
  135. internal Opcode Next
  136. {
  137. get
  138. {
  139. return this.next;
  140. }
  141. set
  142. {
  143. this.next = value;
  144. }
  145. }
  146. internal Opcode Prev
  147. {
  148. get
  149. {
  150. return this.prev;
  151. }
  152. set
  153. {
  154. this.prev = value;
  155. }
  156. }
  157. #if DEBUG
  158. static long NextUniqueId()
  159. {
  160. return Interlocked.Increment(ref Opcode.nextUniqueId);
  161. }
  162. #endif
  163. internal virtual void Add(Opcode op)
  164. {
  165. Fx.Assert(null != op, "");
  166. Fx.Assert(null != this.prev, "");
  167. // Create a branch that will include both this and the new opcode
  168. this.prev.AddBranch(op);
  169. }
  170. internal virtual void AddBranch(Opcode opcode)
  171. {
  172. Fx.Assert(null != opcode, "");
  173. // Replace what follows this opcode with a branch containing .next and the new opcode
  174. // If this opcode is a conditional, then since the tree structure is about to change, conditional
  175. // reachability for everything that follows is about to change.
  176. // 1. Remove .next from the conditional's AlwaysBranch Table.
  177. // 2. Create the new branch structure.
  178. // 3. The branch, once in the tree, will fix up all conditional jumps
  179. Opcode next = this.next;
  180. if (this.TestFlag(OpcodeFlags.InConditional))
  181. {
  182. this.DelinkFromConditional(next);
  183. }
  184. BranchOpcode branch = new BranchOpcode();
  185. this.next = null;
  186. this.Attach(branch);
  187. if (null != next)
  188. {
  189. Fx.Assert(OpcodeID.Branch != next.ID, "");
  190. branch.Add(next);
  191. }
  192. branch.Add(opcode);
  193. }
  194. internal void Attach(Opcode op)
  195. {
  196. Fx.Assert(null == this.next, "");
  197. this.next = op;
  198. op.prev = this;
  199. }
  200. internal virtual void CollectXPathFilters(ICollection<MessageFilter> filters)
  201. {
  202. if (this.next != null)
  203. {
  204. this.next.CollectXPathFilters(filters);
  205. }
  206. }
  207. internal virtual bool IsEquivalentForAdd(Opcode opcode)
  208. {
  209. return (this.ID == opcode.ID);
  210. }
  211. internal bool IsMultipleResult()
  212. {
  213. return ((this.flags & (OpcodeFlags.Result | OpcodeFlags.Multiple)) ==
  214. (OpcodeFlags.Result | OpcodeFlags.Multiple));
  215. }
  216. internal virtual void DelinkFromConditional(Opcode child)
  217. {
  218. Fx.Assert(this.TestFlag(OpcodeFlags.InConditional), "");
  219. if (this.TestFlag(OpcodeFlags.InConditional))
  220. {
  221. ((QueryConditionalBranchOpcode)this.prev).RemoveAlwaysBranch(child);
  222. }
  223. }
  224. internal Opcode DetachChild()
  225. {
  226. Opcode child = this.next;
  227. if (child != null)
  228. {
  229. if (this.IsInConditional())
  230. {
  231. this.DelinkFromConditional(child);
  232. }
  233. }
  234. this.next = null;
  235. child.prev = null;
  236. return child;
  237. }
  238. internal void DetachFromParent()
  239. {
  240. Opcode parent = this.prev;
  241. if (parent != null)
  242. {
  243. parent.DetachChild();
  244. }
  245. }
  246. internal virtual bool Equals(Opcode op)
  247. {
  248. return (op.ID == this.ID);
  249. }
  250. internal virtual Opcode Eval(ProcessingContext context)
  251. {
  252. return this.next;
  253. }
  254. internal virtual Opcode Eval(NodeSequence sequence, SeekableXPathNavigator node)
  255. {
  256. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new QueryProcessingException(QueryProcessingError.Unexpected));
  257. }
  258. internal virtual Opcode EvalSpecial(ProcessingContext context)
  259. {
  260. return this.Eval(context);
  261. }
  262. internal virtual bool IsInConditional()
  263. {
  264. return this.TestFlag(OpcodeFlags.InConditional);
  265. }
  266. internal bool IsReachableFromConditional()
  267. {
  268. return (null != this.prev && this.prev.IsInConditional());
  269. }
  270. internal virtual Opcode Locate(Opcode opcode)
  271. {
  272. Fx.Assert(null != opcode, "");
  273. if (null != this.next && this.next.Equals(opcode))
  274. {
  275. return this.next;
  276. }
  277. return null;
  278. }
  279. internal virtual void LinkToConditional(Opcode child)
  280. {
  281. Fx.Assert(this.TestFlag(OpcodeFlags.InConditional), "");
  282. if (this.TestFlag(OpcodeFlags.InConditional))
  283. {
  284. ((QueryConditionalBranchOpcode)this.prev).AddAlwaysBranch(this, child);
  285. }
  286. }
  287. internal virtual void Remove()
  288. {
  289. if (null == this.next)
  290. {
  291. Opcode prevOpcode = this.prev;
  292. if (null != prevOpcode)
  293. {
  294. prevOpcode.RemoveChild(this);
  295. prevOpcode.Remove();
  296. }
  297. }
  298. }
  299. internal virtual void RemoveChild(Opcode opcode)
  300. {
  301. Fx.Assert(null != opcode, "");
  302. Fx.Assert(this.next == opcode, "");
  303. if (this.IsInConditional())
  304. {
  305. this.DelinkFromConditional(opcode);
  306. }
  307. opcode.prev = null;
  308. this.next = null;
  309. opcode.Flags |= OpcodeFlags.Deleted;
  310. }
  311. internal virtual void Replace(Opcode replace, Opcode with)
  312. {
  313. Fx.Assert(null != replace && null != with, "");
  314. if (this.next == replace)
  315. {
  316. bool isConditional = this.IsInConditional();
  317. if (isConditional)
  318. {
  319. this.DelinkFromConditional(this.next);
  320. }
  321. this.next.prev = null;
  322. this.next = with;
  323. with.prev = this;
  324. if (isConditional)
  325. {
  326. this.LinkToConditional(with);
  327. }
  328. }
  329. }
  330. internal bool TestFlag(OpcodeFlags flag)
  331. {
  332. return (0 != (this.flags & flag));
  333. }
  334. #if DEBUG_FILTER
  335. public override string ToString()
  336. {
  337. #if DEBUG
  338. return string.Format("{0}(#{1})", this.opcodeID.ToString(), this.uniqueID);
  339. #else
  340. return this.opcodeID.ToString();
  341. #endif
  342. }
  343. #endif
  344. internal virtual void Trim()
  345. {
  346. if (this.next != null)
  347. {
  348. this.next.Trim();
  349. }
  350. }
  351. }
  352. struct OpcodeBlock
  353. {
  354. Opcode first;
  355. Opcode last;
  356. internal OpcodeBlock(Opcode first)
  357. {
  358. this.first = first;
  359. this.first.Prev = null;
  360. for (this.last = this.first; this.last.Next != null; this.last = this.last.Next);
  361. }
  362. #if FILTEROPTIMIZER
  363. internal OpcodeBlock(Opcode first, Opcode last)
  364. {
  365. this.first = first;
  366. this.first.Prev = null;
  367. this.last = last;
  368. this.Last.Next = null;
  369. }
  370. #endif
  371. internal Opcode First
  372. {
  373. get
  374. {
  375. return this.first;
  376. }
  377. }
  378. internal Opcode Last
  379. {
  380. get
  381. {
  382. return this.last;
  383. }
  384. }
  385. internal void Append(Opcode opcode)
  386. {
  387. Fx.Assert(null != opcode, "");
  388. if (null == this.last)
  389. {
  390. this.first = opcode;
  391. this.last = opcode;
  392. }
  393. else
  394. {
  395. this.last.Attach(opcode);
  396. opcode.Next = null;
  397. this.last = opcode;
  398. }
  399. }
  400. internal void Append(OpcodeBlock block)
  401. {
  402. if (null == this.last)
  403. {
  404. this.first = block.first;
  405. this.last = block.last;
  406. }
  407. else
  408. {
  409. this.last.Attach(block.first);
  410. this.last = block.last;
  411. }
  412. }
  413. internal void DetachLast()
  414. {
  415. if (null == this.last)
  416. {
  417. return;
  418. }
  419. Opcode newLast = this.last.Prev;
  420. this.last.Prev = null;
  421. this.last = newLast;
  422. if (null != this.last)
  423. {
  424. this.last.Next = null;
  425. }
  426. }
  427. }
  428. class OpcodeList
  429. {
  430. QueryBuffer<Opcode> opcodes;
  431. public OpcodeList(int capacity)
  432. {
  433. this.opcodes = new QueryBuffer<Opcode>(capacity);
  434. }
  435. public int Count
  436. {
  437. get
  438. {
  439. return this.opcodes.count;
  440. }
  441. }
  442. public Opcode this[int index]
  443. {
  444. get
  445. {
  446. return this.opcodes[index];
  447. }
  448. set
  449. {
  450. this.opcodes[index] = value;
  451. }
  452. }
  453. public void Add(Opcode opcode)
  454. {
  455. this.opcodes.Add(opcode);
  456. }
  457. public int IndexOf(Opcode opcode)
  458. {
  459. return this.opcodes.IndexOf(opcode);
  460. }
  461. public void Remove(Opcode opcode)
  462. {
  463. this.opcodes.Remove(opcode);
  464. }
  465. public void Trim()
  466. {
  467. this.opcodes.TrimToCount();
  468. }
  469. }
  470. }