OpCode.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.Reflection.Emit.OpCode
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  27. //
  28. using System;
  29. using System.Reflection;
  30. using System.Reflection.Emit;
  31. using System.Runtime.InteropServices;
  32. namespace System.Reflection.Emit {
  33. [ComVisible (true)]
  34. public struct OpCode {
  35. internal byte op1, op2;
  36. byte push, pop, size, type, args, flow;
  37. //
  38. // The order is:
  39. // Op1, Op2, StackBehaviourPush, StackBehaviourPop
  40. // Size, OpCodeType, OperandType, FlowControl
  41. //
  42. internal OpCode (int p, int q)
  43. {
  44. op1 = (byte) ((p >> 0) & 0xFF);
  45. op2 = (byte) ((p >> 8) & 0xFF);
  46. push = (byte) ((p >> 16) & 0xFF);
  47. pop = (byte) ((p >> 24) & 0xFF);
  48. size = (byte) ((q >> 0) & 0xFF);
  49. type = (byte) ((q >> 8) & 0xFF);
  50. args = (byte) ((q >> 16) & 0xFF);
  51. flow = (byte) ((q >> 24) & 0xFF);
  52. }
  53. public override int GetHashCode ()
  54. {
  55. return Name.GetHashCode ();
  56. }
  57. public override bool Equals (Object obj)
  58. {
  59. if (obj == null || !(obj is OpCode))
  60. return false;
  61. OpCode v = (OpCode) obj;
  62. return v.op1 == op1 && v.op2 == op2;
  63. }
  64. public bool Equals (OpCode obj)
  65. {
  66. return obj.op1 == op1 && obj.op2 == op2;
  67. }
  68. public override string ToString ()
  69. {
  70. return Name;
  71. }
  72. public string Name {
  73. get {
  74. if (op1 == 0xFF)
  75. return OpCodeNames.names [op2];
  76. return OpCodeNames.names [256 + op2];
  77. }
  78. }
  79. public int Size {
  80. get {
  81. return (int) size;
  82. }
  83. }
  84. public OpCodeType OpCodeType {
  85. get {
  86. return (OpCodeType) type;
  87. }
  88. }
  89. public OperandType OperandType {
  90. get {
  91. return (OperandType) args;
  92. }
  93. }
  94. public FlowControl FlowControl {
  95. get {
  96. return (FlowControl) flow;
  97. }
  98. }
  99. public StackBehaviour StackBehaviourPop {
  100. get {
  101. return (StackBehaviour) pop;
  102. }
  103. }
  104. public StackBehaviour StackBehaviourPush {
  105. get {
  106. return (StackBehaviour) push;
  107. }
  108. }
  109. public short Value {
  110. get {
  111. if (size == 1) {
  112. return op2;
  113. } else {
  114. // two byte instruction - combine
  115. // Some old MS betas returned (op1 << 2) | op2 here...
  116. return (short) ((op1 << 8) | op2);
  117. }
  118. }
  119. }
  120. public static bool operator == (OpCode a, OpCode b)
  121. {
  122. return a.op1 == b.op1 && a.op2 == b.op2;
  123. }
  124. public static bool operator != (OpCode a, OpCode b)
  125. {
  126. return a.op1 != b.op1 || a.op2 != b.op2;
  127. }
  128. }
  129. }