Context.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. using Mono.Cecil.Cil;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace CLRSharp
  5. {
  6. /// <summary>
  7. /// 线程上下文
  8. /// 一个线程上下文表示一次调用,直到结束
  9. /// </summary>
  10. public class ThreadContext
  11. {
  12. [ThreadStatic]
  13. static ThreadContext _activeContext = null;
  14. public static ThreadContext activeContext
  15. {
  16. get
  17. {
  18. return _activeContext;
  19. }
  20. }
  21. public ICLRSharp_Environment environment
  22. {
  23. get;
  24. private set;
  25. }
  26. public int DebugLevel
  27. {
  28. get;
  29. private set;
  30. }
  31. public ThreadContext(ICLRSharp_Environment env)
  32. {
  33. this.environment = env;
  34. DebugLevel = 0;
  35. }
  36. public ThreadContext(ICLRSharp_Environment env, int DebugLevel)
  37. {
  38. this.environment = env;
  39. this.DebugLevel = DebugLevel;
  40. }
  41. public Stack<StackFrame> GetStackFrames()
  42. {
  43. return stacks;
  44. }
  45. Stack<StackFrame> stacks = new Stack<StackFrame>();
  46. public bool SetNoTry = false;
  47. public string Dump()
  48. {
  49. string str = "";
  50. foreach (StackFrame s in GetStackFrames())
  51. {
  52. var pos = s._pos;
  53. Instruction sqIns = pos;
  54. while (sqIns != null && sqIns.SequencePoint == null)
  55. {
  56. sqIns = sqIns.Previous;
  57. }
  58. if (sqIns != null && sqIns.SequencePoint != null)
  59. {
  60. str += sqIns.SequencePoint.Document.Url + "(" + sqIns.SequencePoint.StartLine + ")\n";
  61. }
  62. else
  63. {
  64. str +="!no pdb info,no code filename(no line)!\n";
  65. }
  66. if (pos == null)
  67. {
  68. continue;
  69. }
  70. str += " IL " + pos.ToString() + "\n";
  71. if (s._params != null)
  72. {
  73. str += " ===Params(" + s._params.Length + ")===\n";
  74. for (int i = 0; i < s._params.Length; i++)
  75. {
  76. str += " param" + i.ToString("D04") + s._params[i] + "\n";
  77. }
  78. }
  79. str += " ===VarSlots(" + s.slotVar.Count + ")===\n";
  80. for (int i = 0; i < s.slotVar.Count; i++)
  81. {
  82. str += " var" + i.ToString("D04") + s.slotVar[i] + "\n";
  83. }
  84. }
  85. return str;
  86. }
  87. public object ExecuteFunc(IMethod_Sharp method, object _this, object[] _params)
  88. {
  89. _activeContext = this;
  90. if (this.DebugLevel >= 9)
  91. {
  92. environment.logger.Log("<Call>::" + method.DeclaringType.FullName + "::" + method.Name.ToString());
  93. }
  94. StackFrame stack = new StackFrame(method.Name, method.isStatic);
  95. stacks.Push(stack);
  96. object[] _withp = null;
  97. bool isctor = method.Name == ".ctor";
  98. if (isctor)
  99. {
  100. //CLRSharp_Instance pthis = new CLRSharp_Instance(GetType(func.ReturnType) as Type_Common_CLRSharp);
  101. //StackFrame.RefObj pthis = new StackFrame.RefObj(stack, 0, StackFrame.RefType.arg);
  102. _withp = new object[_params == null ? 1 : (_params.Length + 1)];
  103. if (_params != null)
  104. _params.CopyTo(_withp, 1);
  105. _withp[0] = _this;
  106. }
  107. else
  108. {
  109. if (!method.isStatic)
  110. {
  111. _withp = new object[(_params == null) ? 1 : (_params.Length + 1)];
  112. _withp[0] = _this;
  113. if (_params != null)
  114. _params.CopyTo(_withp, 1);
  115. }
  116. else
  117. {
  118. _withp = _params;
  119. }
  120. }
  121. stack.SetParams(_withp);
  122. if (method.body != null)
  123. {
  124. stack.Init(method.body);
  125. stack._pos = method.body.bodyNative.Instructions[0];
  126. if (method.body.bodyNative.HasExceptionHandlers && !SetNoTry)
  127. {
  128. RunCodeWithTry(method.body, stack);
  129. }
  130. else
  131. {
  132. RunCode(stack, method.body);
  133. }
  134. }
  135. if (this.DebugLevel >= 9)
  136. {
  137. environment.logger.Log("<CallEnd>");
  138. }
  139. var ret = stacks.Pop().Return();
  140. return isctor ? _this : ret;
  141. //if (func.HasBody)
  142. //{
  143. // RunCode(stack, func.Body.Instructions);
  144. //}
  145. //var ret = stacks.Pop().Return();
  146. //if (this.DebugLevel >= 9)
  147. //{
  148. // environment.logger.Log("<CallEnd>");
  149. //}
  150. //return ret;
  151. }
  152. private void RunCodeWithTry(CodeBody body, StackFrame stack)
  153. {
  154. try
  155. {
  156. RunCode(stack, body);
  157. }
  158. catch (Exception err)
  159. {
  160. bool bEH = false;
  161. if (body.bodyNative.HasExceptionHandlers)
  162. {
  163. bEH = JumpToErr(body, stack, err);
  164. }
  165. if (!bEH)
  166. {
  167. throw err;
  168. }
  169. }
  170. }
  171. ICLRType GetType(string fullname)
  172. {
  173. var type = environment.GetType(fullname);
  174. ICLRType_Sharp stype = type as ICLRType_Sharp;
  175. if (stype != null && stype.NeedCCtor)
  176. {
  177. //执行.cctor
  178. stype.InvokeCCtor(this);
  179. }
  180. return type;
  181. }
  182. ICLRType GetType(object token)
  183. {
  184. token.GetHashCode();
  185. Mono.Cecil.ModuleDefinition module = null;
  186. string typename = null;
  187. if (token is Mono.Cecil.TypeDefinition)
  188. {
  189. Mono.Cecil.TypeDefinition _def = (token as Mono.Cecil.TypeDefinition);
  190. module = _def.Module;
  191. typename = _def.FullName;
  192. }
  193. else if (token is Mono.Cecil.TypeReference)
  194. {
  195. Mono.Cecil.TypeReference _ref = (token as Mono.Cecil.TypeReference);
  196. module = _ref.Module;
  197. typename = _ref.FullName;
  198. }
  199. else
  200. {
  201. throw new NotImplementedException();
  202. }
  203. return GetType(typename);
  204. }
  205. Dictionary<int, IMethod> methodCache = new Dictionary<int, IMethod>();
  206. Dictionary<int, IField> fieldCache = new Dictionary<int, IField>();
  207. IMethod GetMethod(object token)
  208. {
  209. IMethod __method = null;
  210. if (methodCache.TryGetValue(token.GetHashCode(), out __method))
  211. {
  212. return __method;
  213. }
  214. Mono.Cecil.ModuleDefinition module = null;
  215. string methodname = null;
  216. string typename = null;
  217. MethodParamList genlist = null;
  218. MethodParamList list = null;
  219. if (token is Mono.Cecil.MethodReference)
  220. {
  221. Mono.Cecil.MethodReference _ref = (token as Mono.Cecil.MethodReference);
  222. module = _ref.Module;
  223. methodname = _ref.Name;
  224. typename = _ref.DeclaringType.FullName;
  225. list = new MethodParamList(environment, _ref);
  226. if (_ref.IsGenericInstance)
  227. {
  228. Mono.Cecil.GenericInstanceMethod gmethod = _ref as Mono.Cecil.GenericInstanceMethod;
  229. genlist = new MethodParamList(environment, gmethod);
  230. }
  231. }
  232. else if (token is Mono.Cecil.MethodDefinition)
  233. {
  234. Mono.Cecil.MethodDefinition _def = token as Mono.Cecil.MethodDefinition;
  235. module = _def.Module;
  236. methodname = _def.Name;
  237. typename = _def.DeclaringType.FullName;
  238. list = new MethodParamList(environment, _def);
  239. if (_def.IsGenericInstance)
  240. {
  241. throw new NotImplementedException();
  242. //Mono.Cecil.GenericInstanceMethod gmethod = _def as Mono.Cecil.GenericInstanceMethod;
  243. //genlist = new MethodParamList(environment, gmethod);
  244. }
  245. }
  246. else
  247. {
  248. throw new NotImplementedException();
  249. }
  250. var typesys = GetType(typename);
  251. if (typesys == null)
  252. throw new Exception("type can't find:" + typename);
  253. IMethod _method = null;
  254. if (genlist != null)
  255. {
  256. _method = typesys.GetMethodT(methodname, genlist, list);
  257. }
  258. else
  259. {
  260. _method = typesys.GetMethod(methodname, list);
  261. }
  262. methodCache[token.GetHashCode()] = _method;
  263. return _method;
  264. }
  265. IMethod GetNewForArray(object token)
  266. {
  267. IMethod __method = null;
  268. if (methodCache.TryGetValue(token.GetHashCode(), out __method))
  269. {
  270. return __method;
  271. }
  272. Mono.Cecil.ModuleDefinition module = null;
  273. string typename = null;
  274. if (token is Mono.Cecil.TypeDefinition)
  275. {
  276. Mono.Cecil.TypeDefinition _def = (token as Mono.Cecil.TypeDefinition);
  277. module = _def.Module;
  278. typename = _def.FullName;
  279. }
  280. else if (token is Mono.Cecil.TypeReference)
  281. {
  282. Mono.Cecil.TypeReference _ref = (token as Mono.Cecil.TypeReference);
  283. module = _ref.Module;
  284. typename = _ref.FullName;
  285. }
  286. else
  287. {
  288. throw new NotImplementedException();
  289. }
  290. ICLRType _Itype = GetType(typename);
  291. typename += "[]";
  292. //var _type = context.environment.GetType(typename, type.Module);
  293. var _type = GetType(typename);
  294. MethodParamList tlist = MethodParamList.const_OneParam_Int(environment);
  295. var m = _type.GetMethod(".ctor", tlist);
  296. methodCache[token.GetHashCode()] = m;
  297. return m;
  298. }
  299. IField GetField(object token)
  300. {
  301. IField __field = null;
  302. if (fieldCache.TryGetValue(token.GetHashCode(), out __field))
  303. {
  304. return __field;
  305. }
  306. if (token is Mono.Cecil.FieldDefinition)
  307. {
  308. Mono.Cecil.FieldDefinition field = token as Mono.Cecil.FieldDefinition;
  309. var type = GetType(field.DeclaringType.FullName);
  310. __field = type.GetField(field.Name);
  311. }
  312. else if (token is Mono.Cecil.FieldReference)
  313. {
  314. Mono.Cecil.FieldReference field = token as Mono.Cecil.FieldReference;
  315. var type = GetType(field.DeclaringType.FullName);
  316. __field = type.GetField(field.Name);
  317. }
  318. //else if(token is CLRSharp_Instance)
  319. // {
  320. //CLRSharp_Instance inst = token as CLRSharp_Instance;
  321. //return inst.Fields[field.Name];
  322. // }
  323. else
  324. {
  325. throw new NotImplementedException("不可处理的token" + token.GetType().ToString());
  326. }
  327. fieldCache[token.GetHashCode()] = __field;
  328. return __field;
  329. }
  330. object GetToken(object token)
  331. {
  332. if (token is Mono.Cecil.FieldDefinition || token is Mono.Cecil.FieldReference)
  333. {
  334. return GetField(token);
  335. }
  336. else if (token is Mono.Cecil.TypeDefinition || token is Mono.Cecil.TypeReference)
  337. {
  338. return GetType(token);
  339. }
  340. else
  341. {
  342. throw new NotImplementedException("不可处理的token" + token.GetType().ToString());
  343. }
  344. }
  345. int GetParamPos(object token)
  346. {
  347. if (token is byte)
  348. {
  349. return (byte)token;
  350. }
  351. else if (token is sbyte)
  352. {
  353. return (sbyte)token;
  354. }
  355. else if (token is int)
  356. {
  357. return (int)token;
  358. }
  359. else if (token is Mono.Cecil.ParameterReference)
  360. {
  361. int i = (token as Mono.Cecil.ParameterReference).Index;
  362. if (this.stacks.Peek().Name == ".ctor" || this.stacks.Peek().IsStatic == false)
  363. {
  364. i++;
  365. }
  366. return i;
  367. }
  368. else
  369. {
  370. throw new NotImplementedException();
  371. }
  372. }
  373. int GetBaseCount(Type _now, Type _base)
  374. {
  375. if (_now == _base)
  376. return 0;
  377. if (_now.IsSubclassOf(_base) == false)
  378. {
  379. return -1;
  380. }
  381. return GetBaseCount(_now.BaseType, _base) + 1;
  382. }
  383. bool JumpToErr(CodeBody body, StackFrame frame, Exception err)
  384. {
  385. var posnow = frame._pos;
  386. List<Mono.Cecil.Cil.ExceptionHandler> ehs = new List<ExceptionHandler>();
  387. Mono.Cecil.Cil.ExceptionHandler ehNear = null;
  388. int ehNearB = -1;
  389. foreach (var eh in body.bodyNative.ExceptionHandlers)
  390. {
  391. if (eh.HandlerType == ExceptionHandlerType.Catch)
  392. {
  393. Type ehtype = GetType(eh.CatchType).TypeForSystem;
  394. if (ehtype == err.GetType() || err.GetType().IsSubclassOf(ehtype))
  395. //if(GetType(eh.CatchType)== environment.GetType(err.GetType()))
  396. {
  397. if (eh.TryStart.Offset <= posnow.Offset && eh.TryEnd.Offset >= posnow.Offset)
  398. {
  399. if (ehNear == null)
  400. {
  401. ehNear = eh;//第一个
  402. ehNearB = GetBaseCount(ehtype, err.GetType());
  403. }
  404. else
  405. {
  406. if (eh.TryStart.Offset > ehNear.TryStart.Offset || eh.TryEnd.Offset < ehNear.TryEnd.Offset)//范围更小
  407. {
  408. ehNear = eh;
  409. ehNearB = GetBaseCount(ehtype, err.GetType());
  410. }
  411. else if (eh.TryStart.Offset == ehNear.TryStart.Offset || eh.TryEnd.Offset == ehNear.TryEnd.Offset)//范围相等
  412. {
  413. if (ehtype == err.GetType())//类型一致,没有比这个更牛的了
  414. {
  415. ehNear = eh;
  416. ehNearB = GetBaseCount(ehtype, err.GetType());
  417. }
  418. else if (GetType(ehNear.CatchType).TypeForSystem == err.GetType())//上次找到的就是第一,不用比了
  419. {
  420. continue;
  421. }
  422. else //比较上次找到的类型,和这次找到的类型的亲缘性;
  423. {
  424. int newehNearB = GetBaseCount(ehtype, err.GetType());
  425. if (newehNearB == -1) continue;
  426. if (newehNearB < ehNearB)
  427. {
  428. ehNear = eh;
  429. ehNearB = newehNearB;
  430. }
  431. }
  432. }
  433. }
  434. ehs.Add(eh);
  435. }
  436. }
  437. }
  438. }
  439. if (ehNear != null)
  440. {
  441. frame.Ldobj(this, err);
  442. frame._pos = ehNear.HandlerStart;
  443. RunCodeWithTry(body, frame);
  444. return true;
  445. }
  446. return false;
  447. }
  448. void RunCode(StackFrame stack, CodeBody body)
  449. {
  450. Mono.Collections.Generic.Collection<Mono.Cecil.Cil.Instruction> codes = body.bodyNative.Instructions;
  451. while (true)
  452. {
  453. var code = stack._pos;
  454. if (DebugLevel >= 9)
  455. {
  456. environment.logger.Log(code.ToString());
  457. }
  458. switch (code.OpCode.Code)
  459. {
  460. ///////////
  461. //流程控制
  462. case Code.Nop:
  463. stack.Nop();
  464. break;
  465. case Code.Ret:
  466. stack.Ret();
  467. return;
  468. case Code.Leave:
  469. stack.Leave(code.Operand as Mono.Cecil.Cil.Instruction);
  470. break;
  471. case Code.Leave_S:
  472. //stack.Ret();
  473. stack.Leave(code.Operand as Mono.Cecil.Cil.Instruction);
  474. break;
  475. //流程控制之goto
  476. case Code.Br:
  477. stack.Br(code.Operand as Mono.Cecil.Cil.Instruction);
  478. break;
  479. case Code.Br_S:
  480. stack.Br(code.Operand as Mono.Cecil.Cil.Instruction);
  481. break;
  482. case Code.Brtrue:
  483. stack.Brtrue(code.Operand as Mono.Cecil.Cil.Instruction);
  484. break;
  485. case Code.Brtrue_S:
  486. stack.Brtrue(code.Operand as Mono.Cecil.Cil.Instruction);
  487. break;
  488. case Code.Brfalse:
  489. stack.Brfalse(code.Operand as Mono.Cecil.Cil.Instruction);
  490. break;
  491. case Code.Brfalse_S:
  492. stack.Brfalse(code.Operand as Mono.Cecil.Cil.Instruction);
  493. break;
  494. //比较流程控制
  495. case Code.Beq:
  496. stack.Beq(code.Operand as Mono.Cecil.Cil.Instruction);
  497. break;
  498. case Code.Beq_S:
  499. stack.Beq(code.Operand as Mono.Cecil.Cil.Instruction);
  500. break;
  501. case Code.Bne_Un:
  502. stack.Bne_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  503. break;
  504. case Code.Bne_Un_S:
  505. stack.Bne_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  506. break;
  507. case Code.Bge:
  508. stack.Bge(code.Operand as Mono.Cecil.Cil.Instruction);
  509. break;
  510. case Code.Bge_S:
  511. stack.Bge(code.Operand as Mono.Cecil.Cil.Instruction);
  512. break;
  513. case Code.Bge_Un:
  514. stack.Bge_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  515. break;
  516. case Code.Bge_Un_S:
  517. stack.Bge_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  518. break;
  519. case Code.Bgt:
  520. stack.Bgt(code.Operand as Mono.Cecil.Cil.Instruction);
  521. break;
  522. case Code.Bgt_S:
  523. stack.Bgt(code.Operand as Mono.Cecil.Cil.Instruction);
  524. break;
  525. case Code.Bgt_Un:
  526. stack.Bgt_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  527. break;
  528. case Code.Bgt_Un_S:
  529. stack.Bge_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  530. break;
  531. case Code.Ble:
  532. stack.Ble(code.Operand as Mono.Cecil.Cil.Instruction);
  533. break;
  534. case Code.Ble_S:
  535. stack.Ble(code.Operand as Mono.Cecil.Cil.Instruction);
  536. break;
  537. case Code.Ble_Un:
  538. stack.Ble_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  539. break;
  540. case Code.Ble_Un_S:
  541. stack.Ble_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  542. break;
  543. case Code.Blt:
  544. stack.Blt(code.Operand as Mono.Cecil.Cil.Instruction);
  545. break;
  546. case Code.Blt_S:
  547. stack.Blt(code.Operand as Mono.Cecil.Cil.Instruction);
  548. break;
  549. case Code.Blt_Un:
  550. stack.Blt_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  551. break;
  552. case Code.Blt_Un_S:
  553. stack.Ble_Un(code.Operand as Mono.Cecil.Cil.Instruction);
  554. break;
  555. //逻辑计算
  556. case Code.Ceq:
  557. stack.Ceq();
  558. break;
  559. case Code.Cgt:
  560. stack.Cgt();
  561. break;
  562. case Code.Cgt_Un:
  563. stack.Cgt_Un();
  564. break;
  565. case Code.Clt:
  566. stack.Clt();
  567. break;
  568. case Code.Clt_Un:
  569. stack.Clt_Un();
  570. break;
  571. case Code.Ckfinite:
  572. stack.Ckfinite();
  573. break;
  574. //常量加载
  575. case Code.Ldc_I4:
  576. stack.Ldc_I4((int)Convert.ToDecimal(code.Operand));
  577. break;
  578. case Code.Ldc_I4_S:
  579. stack.Ldc_I4((int)Convert.ToDecimal(code.Operand));
  580. break;
  581. case Code.Ldc_I4_M1:
  582. stack.Ldc_I4(-1);
  583. break;
  584. case Code.Ldc_I4_0:
  585. stack.Ldc_I4(0);
  586. break;
  587. case Code.Ldc_I4_1:
  588. stack.Ldc_I4(1);
  589. break;
  590. case Code.Ldc_I4_2:
  591. stack.Ldc_I4(2);
  592. break;
  593. case Code.Ldc_I4_3:
  594. stack.Ldc_I4(3);
  595. break;
  596. case Code.Ldc_I4_4:
  597. stack.Ldc_I4(4);
  598. break;
  599. case Code.Ldc_I4_5:
  600. stack.Ldc_I4(5);
  601. break;
  602. case Code.Ldc_I4_6:
  603. stack.Ldc_I4(6);
  604. break;
  605. case Code.Ldc_I4_7:
  606. stack.Ldc_I4(7);
  607. break;
  608. case Code.Ldc_I4_8:
  609. stack.Ldc_I4(8);
  610. break;
  611. case Code.Ldc_I8:
  612. stack.Ldc_I8((Int64)(Convert.ToDecimal(code.Operand)));
  613. break;
  614. case Code.Ldc_R4:
  615. stack.Ldc_R4((float)(Convert.ToDecimal(code.Operand)));
  616. break;
  617. case Code.Ldc_R8:
  618. stack.Ldc_R8((double)(Convert.ToDecimal(code.Operand)));
  619. break;
  620. //定义为临时变量
  621. case Code.Stloc:
  622. stack.Stloc((int)code.Operand);
  623. break;
  624. case Code.Stloc_S:
  625. stack.Stloc(((VariableDefinition)code.Operand).Index);
  626. break;
  627. case Code.Stloc_0:
  628. stack.Stloc(0);
  629. break;
  630. case Code.Stloc_1:
  631. stack.Stloc(1);
  632. break;
  633. case Code.Stloc_2:
  634. stack.Stloc(2);
  635. break;
  636. case Code.Stloc_3:
  637. stack.Stloc(3);
  638. break;
  639. //从临时变量加载
  640. case Code.Ldloc:
  641. stack.Ldloc((int)code.Operand);
  642. break;
  643. case Code.Ldloc_S:
  644. stack.Ldloc(((VariableDefinition)code.Operand).Index);
  645. break;
  646. case Code.Ldloc_0:
  647. stack.Ldloc(0);
  648. break;
  649. case Code.Ldloc_1:
  650. stack.Ldloc(1);
  651. break;
  652. case Code.Ldloc_2:
  653. stack.Ldloc(2);
  654. break;
  655. case Code.Ldloc_3:
  656. stack.Ldloc(3);
  657. break;
  658. case Code.Ldloca:
  659. stack.Ldloca(((VariableDefinition)code.Operand).Index);
  660. break;
  661. case Code.Ldloca_S:
  662. stack.Ldloca(((VariableDefinition)code.Operand).Index);
  663. break;
  664. //加载字符串
  665. case Code.Ldstr:
  666. stack.Ldstr(code.Operand as string);
  667. break;
  668. //呼叫函数
  669. case Code.Call:
  670. stack.Call(this, GetMethod(code.Operand), false);
  671. break;
  672. case Code.Callvirt:
  673. stack.Call(this, GetMethod(code.Operand), true);
  674. break;
  675. //算术指令
  676. case Code.Add:
  677. stack.Add();
  678. break;
  679. case Code.Sub:
  680. stack.Sub();
  681. break;
  682. case Code.Mul:
  683. stack.Mul();
  684. break;
  685. case Code.Div:
  686. stack.Div();
  687. break;
  688. case Code.Div_Un:
  689. stack.Div_Un();
  690. break;
  691. case Code.Rem:
  692. stack.Rem();
  693. break;
  694. case Code.Rem_Un:
  695. stack.Rem_Un();
  696. break;
  697. case Code.Neg:
  698. stack.Neg();
  699. break;
  700. //装箱
  701. case Code.Box:
  702. stack.Box(GetType(code.Operand));
  703. break;
  704. case Code.Unbox:
  705. stack.Unbox();
  706. break;
  707. case Code.Unbox_Any:
  708. stack.Unbox_Any();
  709. break;
  710. //加载参数
  711. case Code.Ldarg:
  712. stack.Ldarg((int)code.Operand);
  713. break;
  714. case Code.Ldarg_S:
  715. stack.Ldarg(GetParamPos(code.Operand));
  716. break;
  717. case Code.Ldarg_0:
  718. stack.Ldarg(0);
  719. break;
  720. case Code.Ldarg_1:
  721. stack.Ldarg(1);
  722. break;
  723. case Code.Ldarg_2:
  724. stack.Ldarg(2);
  725. break;
  726. case Code.Ldarg_3:
  727. stack.Ldarg(3);
  728. break;
  729. //转换
  730. case Code.Conv_I1:
  731. stack.Conv_I1();
  732. break;
  733. case Code.Conv_U1:
  734. stack.Conv_U1();
  735. break;
  736. case Code.Conv_I2:
  737. stack.Conv_I2();
  738. break;
  739. case Code.Conv_U2:
  740. stack.Conv_U2();
  741. break;
  742. case Code.Conv_I4:
  743. stack.Conv_I4();
  744. break;
  745. case Code.Conv_U4:
  746. stack.Conv_U4();
  747. break;
  748. case Code.Conv_I8:
  749. stack.Conv_I8();
  750. break;
  751. case Code.Conv_U8:
  752. stack.Conv_U8();
  753. break;
  754. case Code.Conv_I:
  755. stack.Conv_I();
  756. break;
  757. case Code.Conv_U:
  758. stack.Conv_U();
  759. break;
  760. case Code.Conv_R4:
  761. stack.Conv_R4();
  762. break;
  763. case Code.Conv_R8:
  764. stack.Conv_R8();
  765. break;
  766. case Code.Conv_R_Un:
  767. stack.Conv_R_Un();
  768. break;
  769. case Code.Conv_Ovf_I1:
  770. stack.Conv_Ovf_I1();
  771. break;
  772. case Code.Conv_Ovf_U1:
  773. stack.Conv_Ovf_U1();
  774. break;
  775. case Code.Conv_Ovf_I2:
  776. stack.Conv_Ovf_I2();
  777. break;
  778. case Code.Conv_Ovf_U2:
  779. stack.Conv_Ovf_U2();
  780. break;
  781. case Code.Conv_Ovf_I4:
  782. stack.Conv_Ovf_I4();
  783. break;
  784. case Code.Conv_Ovf_U4:
  785. stack.Conv_Ovf_U4();
  786. break;
  787. case Code.Conv_Ovf_I8:
  788. stack.Conv_Ovf_I8();
  789. break;
  790. case Code.Conv_Ovf_U8:
  791. stack.Conv_Ovf_U8();
  792. break;
  793. case Code.Conv_Ovf_I:
  794. stack.Conv_Ovf_I();
  795. break;
  796. case Code.Conv_Ovf_U:
  797. stack.Conv_Ovf_U();
  798. break;
  799. case Code.Conv_Ovf_I1_Un:
  800. stack.Conv_Ovf_I1_Un();
  801. break;
  802. case Code.Conv_Ovf_U1_Un:
  803. stack.Conv_Ovf_U1_Un();
  804. break;
  805. case Code.Conv_Ovf_I2_Un:
  806. stack.Conv_Ovf_I2_Un();
  807. break;
  808. case Code.Conv_Ovf_U2_Un:
  809. stack.Conv_Ovf_U2_Un();
  810. break;
  811. case Code.Conv_Ovf_I4_Un:
  812. stack.Conv_Ovf_I4_Un();
  813. break;
  814. case Code.Conv_Ovf_U4_Un:
  815. stack.Conv_Ovf_U4_Un();
  816. break;
  817. case Code.Conv_Ovf_I8_Un:
  818. stack.Conv_Ovf_I8_Un();
  819. break;
  820. case Code.Conv_Ovf_U8_Un:
  821. stack.Conv_Ovf_U8_Un();
  822. break;
  823. case Code.Conv_Ovf_I_Un:
  824. stack.Conv_Ovf_I_Un();
  825. break;
  826. case Code.Conv_Ovf_U_Un:
  827. stack.Conv_Ovf_U_Un();
  828. break;
  829. //数组
  830. case Code.Newarr:
  831. stack.NewArr(this, GetNewForArray(code.Operand));
  832. break;
  833. case Code.Ldlen:
  834. stack.LdLen();
  835. break;
  836. case Code.Ldelema:
  837. stack.Ldelema(code.Operand);
  838. break;
  839. case Code.Ldelem_I1:
  840. stack.Ldelem_I1();
  841. break;
  842. case Code.Ldelem_U1:
  843. stack.Ldelem_U1();
  844. break;
  845. case Code.Ldelem_I2:
  846. stack.Ldelem_I2();
  847. break;
  848. case Code.Ldelem_U2:
  849. stack.Ldelem_U2();
  850. break;
  851. case Code.Ldelem_I4:
  852. stack.Ldelem_I4();
  853. break;
  854. case Code.Ldelem_U4:
  855. stack.Ldelem_U4();
  856. break;
  857. case Code.Ldelem_I8:
  858. stack.Ldelem_I8();
  859. break;
  860. case Code.Ldelem_I:
  861. stack.Ldelem_I();
  862. break;
  863. case Code.Ldelem_R4:
  864. stack.Ldelem_R4();
  865. break;
  866. case Code.Ldelem_R8:
  867. stack.Ldelem_R8();
  868. break;
  869. case Code.Ldelem_Ref:
  870. stack.Ldelem_Ref();
  871. break;
  872. case Code.Ldelem_Any:
  873. stack.Ldelem_Any(code.Operand);
  874. break;
  875. case Code.Stelem_I:
  876. stack.Stelem_I();
  877. break;
  878. case Code.Stelem_I1:
  879. stack.Stelem_I1();
  880. break;
  881. case Code.Stelem_I2:
  882. stack.Stelem_I2();
  883. break;
  884. case Code.Stelem_I4:
  885. stack.Stelem_I4();
  886. break;
  887. case Code.Stelem_I8:
  888. stack.Stelem_I8();
  889. break;
  890. case Code.Stelem_R4:
  891. stack.Stelem_R4();
  892. break;
  893. case Code.Stelem_R8:
  894. stack.Stelem_R8();
  895. break;
  896. case Code.Stelem_Ref:
  897. stack.Stelem_Ref();
  898. break;
  899. case Code.Stelem_Any:
  900. stack.Stelem_Any();
  901. break;
  902. case Code.Newobj:
  903. stack.NewObj(this, GetMethod(code.Operand));
  904. break;
  905. case Code.Dup:
  906. stack.Dup();
  907. break;
  908. case Code.Pop:
  909. stack.Pop();
  910. break;
  911. case Code.Ldfld:
  912. stack.Ldfld(this, GetField(code.Operand));
  913. break;
  914. case Code.Ldflda:
  915. stack.Ldflda(this, GetField(code.Operand));
  916. break;
  917. case Code.Ldsfld:
  918. stack.Ldsfld(this, GetField(code.Operand));
  919. break;
  920. case Code.Ldsflda:
  921. stack.Ldsflda(this, GetField(code.Operand));
  922. break;
  923. case Code.Stfld:
  924. stack.Stfld(this, GetField(code.Operand));
  925. break;
  926. case Code.Stsfld:
  927. stack.Stsfld(this, GetField(code.Operand));
  928. break;
  929. case Code.Constrained:
  930. stack.Constrained(this, GetType(code.Operand));
  931. break;
  932. case Code.Isinst:
  933. stack.Isinst(this, GetType(code.Operand));
  934. break;
  935. case Code.Ldtoken:
  936. stack.Ldtoken(this, GetToken(code.Operand));
  937. break;
  938. case Code.Ldftn:
  939. stack.Ldftn(this, GetMethod(code.Operand));
  940. break;
  941. case Code.Ldvirtftn:
  942. stack.Ldvirtftn(this, GetMethod(code.Operand));
  943. break;
  944. case Code.Ldarga:
  945. stack.Ldarga(this, code.Operand);
  946. break;
  947. case Code.Ldarga_S:
  948. stack.Ldarga(this, code.Operand);
  949. break;
  950. case Code.Calli:
  951. stack.Calli(this, code.Operand);
  952. break;
  953. ///下面是还没有处理的指令
  954. case Code.Break:
  955. stack.Break(this, code.Operand);
  956. break;
  957. case Code.Starg_S:
  958. stack.Starg_S(this, code.Operand);
  959. break;
  960. case Code.Ldnull:
  961. stack.Ldnull();
  962. break;
  963. case Code.Jmp:
  964. stack.Jmp(this, code.Operand);
  965. break;
  966. case Code.Switch:
  967. stack.Switch(this, code.Operand as Mono.Cecil.Cil.Instruction[]);
  968. break;
  969. case Code.Ldind_I1:
  970. stack.Ldind_I1(this, code.Operand);
  971. break;
  972. case Code.Ldind_U1:
  973. stack.Ldind_U1(this, code.Operand);
  974. break;
  975. case Code.Ldind_I2:
  976. stack.Ldind_I2(this, code.Operand);
  977. break;
  978. case Code.Ldind_U2:
  979. stack.Ldind_U2(this, code.Operand);
  980. break;
  981. case Code.Ldind_I4:
  982. stack.Ldind_I4(this, code.Operand);
  983. break;
  984. case Code.Ldind_U4:
  985. stack.Ldind_U4(this, code.Operand);
  986. break;
  987. case Code.Ldind_I8:
  988. stack.Ldind_I8(this, code.Operand);
  989. break;
  990. case Code.Ldind_I:
  991. stack.Ldind_I(this, code.Operand);
  992. break;
  993. case Code.Ldind_R4:
  994. stack.Ldind_R4(this, code.Operand);
  995. break;
  996. case Code.Ldind_R8:
  997. stack.Ldind_R8(this, code.Operand);
  998. break;
  999. case Code.Ldind_Ref:
  1000. stack.Ldind_Ref(this, code.Operand);
  1001. break;
  1002. case Code.Stind_Ref:
  1003. stack.Stind_Ref(this, code.Operand);
  1004. break;
  1005. case Code.Stind_I1:
  1006. stack.Stind_I1(this, code.Operand);
  1007. break;
  1008. case Code.Stind_I2:
  1009. stack.Stind_I2(this, code.Operand);
  1010. break;
  1011. case Code.Stind_I4:
  1012. stack.Stind_I4(this, code.Operand);
  1013. break;
  1014. case Code.Stind_I8:
  1015. stack.Stind_I8(this, code.Operand);
  1016. break;
  1017. case Code.Stind_R4:
  1018. stack.Stind_R4(this, code.Operand);
  1019. break;
  1020. case Code.Stind_R8:
  1021. stack.Stind_R8(this, code.Operand);
  1022. break;
  1023. case Code.And:
  1024. stack.And(this, code.Operand);
  1025. break;
  1026. case Code.Or:
  1027. stack.Or(this, code.Operand);
  1028. break;
  1029. case Code.Xor:
  1030. stack.Xor(this, code.Operand);
  1031. break;
  1032. case Code.Shl:
  1033. stack.Shl(this, code.Operand);
  1034. break;
  1035. case Code.Shr:
  1036. stack.Shr(this, code.Operand);
  1037. break;
  1038. case Code.Shr_Un:
  1039. stack.Shr_Un(this, code.Operand);
  1040. break;
  1041. case Code.Not:
  1042. stack.Not(this, code.Operand);
  1043. break;
  1044. case Code.Cpobj:
  1045. stack.Cpobj(this, code.Operand);
  1046. break;
  1047. case Code.Ldobj:
  1048. stack.Ldobj(this, code.Operand);
  1049. break;
  1050. case Code.Castclass:
  1051. stack.Castclass(this, code.Operand);
  1052. break;
  1053. case Code.Throw:
  1054. stack.Throw(this, code.Operand);
  1055. break;
  1056. case Code.Stobj:
  1057. stack.Stobj(this, code.Operand);
  1058. break;
  1059. case Code.Refanyval:
  1060. stack.Refanyval(this, code.Operand);
  1061. break;
  1062. case Code.Mkrefany:
  1063. stack.Mkrefany(this, code.Operand);
  1064. break;
  1065. case Code.Add_Ovf:
  1066. stack.Add_Ovf(this, code.Operand);
  1067. break;
  1068. case Code.Add_Ovf_Un:
  1069. stack.Add_Ovf_Un(this, code.Operand);
  1070. break;
  1071. case Code.Mul_Ovf:
  1072. stack.Mul_Ovf(this, code.Operand);
  1073. break;
  1074. case Code.Mul_Ovf_Un:
  1075. stack.Mul_Ovf_Un(this, code.Operand);
  1076. break;
  1077. case Code.Sub_Ovf:
  1078. stack.Sub_Ovf(this, code.Operand);
  1079. break;
  1080. case Code.Sub_Ovf_Un:
  1081. stack.Sub_Ovf_Un(this, code.Operand);
  1082. break;
  1083. case Code.Endfinally:
  1084. stack.Endfinally(this, code.Operand);
  1085. break;
  1086. case Code.Stind_I:
  1087. stack.Stind_I(this, code.Operand);
  1088. break;
  1089. case Code.Arglist:
  1090. stack.Arglist(this, code.Operand);
  1091. break;
  1092. case Code.Starg:
  1093. stack.Starg(this, code.Operand);
  1094. break;
  1095. case Code.Localloc:
  1096. stack.Localloc(this, code.Operand);
  1097. break;
  1098. case Code.Endfilter:
  1099. stack.Endfilter(this, code.Operand);
  1100. break;
  1101. case Code.Unaligned:
  1102. stack.Unaligned(this, code.Operand);
  1103. break;
  1104. case Code.Volatile:
  1105. stack.Volatile(this, code.Operand);
  1106. break;
  1107. case Code.Tail:
  1108. stack.Tail(this, code.Operand);
  1109. break;
  1110. case Code.Initobj:
  1111. stack.Initobj(this, this.GetType(code.Operand));
  1112. break;
  1113. case Code.Cpblk:
  1114. stack.Cpblk(this, code.Operand);
  1115. break;
  1116. case Code.Initblk:
  1117. stack.Initblk(this, code.Operand);
  1118. break;
  1119. case Code.No:
  1120. stack.No(this, code.Operand);
  1121. break;
  1122. case Code.Rethrow:
  1123. stack.Rethrow(this, code.Operand);
  1124. break;
  1125. case Code.Sizeof:
  1126. stack.Sizeof(this, code.Operand);
  1127. break;
  1128. case Code.Refanytype:
  1129. stack.Refanytype(this, code.Operand);
  1130. break;
  1131. case Code.Readonly:
  1132. stack.Readonly(this, code.Operand);
  1133. break;
  1134. default:
  1135. throw new Exception("未实现的OpCode:" + code.OpCode.Code);
  1136. }
  1137. }
  1138. }
  1139. }
  1140. }