ILEmitterCtx.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.Instructions;
  3. using ChocolArm64.Memory;
  4. using ChocolArm64.State;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using System.Reflection.Emit;
  9. namespace ChocolArm64.Translation
  10. {
  11. class ILEmitterCtx
  12. {
  13. public MemoryManager Memory { get; }
  14. private TranslatorCache _cache;
  15. private TranslatorQueue _queue;
  16. private Dictionary<long, ILLabel> _labels;
  17. private long _subPosition;
  18. private int _opcIndex;
  19. private Block _currBlock;
  20. public Block CurrBlock => _currBlock;
  21. public OpCode64 CurrOp => _currBlock?.OpCodes[_opcIndex];
  22. public TranslationTier Tier { get; }
  23. public Aarch32Mode Mode { get; } = Aarch32Mode.User; //TODO
  24. public bool HasIndirectJump { get; set; }
  25. public bool HasSlowCall { get; set; }
  26. private Dictionary<Block, ILBlock> _visitedBlocks;
  27. private Queue<Block> _branchTargets;
  28. private List<ILBlock> _ilBlocks;
  29. private ILBlock _ilBlock;
  30. private OpCode64 _optOpLastCompare;
  31. private OpCode64 _optOpLastFlagSet;
  32. //This is the index of the temporary register, used to store temporary
  33. //values needed by some functions, since IL doesn't have a swap instruction.
  34. //You can use any value here as long it doesn't conflict with the indices
  35. //for the other registers. Any value >= 64 or < 0 will do.
  36. private const int ReservedLocalsCount = 64;
  37. private const int RorTmpIndex = ReservedLocalsCount + 0;
  38. private const int CmpOptTmp1Index = ReservedLocalsCount + 1;
  39. private const int CmpOptTmp2Index = ReservedLocalsCount + 2;
  40. private const int IntGpTmp1Index = ReservedLocalsCount + 3;
  41. private const int IntGpTmp2Index = ReservedLocalsCount + 4;
  42. private const int UserIntTempStart = ReservedLocalsCount + 5;
  43. //Vectors are part of another "set" of locals.
  44. private const int VecGpTmp1Index = ReservedLocalsCount + 0;
  45. private const int UserVecTempStart = ReservedLocalsCount + 1;
  46. private static int _userIntTempCount;
  47. private static int _userVecTempCount;
  48. public ILEmitterCtx(
  49. MemoryManager memory,
  50. TranslatorCache cache,
  51. TranslatorQueue queue,
  52. TranslationTier tier,
  53. Block graph)
  54. {
  55. Memory = memory ?? throw new ArgumentNullException(nameof(memory));
  56. _cache = cache ?? throw new ArgumentNullException(nameof(cache));
  57. _queue = queue ?? throw new ArgumentNullException(nameof(queue));
  58. _currBlock = graph ?? throw new ArgumentNullException(nameof(graph));
  59. Tier = tier;
  60. _labels = new Dictionary<long, ILLabel>();
  61. _visitedBlocks = new Dictionary<Block, ILBlock>();
  62. _visitedBlocks.Add(graph, new ILBlock());
  63. _branchTargets = new Queue<Block>();
  64. _ilBlocks = new List<ILBlock>();
  65. _subPosition = graph.Position;
  66. ResetBlockState();
  67. if (AdvanceOpCode())
  68. {
  69. EmitSynchronization();
  70. _ilBlock.Add(new ILOpCodeLoadState(_ilBlock, isSubEntry: true));
  71. }
  72. }
  73. public static int GetIntTempIndex()
  74. {
  75. return UserIntTempStart + _userIntTempCount++;
  76. }
  77. public static int GetVecTempIndex()
  78. {
  79. return UserVecTempStart + _userVecTempCount++;
  80. }
  81. public ILBlock[] GetILBlocks()
  82. {
  83. EmitAllOpCodes();
  84. return _ilBlocks.ToArray();
  85. }
  86. private void EmitAllOpCodes()
  87. {
  88. do
  89. {
  90. EmitOpCode();
  91. }
  92. while (AdvanceOpCode());
  93. }
  94. private void EmitOpCode()
  95. {
  96. if (_currBlock == null)
  97. {
  98. return;
  99. }
  100. int opcIndex = _opcIndex;
  101. if (opcIndex == 0)
  102. {
  103. MarkLabel(GetLabel(_currBlock.Position));
  104. }
  105. bool isLastOp = opcIndex == CurrBlock.OpCodes.Count - 1;
  106. if (isLastOp && CurrBlock.Branch != null &&
  107. (ulong)CurrBlock.Branch.Position <= (ulong)CurrBlock.Position)
  108. {
  109. EmitSynchronization();
  110. }
  111. //On AARCH32 mode, (almost) all instruction can be conditionally
  112. //executed, and the required condition is encoded on the opcode.
  113. //We handle that here, skipping the instruction if the condition
  114. //is not met. We can just ignore it when the condition is "Always",
  115. //because in this case the instruction is always going to be executed.
  116. //Condition "Never" is also ignored because this is a special encoding
  117. //used by some unconditional instructions.
  118. ILLabel lblSkip = null;
  119. if (CurrOp is OpCode32 op && op.Cond < Condition.Al)
  120. {
  121. lblSkip = new ILLabel();
  122. EmitCondBranch(lblSkip, GetInverseCond(op.Cond));
  123. }
  124. CurrOp.Emitter(this);
  125. if (lblSkip != null)
  126. {
  127. MarkLabel(lblSkip);
  128. //If this is the last op on the block, and there's no "next" block
  129. //after this one, then we have to return right now, with the address
  130. //of the next instruction to be executed (in the case that the condition
  131. //is false, and the branch was not taken, as all basic blocks should end with
  132. //some kind of branch).
  133. if (isLastOp && CurrBlock.Next == null)
  134. {
  135. EmitStoreState();
  136. EmitLdc_I8(CurrOp.Position + CurrOp.OpCodeSizeInBytes);
  137. Emit(OpCodes.Ret);
  138. }
  139. }
  140. _ilBlock.Add(new ILBarrier());
  141. }
  142. private static Condition GetInverseCond(Condition cond)
  143. {
  144. //Bit 0 of all conditions is basically a negation bit, so
  145. //inverting this bit has the effect of inverting the condition.
  146. return (Condition)((int)cond ^ 1);
  147. }
  148. private void EmitSynchronization()
  149. {
  150. EmitLdarg(TranslatedSub.StateArgIdx);
  151. EmitLdc_I4(_currBlock.OpCodes.Count);
  152. EmitPrivateCall(typeof(CpuThreadState), nameof(CpuThreadState.Synchronize));
  153. EmitLdc_I4(0);
  154. ILLabel lblContinue = new ILLabel();
  155. Emit(OpCodes.Bne_Un_S, lblContinue);
  156. EmitLdc_I8(0);
  157. Emit(OpCodes.Ret);
  158. MarkLabel(lblContinue);
  159. }
  160. private bool AdvanceOpCode()
  161. {
  162. if (_currBlock == null)
  163. {
  164. return false;
  165. }
  166. while (++_opcIndex >= _currBlock.OpCodes.Count)
  167. {
  168. if (!AdvanceBlock())
  169. {
  170. return false;
  171. }
  172. ResetBlockState();
  173. }
  174. return true;
  175. }
  176. private bool AdvanceBlock()
  177. {
  178. if (_currBlock.Branch != null)
  179. {
  180. if (_visitedBlocks.TryAdd(_currBlock.Branch, _ilBlock.Branch))
  181. {
  182. _branchTargets.Enqueue(_currBlock.Branch);
  183. }
  184. }
  185. if (_currBlock.Next != null)
  186. {
  187. if (_visitedBlocks.TryAdd(_currBlock.Next, _ilBlock.Next))
  188. {
  189. _currBlock = _currBlock.Next;
  190. return true;
  191. }
  192. else
  193. {
  194. Emit(OpCodes.Br, GetLabel(_currBlock.Next.Position));
  195. }
  196. }
  197. return _branchTargets.TryDequeue(out _currBlock);
  198. }
  199. private void ResetBlockState()
  200. {
  201. _ilBlock = _visitedBlocks[_currBlock];
  202. _ilBlocks.Add(_ilBlock);
  203. _ilBlock.Next = GetOrCreateILBlock(_currBlock.Next);
  204. _ilBlock.Branch = GetOrCreateILBlock(_currBlock.Branch);
  205. _opcIndex = -1;
  206. _optOpLastFlagSet = null;
  207. _optOpLastCompare = null;
  208. }
  209. private ILBlock GetOrCreateILBlock(Block block)
  210. {
  211. if (block == null)
  212. {
  213. return null;
  214. }
  215. if (_visitedBlocks.TryGetValue(block, out ILBlock ilBlock))
  216. {
  217. return ilBlock;
  218. }
  219. return new ILBlock();
  220. }
  221. public void TranslateAhead(long position, ExecutionMode mode = ExecutionMode.Aarch64)
  222. {
  223. if (_cache.TryGetSubroutine(position, out TranslatedSub sub) && sub.Tier != TranslationTier.Tier0)
  224. {
  225. return;
  226. }
  227. _queue.Enqueue(position, mode, TranslationTier.Tier1, isComplete: true);
  228. }
  229. public bool TryOptEmitSubroutineCall()
  230. {
  231. //Calls should always have a next block, unless
  232. //we're translating a single basic block.
  233. if (_currBlock.Next == null)
  234. {
  235. return false;
  236. }
  237. if (!(CurrOp is IOpCodeBImm op))
  238. {
  239. return false;
  240. }
  241. if (!_cache.TryGetSubroutine(op.Imm, out TranslatedSub sub))
  242. {
  243. return false;
  244. }
  245. //It's not worth to call a Tier0 method, because
  246. //it contains slow code, rather than the entire function.
  247. if (sub.Tier == TranslationTier.Tier0)
  248. {
  249. return false;
  250. }
  251. EmitStoreState(sub);
  252. for (int index = 0; index < TranslatedSub.FixedArgTypes.Length; index++)
  253. {
  254. EmitLdarg(index);
  255. }
  256. EmitCall(sub.Method);
  257. return true;
  258. }
  259. public void TryOptMarkCondWithoutCmp()
  260. {
  261. _optOpLastCompare = CurrOp;
  262. InstEmitAluHelper.EmitAluLoadOpers(this);
  263. Stloc(CmpOptTmp2Index, VarType.Int);
  264. Stloc(CmpOptTmp1Index, VarType.Int);
  265. }
  266. private Dictionary<Condition, OpCode> _branchOps = new Dictionary<Condition, OpCode>()
  267. {
  268. { Condition.Eq, OpCodes.Beq },
  269. { Condition.Ne, OpCodes.Bne_Un },
  270. { Condition.GeUn, OpCodes.Bge_Un },
  271. { Condition.LtUn, OpCodes.Blt_Un },
  272. { Condition.GtUn, OpCodes.Bgt_Un },
  273. { Condition.LeUn, OpCodes.Ble_Un },
  274. { Condition.Ge, OpCodes.Bge },
  275. { Condition.Lt, OpCodes.Blt },
  276. { Condition.Gt, OpCodes.Bgt },
  277. { Condition.Le, OpCodes.Ble }
  278. };
  279. public void EmitCondBranch(ILLabel target, Condition cond)
  280. {
  281. if (_optOpLastCompare != null &&
  282. _optOpLastCompare == _optOpLastFlagSet && _branchOps.ContainsKey(cond))
  283. {
  284. if (_optOpLastCompare.Emitter == InstEmit.Subs)
  285. {
  286. Ldloc(CmpOptTmp1Index, VarType.Int, _optOpLastCompare.RegisterSize);
  287. Ldloc(CmpOptTmp2Index, VarType.Int, _optOpLastCompare.RegisterSize);
  288. Emit(_branchOps[cond], target);
  289. return;
  290. }
  291. else if (_optOpLastCompare.Emitter == InstEmit.Adds && cond != Condition.GeUn
  292. && cond != Condition.LtUn
  293. && cond != Condition.GtUn
  294. && cond != Condition.LeUn)
  295. {
  296. //There are several limitations that needs to be taken into account for CMN comparisons:
  297. //* The unsigned comparisons are not valid, as they depend on the
  298. //carry flag value, and they will have different values for addition and
  299. //subtraction. For addition, it's carry, and for subtraction, it's borrow.
  300. //So, we need to make sure we're not doing a unsigned compare for the CMN case.
  301. //* We can only do the optimization for the immediate variants,
  302. //because when the second operand value is exactly INT_MIN, we can't
  303. //negate the value as theres no positive counterpart.
  304. //Such invalid values can't be encoded on the immediate encodings.
  305. if (_optOpLastCompare is IOpCodeAluImm64 op)
  306. {
  307. Ldloc(CmpOptTmp1Index, VarType.Int, _optOpLastCompare.RegisterSize);
  308. if (_optOpLastCompare.RegisterSize == RegisterSize.Int32)
  309. {
  310. EmitLdc_I4((int)-op.Imm);
  311. }
  312. else
  313. {
  314. EmitLdc_I8(-op.Imm);
  315. }
  316. Emit(_branchOps[cond], target);
  317. return;
  318. }
  319. }
  320. }
  321. OpCode ilOp;
  322. int intCond = (int)cond;
  323. if (intCond < 14)
  324. {
  325. int condTrue = intCond >> 1;
  326. switch (condTrue)
  327. {
  328. case 0: EmitLdflg((int)PState.ZBit); break;
  329. case 1: EmitLdflg((int)PState.CBit); break;
  330. case 2: EmitLdflg((int)PState.NBit); break;
  331. case 3: EmitLdflg((int)PState.VBit); break;
  332. case 4:
  333. EmitLdflg((int)PState.CBit);
  334. EmitLdflg((int)PState.ZBit);
  335. Emit(OpCodes.Not);
  336. Emit(OpCodes.And);
  337. break;
  338. case 5:
  339. case 6:
  340. EmitLdflg((int)PState.NBit);
  341. EmitLdflg((int)PState.VBit);
  342. Emit(OpCodes.Ceq);
  343. if (condTrue == 6)
  344. {
  345. EmitLdflg((int)PState.ZBit);
  346. Emit(OpCodes.Not);
  347. Emit(OpCodes.And);
  348. }
  349. break;
  350. }
  351. ilOp = (intCond & 1) != 0
  352. ? OpCodes.Brfalse
  353. : OpCodes.Brtrue;
  354. }
  355. else
  356. {
  357. ilOp = OpCodes.Br;
  358. }
  359. Emit(ilOp, target);
  360. }
  361. public void EmitCast(IntType intType)
  362. {
  363. switch (intType)
  364. {
  365. case IntType.UInt8: Emit(OpCodes.Conv_U1); break;
  366. case IntType.UInt16: Emit(OpCodes.Conv_U2); break;
  367. case IntType.UInt32: Emit(OpCodes.Conv_U4); break;
  368. case IntType.UInt64: Emit(OpCodes.Conv_U8); break;
  369. case IntType.Int8: Emit(OpCodes.Conv_I1); break;
  370. case IntType.Int16: Emit(OpCodes.Conv_I2); break;
  371. case IntType.Int32: Emit(OpCodes.Conv_I4); break;
  372. case IntType.Int64: Emit(OpCodes.Conv_I8); break;
  373. }
  374. bool sz64 = CurrOp.RegisterSize != RegisterSize.Int32;
  375. if (sz64 == (intType == IntType.UInt64 ||
  376. intType == IntType.Int64))
  377. {
  378. return;
  379. }
  380. if (sz64)
  381. {
  382. Emit(intType >= IntType.Int8
  383. ? OpCodes.Conv_I8
  384. : OpCodes.Conv_U8);
  385. }
  386. else
  387. {
  388. Emit(OpCodes.Conv_U4);
  389. }
  390. }
  391. public void EmitLsl(int amount) => EmitILShift(amount, OpCodes.Shl);
  392. public void EmitLsr(int amount) => EmitILShift(amount, OpCodes.Shr_Un);
  393. public void EmitAsr(int amount) => EmitILShift(amount, OpCodes.Shr);
  394. private void EmitILShift(int amount, OpCode ilOp)
  395. {
  396. if (amount > 0)
  397. {
  398. EmitLdc_I4(amount);
  399. Emit(ilOp);
  400. }
  401. }
  402. public void EmitRor(int amount)
  403. {
  404. if (amount > 0)
  405. {
  406. Stloc(RorTmpIndex, VarType.Int);
  407. Ldloc(RorTmpIndex, VarType.Int);
  408. EmitLdc_I4(amount);
  409. Emit(OpCodes.Shr_Un);
  410. Ldloc(RorTmpIndex, VarType.Int);
  411. EmitLdc_I4(CurrOp.GetBitsCount() - amount);
  412. Emit(OpCodes.Shl);
  413. Emit(OpCodes.Or);
  414. }
  415. }
  416. public ILLabel GetLabel(long position)
  417. {
  418. if (!_labels.TryGetValue(position, out ILLabel output))
  419. {
  420. output = new ILLabel();
  421. _labels.Add(position, output);
  422. }
  423. return output;
  424. }
  425. public void MarkLabel(ILLabel label)
  426. {
  427. _ilBlock.Add(label);
  428. }
  429. public void Emit(OpCode ilOp)
  430. {
  431. _ilBlock.Add(new ILOpCode(ilOp));
  432. }
  433. public void Emit(OpCode ilOp, ILLabel label)
  434. {
  435. _ilBlock.Add(new ILOpCodeBranch(ilOp, label));
  436. }
  437. public void EmitFieldLoad(FieldInfo info)
  438. {
  439. _ilBlock.Add(new ILOpCodeLoadField(info));
  440. }
  441. public void EmitPrint(string text)
  442. {
  443. _ilBlock.Add(new ILOpCodeLog(text));
  444. }
  445. public void EmitLdarg(int index)
  446. {
  447. _ilBlock.Add(new ILOpCodeLoad(index, VarType.Arg));
  448. }
  449. public void EmitLdintzr(int index)
  450. {
  451. if (index != RegisterAlias.Zr)
  452. {
  453. EmitLdint(index);
  454. }
  455. else
  456. {
  457. EmitLdc_I(0);
  458. }
  459. }
  460. public void EmitStintzr(int index)
  461. {
  462. if (index != RegisterAlias.Zr)
  463. {
  464. EmitStint(index);
  465. }
  466. else
  467. {
  468. Emit(OpCodes.Pop);
  469. }
  470. }
  471. public void EmitLoadState()
  472. {
  473. if (_ilBlock.Next == null)
  474. {
  475. throw new InvalidOperationException("Can't load state for next block, because there's no next block.");
  476. }
  477. _ilBlock.Add(new ILOpCodeLoadState(_ilBlock.Next));
  478. }
  479. public void EmitStoreState()
  480. {
  481. _ilBlock.Add(new ILOpCodeStoreState(_ilBlock));
  482. }
  483. private void EmitStoreState(TranslatedSub callSub)
  484. {
  485. _ilBlock.Add(new ILOpCodeStoreState(_ilBlock, callSub));
  486. }
  487. public void EmitLdtmp() => EmitLdint(IntGpTmp1Index);
  488. public void EmitSttmp() => EmitStint(IntGpTmp1Index);
  489. public void EmitLdtmp2() => EmitLdint(IntGpTmp2Index);
  490. public void EmitSttmp2() => EmitStint(IntGpTmp2Index);
  491. public void EmitLdvectmp() => EmitLdvec(VecGpTmp1Index);
  492. public void EmitStvectmp() => EmitStvec(VecGpTmp1Index);
  493. public void EmitLdint(int index) => Ldloc(index, VarType.Int);
  494. public void EmitStint(int index) => Stloc(index, VarType.Int);
  495. public void EmitLdvec(int index) => Ldloc(index, VarType.Vector);
  496. public void EmitStvec(int index) => Stloc(index, VarType.Vector);
  497. public void EmitLdflg(int index) => Ldloc(index, VarType.Flag);
  498. public void EmitStflg(int index)
  499. {
  500. //Set this only if any of the NZCV flag bits were modified.
  501. //This is used to ensure that, when emiting a direct IL branch
  502. //instruction for compare + branch sequences, we're not expecting
  503. //to use comparison values from an old instruction, when in fact
  504. //the flags were already overwritten by another instruction further along.
  505. if (index >= (int)PState.VBit)
  506. {
  507. _optOpLastFlagSet = CurrOp;
  508. }
  509. Stloc(index, VarType.Flag);
  510. }
  511. private void Ldloc(int index, VarType varType)
  512. {
  513. _ilBlock.Add(new ILOpCodeLoad(index, varType, CurrOp.RegisterSize));
  514. }
  515. private void Ldloc(int index, VarType varType, RegisterSize registerSize)
  516. {
  517. _ilBlock.Add(new ILOpCodeLoad(index, varType, registerSize));
  518. }
  519. private void Stloc(int index, VarType varType)
  520. {
  521. _ilBlock.Add(new ILOpCodeStore(index, varType, CurrOp.RegisterSize));
  522. }
  523. public void EmitCallPropGet(Type objType, string propName)
  524. {
  525. EmitCall(objType, $"get_{propName}");
  526. }
  527. public void EmitCallPropSet(Type objType, string propName)
  528. {
  529. EmitCall(objType, $"set_{propName}");
  530. }
  531. public void EmitCall(Type objType, string mthdName)
  532. {
  533. if (objType == null)
  534. {
  535. throw new ArgumentNullException(nameof(objType));
  536. }
  537. if (mthdName == null)
  538. {
  539. throw new ArgumentNullException(nameof(mthdName));
  540. }
  541. EmitCall(objType.GetMethod(mthdName));
  542. }
  543. public void EmitCallPrivatePropGet(Type objType, string propName)
  544. {
  545. EmitPrivateCall(objType, $"get_{propName}");
  546. }
  547. public void EmitCallPrivatePropSet(Type objType, string propName)
  548. {
  549. EmitPrivateCall(objType, $"set_{propName}");
  550. }
  551. public void EmitPrivateCall(Type objType, string mthdName)
  552. {
  553. if (objType == null)
  554. {
  555. throw new ArgumentNullException(nameof(objType));
  556. }
  557. if (mthdName == null)
  558. {
  559. throw new ArgumentNullException(nameof(mthdName));
  560. }
  561. EmitCall(objType.GetMethod(mthdName, BindingFlags.Instance | BindingFlags.NonPublic));
  562. }
  563. public void EmitCall(MethodInfo mthdInfo, bool isVirtual = false)
  564. {
  565. _ilBlock.Add(new ILOpCodeCall(mthdInfo ?? throw new ArgumentNullException(nameof(mthdInfo)), isVirtual));
  566. }
  567. public void EmitLdc_I(long value)
  568. {
  569. if (CurrOp.RegisterSize == RegisterSize.Int32)
  570. {
  571. EmitLdc_I4((int)value);
  572. }
  573. else
  574. {
  575. EmitLdc_I8(value);
  576. }
  577. }
  578. public void EmitLdc_I4(int value)
  579. {
  580. _ilBlock.Add(new ILOpCodeConst(value));
  581. }
  582. public void EmitLdc_I8(long value)
  583. {
  584. _ilBlock.Add(new ILOpCodeConst(value));
  585. }
  586. public void EmitLdc_R4(float value)
  587. {
  588. _ilBlock.Add(new ILOpCodeConst(value));
  589. }
  590. public void EmitLdc_R8(double value)
  591. {
  592. _ilBlock.Add(new ILOpCodeConst(value));
  593. }
  594. public void EmitZnFlagCheck()
  595. {
  596. EmitZnCheck(OpCodes.Ceq, (int)PState.ZBit);
  597. EmitZnCheck(OpCodes.Clt, (int)PState.NBit);
  598. }
  599. private void EmitZnCheck(OpCode ilCmpOp, int flag)
  600. {
  601. Emit(OpCodes.Dup);
  602. Emit(OpCodes.Ldc_I4_0);
  603. if (CurrOp.RegisterSize != RegisterSize.Int32)
  604. {
  605. Emit(OpCodes.Conv_I8);
  606. }
  607. Emit(ilCmpOp);
  608. EmitStflg(flag);
  609. }
  610. }
  611. }