OpCode.cs 3.2 KB

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