OpCode.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.Runtime.InteropServices;
  29. namespace System.Reflection.Emit {
  30. [ComVisible (true)]
  31. public struct OpCode {
  32. internal byte op1, op2;
  33. byte push, pop, size, type, args, flow;
  34. //
  35. // The order is:
  36. // Op1, Op2, StackBehaviourPush, StackBehaviourPop
  37. // Size, OpCodeType, OperandType, FlowControl
  38. //
  39. internal OpCode (int p, int q)
  40. {
  41. op1 = (byte) ((p >> 0) & 0xFF);
  42. op2 = (byte) ((p >> 8) & 0xFF);
  43. push = (byte) ((p >> 16) & 0xFF);
  44. pop = (byte) ((p >> 24) & 0xFF);
  45. size = (byte) ((q >> 0) & 0xFF);
  46. type = (byte) ((q >> 8) & 0xFF);
  47. args = (byte) ((q >> 16) & 0xFF);
  48. flow = (byte) ((q >> 24) & 0xFF);
  49. }
  50. public override int GetHashCode ()
  51. {
  52. return Name.GetHashCode ();
  53. }
  54. public override bool Equals (Object obj)
  55. {
  56. if (obj == null || !(obj is OpCode))
  57. return false;
  58. OpCode v = (OpCode) obj;
  59. return v.op1 == op1 && v.op2 == op2;
  60. }
  61. public bool Equals (OpCode obj)
  62. {
  63. return obj.op1 == op1 && obj.op2 == op2;
  64. }
  65. public override string ToString ()
  66. {
  67. return Name;
  68. }
  69. public string Name {
  70. get {
  71. if (op1 == 0xFF)
  72. return OpCodeNames.names [op2];
  73. return OpCodeNames.names [256 + op2];
  74. }
  75. }
  76. public int Size {
  77. get {
  78. return (int) size;
  79. }
  80. }
  81. public OpCodeType OpCodeType {
  82. get {
  83. return (OpCodeType) type;
  84. }
  85. }
  86. public OperandType OperandType {
  87. get {
  88. return (OperandType) args;
  89. }
  90. }
  91. public FlowControl FlowControl {
  92. get {
  93. return (FlowControl) flow;
  94. }
  95. }
  96. public StackBehaviour StackBehaviourPop {
  97. get {
  98. return (StackBehaviour) pop;
  99. }
  100. }
  101. public StackBehaviour StackBehaviourPush {
  102. get {
  103. return (StackBehaviour) push;
  104. }
  105. }
  106. public short Value {
  107. get {
  108. if (size == 1) {
  109. return op2;
  110. } else {
  111. // two byte instruction - combine
  112. // Some old MS betas returned (op1 << 2) | op2 here...
  113. return (short) ((op1 << 8) | op2);
  114. }
  115. }
  116. }
  117. public static bool operator == (OpCode a, OpCode b)
  118. {
  119. return a.op1 == b.op1 && a.op2 == b.op2;
  120. }
  121. public static bool operator != (OpCode a, OpCode b)
  122. {
  123. return a.op1 != b.op1 || a.op2 != b.op2;
  124. }
  125. }
  126. }