OpCode.cs 3.0 KB

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