OpCode.cs 13 KB

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