OpCode.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. //
  2. // OpCode.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. namespace Mono.Cecil.Cil {
  29. public enum FlowControl {
  30. Branch,
  31. Break,
  32. Call,
  33. Cond_Branch,
  34. Meta,
  35. Next,
  36. Phi,
  37. Return,
  38. Throw,
  39. }
  40. public enum OpCodeType {
  41. Annotation,
  42. Macro,
  43. Nternal,
  44. Objmodel,
  45. Prefix,
  46. Primitive,
  47. }
  48. public enum OperandType {
  49. InlineBrTarget,
  50. InlineField,
  51. InlineI,
  52. InlineI8,
  53. InlineMethod,
  54. InlineNone,
  55. InlinePhi,
  56. InlineR,
  57. InlineSig,
  58. InlineString,
  59. InlineSwitch,
  60. InlineTok,
  61. InlineType,
  62. InlineVar,
  63. InlineArg,
  64. ShortInlineBrTarget,
  65. ShortInlineI,
  66. ShortInlineR,
  67. ShortInlineVar,
  68. ShortInlineArg,
  69. }
  70. public enum StackBehaviour {
  71. Pop0,
  72. Pop1,
  73. Pop1_pop1,
  74. Popi,
  75. Popi_pop1,
  76. Popi_popi,
  77. Popi_popi8,
  78. Popi_popi_popi,
  79. Popi_popr4,
  80. Popi_popr8,
  81. Popref,
  82. Popref_pop1,
  83. Popref_popi,
  84. Popref_popi_popi,
  85. Popref_popi_popi8,
  86. Popref_popi_popr4,
  87. Popref_popi_popr8,
  88. Popref_popi_popref,
  89. PopAll,
  90. Push0,
  91. Push1,
  92. Push1_push1,
  93. Pushi,
  94. Pushi8,
  95. Pushr4,
  96. Pushr8,
  97. Pushref,
  98. Varpop,
  99. Varpush,
  100. }
  101. public struct OpCode {
  102. readonly byte op1;
  103. readonly byte op2;
  104. readonly byte code;
  105. readonly byte flow_control;
  106. readonly byte opcode_type;
  107. readonly byte operand_type;
  108. readonly byte stack_behavior_pop;
  109. readonly byte stack_behavior_push;
  110. public string Name {
  111. get { return OpCodeNames.names [(int) Code]; }
  112. }
  113. public int Size {
  114. get { return op1 == 0xff ? 1 : 2; }
  115. }
  116. public byte Op1 {
  117. get { return op1; }
  118. }
  119. public byte Op2 {
  120. get { return op2; }
  121. }
  122. public short Value {
  123. get { return op1 == 0xff ? op2 : (short) ((op1 << 8) | op2); }
  124. }
  125. public Code Code {
  126. get { return (Code) code; }
  127. }
  128. public FlowControl FlowControl {
  129. get { return (FlowControl) flow_control; }
  130. }
  131. public OpCodeType OpCodeType {
  132. get { return (OpCodeType) opcode_type; }
  133. }
  134. public OperandType OperandType {
  135. get { return (OperandType) operand_type; }
  136. }
  137. public StackBehaviour StackBehaviourPop {
  138. get { return (StackBehaviour) stack_behavior_pop; }
  139. }
  140. public StackBehaviour StackBehaviourPush {
  141. get { return (StackBehaviour) stack_behavior_push; }
  142. }
  143. internal OpCode (int x, int y)
  144. {
  145. this.op1 = (byte) ((x >> 0) & 0xff);
  146. this.op2 = (byte) ((x >> 8) & 0xff);
  147. this.code = (byte) ((x >> 16) & 0xff);
  148. this.flow_control = (byte) ((x >> 24) & 0xff);
  149. this.opcode_type = (byte) ((y >> 0) & 0xff);
  150. this.operand_type = (byte) ((y >> 8) & 0xff);
  151. this.stack_behavior_pop = (byte) ((y >> 16) & 0xff);
  152. this.stack_behavior_push = (byte) ((y >> 24) & 0xff);
  153. if (op1 == 0xff)
  154. OpCodes.OneByteOpCode [op2] = this;
  155. else
  156. OpCodes.TwoBytesOpCode [op2] = this;
  157. }
  158. public override int GetHashCode ()
  159. {
  160. return Value;
  161. }
  162. public override bool Equals (object obj)
  163. {
  164. if (!(obj is OpCode))
  165. return false;
  166. var opcode = (OpCode) obj;
  167. return op1 == opcode.op1 && op2 == opcode.op2;
  168. }
  169. public bool Equals (OpCode opcode)
  170. {
  171. return op1 == opcode.op1 && op2 == opcode.op2;
  172. }
  173. public static bool operator == (OpCode one, OpCode other)
  174. {
  175. return one.op1 == other.op1 && one.op2 == other.op2;
  176. }
  177. public static bool operator != (OpCode one, OpCode other)
  178. {
  179. return one.op1 != other.op1 || one.op2 != other.op2;
  180. }
  181. public override string ToString ()
  182. {
  183. return Name;
  184. }
  185. }
  186. static class OpCodeNames {
  187. internal static readonly string [] names;
  188. static OpCodeNames ()
  189. {
  190. var table = new byte [] {
  191. 3, 110, 111, 112,
  192. 5, 98, 114, 101, 97, 107,
  193. 7, 108, 100, 97, 114, 103, 46, 48,
  194. 7, 108, 100, 97, 114, 103, 46, 49,
  195. 7, 108, 100, 97, 114, 103, 46, 50,
  196. 7, 108, 100, 97, 114, 103, 46, 51,
  197. 7, 108, 100, 108, 111, 99, 46, 48,
  198. 7, 108, 100, 108, 111, 99, 46, 49,
  199. 7, 108, 100, 108, 111, 99, 46, 50,
  200. 7, 108, 100, 108, 111, 99, 46, 51,
  201. 7, 115, 116, 108, 111, 99, 46, 48,
  202. 7, 115, 116, 108, 111, 99, 46, 49,
  203. 7, 115, 116, 108, 111, 99, 46, 50,
  204. 7, 115, 116, 108, 111, 99, 46, 51,
  205. 7, 108, 100, 97, 114, 103, 46, 115,
  206. 8, 108, 100, 97, 114, 103, 97, 46, 115,
  207. 7, 115, 116, 97, 114, 103, 46, 115,
  208. 7, 108, 100, 108, 111, 99, 46, 115,
  209. 8, 108, 100, 108, 111, 99, 97, 46, 115,
  210. 7, 115, 116, 108, 111, 99, 46, 115,
  211. 6, 108, 100, 110, 117, 108, 108,
  212. 9, 108, 100, 99, 46, 105, 52, 46, 109, 49,
  213. 8, 108, 100, 99, 46, 105, 52, 46, 48,
  214. 8, 108, 100, 99, 46, 105, 52, 46, 49,
  215. 8, 108, 100, 99, 46, 105, 52, 46, 50,
  216. 8, 108, 100, 99, 46, 105, 52, 46, 51,
  217. 8, 108, 100, 99, 46, 105, 52, 46, 52,
  218. 8, 108, 100, 99, 46, 105, 52, 46, 53,
  219. 8, 108, 100, 99, 46, 105, 52, 46, 54,
  220. 8, 108, 100, 99, 46, 105, 52, 46, 55,
  221. 8, 108, 100, 99, 46, 105, 52, 46, 56,
  222. 8, 108, 100, 99, 46, 105, 52, 46, 115,
  223. 6, 108, 100, 99, 46, 105, 52,
  224. 6, 108, 100, 99, 46, 105, 56,
  225. 6, 108, 100, 99, 46, 114, 52,
  226. 6, 108, 100, 99, 46, 114, 56,
  227. 3, 100, 117, 112,
  228. 3, 112, 111, 112,
  229. 3, 106, 109, 112,
  230. 4, 99, 97, 108, 108,
  231. 5, 99, 97, 108, 108, 105,
  232. 3, 114, 101, 116,
  233. 4, 98, 114, 46, 115,
  234. 9, 98, 114, 102, 97, 108, 115, 101, 46, 115,
  235. 8, 98, 114, 116, 114, 117, 101, 46, 115,
  236. 5, 98, 101, 113, 46, 115,
  237. 5, 98, 103, 101, 46, 115,
  238. 5, 98, 103, 116, 46, 115,
  239. 5, 98, 108, 101, 46, 115,
  240. 5, 98, 108, 116, 46, 115,
  241. 8, 98, 110, 101, 46, 117, 110, 46, 115,
  242. 8, 98, 103, 101, 46, 117, 110, 46, 115,
  243. 8, 98, 103, 116, 46, 117, 110, 46, 115,
  244. 8, 98, 108, 101, 46, 117, 110, 46, 115,
  245. 8, 98, 108, 116, 46, 117, 110, 46, 115,
  246. 2, 98, 114,
  247. 7, 98, 114, 102, 97, 108, 115, 101,
  248. 6, 98, 114, 116, 114, 117, 101,
  249. 3, 98, 101, 113,
  250. 3, 98, 103, 101,
  251. 3, 98, 103, 116,
  252. 3, 98, 108, 101,
  253. 3, 98, 108, 116,
  254. 6, 98, 110, 101, 46, 117, 110,
  255. 6, 98, 103, 101, 46, 117, 110,
  256. 6, 98, 103, 116, 46, 117, 110,
  257. 6, 98, 108, 101, 46, 117, 110,
  258. 6, 98, 108, 116, 46, 117, 110,
  259. 6, 115, 119, 105, 116, 99, 104,
  260. 8, 108, 100, 105, 110, 100, 46, 105, 49,
  261. 8, 108, 100, 105, 110, 100, 46, 117, 49,
  262. 8, 108, 100, 105, 110, 100, 46, 105, 50,
  263. 8, 108, 100, 105, 110, 100, 46, 117, 50,
  264. 8, 108, 100, 105, 110, 100, 46, 105, 52,
  265. 8, 108, 100, 105, 110, 100, 46, 117, 52,
  266. 8, 108, 100, 105, 110, 100, 46, 105, 56,
  267. 7, 108, 100, 105, 110, 100, 46, 105,
  268. 8, 108, 100, 105, 110, 100, 46, 114, 52,
  269. 8, 108, 100, 105, 110, 100, 46, 114, 56,
  270. 9, 108, 100, 105, 110, 100, 46, 114, 101, 102,
  271. 9, 115, 116, 105, 110, 100, 46, 114, 101, 102,
  272. 8, 115, 116, 105, 110, 100, 46, 105, 49,
  273. 8, 115, 116, 105, 110, 100, 46, 105, 50,
  274. 8, 115, 116, 105, 110, 100, 46, 105, 52,
  275. 8, 115, 116, 105, 110, 100, 46, 105, 56,
  276. 8, 115, 116, 105, 110, 100, 46, 114, 52,
  277. 8, 115, 116, 105, 110, 100, 46, 114, 56,
  278. 3, 97, 100, 100,
  279. 3, 115, 117, 98,
  280. 3, 109, 117, 108,
  281. 3, 100, 105, 118,
  282. 6, 100, 105, 118, 46, 117, 110,
  283. 3, 114, 101, 109,
  284. 6, 114, 101, 109, 46, 117, 110,
  285. 3, 97, 110, 100,
  286. 2, 111, 114,
  287. 3, 120, 111, 114,
  288. 3, 115, 104, 108,
  289. 3, 115, 104, 114,
  290. 6, 115, 104, 114, 46, 117, 110,
  291. 3, 110, 101, 103,
  292. 3, 110, 111, 116,
  293. 7, 99, 111, 110, 118, 46, 105, 49,
  294. 7, 99, 111, 110, 118, 46, 105, 50,
  295. 7, 99, 111, 110, 118, 46, 105, 52,
  296. 7, 99, 111, 110, 118, 46, 105, 56,
  297. 7, 99, 111, 110, 118, 46, 114, 52,
  298. 7, 99, 111, 110, 118, 46, 114, 56,
  299. 7, 99, 111, 110, 118, 46, 117, 52,
  300. 7, 99, 111, 110, 118, 46, 117, 56,
  301. 8, 99, 97, 108, 108, 118, 105, 114, 116,
  302. 5, 99, 112, 111, 98, 106,
  303. 5, 108, 100, 111, 98, 106,
  304. 5, 108, 100, 115, 116, 114,
  305. 6, 110, 101, 119, 111, 98, 106,
  306. 9, 99, 97, 115, 116, 99, 108, 97, 115, 115,
  307. 6, 105, 115, 105, 110, 115, 116,
  308. 9, 99, 111, 110, 118, 46, 114, 46, 117, 110,
  309. 5, 117, 110, 98, 111, 120,
  310. 5, 116, 104, 114, 111, 119,
  311. 5, 108, 100, 102, 108, 100,
  312. 6, 108, 100, 102, 108, 100, 97,
  313. 5, 115, 116, 102, 108, 100,
  314. 6, 108, 100, 115, 102, 108, 100,
  315. 7, 108, 100, 115, 102, 108, 100, 97,
  316. 6, 115, 116, 115, 102, 108, 100,
  317. 5, 115, 116, 111, 98, 106,
  318. 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49, 46, 117, 110,
  319. 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50, 46, 117, 110,
  320. 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52, 46, 117, 110,
  321. 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56, 46, 117, 110,
  322. 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49, 46, 117, 110,
  323. 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50, 46, 117, 110,
  324. 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52, 46, 117, 110,
  325. 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56, 46, 117, 110,
  326. 13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 46, 117, 110,
  327. 13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 46, 117, 110,
  328. 3, 98, 111, 120,
  329. 6, 110, 101, 119, 97, 114, 114,
  330. 5, 108, 100, 108, 101, 110,
  331. 7, 108, 100, 101, 108, 101, 109, 97,
  332. 9, 108, 100, 101, 108, 101, 109, 46, 105, 49,
  333. 9, 108, 100, 101, 108, 101, 109, 46, 117, 49,
  334. 9, 108, 100, 101, 108, 101, 109, 46, 105, 50,
  335. 9, 108, 100, 101, 108, 101, 109, 46, 117, 50,
  336. 9, 108, 100, 101, 108, 101, 109, 46, 105, 52,
  337. 9, 108, 100, 101, 108, 101, 109, 46, 117, 52,
  338. 9, 108, 100, 101, 108, 101, 109, 46, 105, 56,
  339. 8, 108, 100, 101, 108, 101, 109, 46, 105,
  340. 9, 108, 100, 101, 108, 101, 109, 46, 114, 52,
  341. 9, 108, 100, 101, 108, 101, 109, 46, 114, 56,
  342. 10, 108, 100, 101, 108, 101, 109, 46, 114, 101, 102,
  343. 8, 115, 116, 101, 108, 101, 109, 46, 105,
  344. 9, 115, 116, 101, 108, 101, 109, 46, 105, 49,
  345. 9, 115, 116, 101, 108, 101, 109, 46, 105, 50,
  346. 9, 115, 116, 101, 108, 101, 109, 46, 105, 52,
  347. 9, 115, 116, 101, 108, 101, 109, 46, 105, 56,
  348. 9, 115, 116, 101, 108, 101, 109, 46, 114, 52,
  349. 9, 115, 116, 101, 108, 101, 109, 46, 114, 56,
  350. 10, 115, 116, 101, 108, 101, 109, 46, 114, 101, 102,
  351. 10, 108, 100, 101, 108, 101, 109, 46, 97, 110, 121,
  352. 10, 115, 116, 101, 108, 101, 109, 46, 97, 110, 121,
  353. 9, 117, 110, 98, 111, 120, 46, 97, 110, 121,
  354. 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49,
  355. 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49,
  356. 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50,
  357. 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50,
  358. 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52,
  359. 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52,
  360. 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56,
  361. 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56,
  362. 9, 114, 101, 102, 97, 110, 121, 118, 97, 108,
  363. 8, 99, 107, 102, 105, 110, 105, 116, 101,
  364. 8, 109, 107, 114, 101, 102, 97, 110, 121,
  365. 7, 108, 100, 116, 111, 107, 101, 110,
  366. 7, 99, 111, 110, 118, 46, 117, 50,
  367. 7, 99, 111, 110, 118, 46, 117, 49,
  368. 6, 99, 111, 110, 118, 46, 105,
  369. 10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105,
  370. 10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117,
  371. 7, 97, 100, 100, 46, 111, 118, 102,
  372. 10, 97, 100, 100, 46, 111, 118, 102, 46, 117, 110,
  373. 7, 109, 117, 108, 46, 111, 118, 102,
  374. 10, 109, 117, 108, 46, 111, 118, 102, 46, 117, 110,
  375. 7, 115, 117, 98, 46, 111, 118, 102,
  376. 10, 115, 117, 98, 46, 111, 118, 102, 46, 117, 110,
  377. 10, 101, 110, 100, 102, 105, 110, 97, 108, 108, 121,
  378. 5, 108, 101, 97, 118, 101,
  379. 7, 108, 101, 97, 118, 101, 46, 115,
  380. 7, 115, 116, 105, 110, 100, 46, 105,
  381. 6, 99, 111, 110, 118, 46, 117,
  382. 7, 97, 114, 103, 108, 105, 115, 116,
  383. 3, 99, 101, 113,
  384. 3, 99, 103, 116,
  385. 6, 99, 103, 116, 46, 117, 110,
  386. 3, 99, 108, 116,
  387. 6, 99, 108, 116, 46, 117, 110,
  388. 5, 108, 100, 102, 116, 110,
  389. 9, 108, 100, 118, 105, 114, 116, 102, 116, 110,
  390. 5, 108, 100, 97, 114, 103,
  391. 6, 108, 100, 97, 114, 103, 97,
  392. 5, 115, 116, 97, 114, 103,
  393. 5, 108, 100, 108, 111, 99,
  394. 6, 108, 100, 108, 111, 99, 97,
  395. 5, 115, 116, 108, 111, 99,
  396. 8, 108, 111, 99, 97, 108, 108, 111, 99,
  397. 9, 101, 110, 100, 102, 105, 108, 116, 101, 114,
  398. 10, 117, 110, 97, 108, 105, 103, 110, 101, 100, 46,
  399. 9, 118, 111, 108, 97, 116, 105, 108, 101, 46,
  400. 5, 116, 97, 105, 108, 46,
  401. 7, 105, 110, 105, 116, 111, 98, 106,
  402. 12, 99, 111, 110, 115, 116, 114, 97, 105, 110, 101, 100, 46,
  403. 5, 99, 112, 98, 108, 107,
  404. 7, 105, 110, 105, 116, 98, 108, 107,
  405. 3, 110, 111, 46,
  406. 7, 114, 101, 116, 104, 114, 111, 119,
  407. 6, 115, 105, 122, 101, 111, 102,
  408. 10, 114, 101, 102, 97, 110, 121, 116, 121, 112, 101,
  409. 9, 114, 101, 97, 100, 111, 110, 108, 121, 46,
  410. };
  411. names = new string [219];
  412. for (int i = 0, p = 0; i < names.Length; i++) {
  413. var buffer = new char [table [p++]];
  414. for (int j = 0; j < buffer.Length; j++)
  415. buffer [j] = (char) table [p++];
  416. names [i] = new string (buffer);
  417. }
  418. }
  419. }
  420. }