AILEmitterCtx.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.Instruction;
  3. using ChocolArm64.State;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using System.Reflection.Emit;
  8. namespace ChocolArm64.Translation
  9. {
  10. class AILEmitterCtx
  11. {
  12. private ATranslator Translator;
  13. private Dictionary<long, AILLabel> Labels;
  14. private int BlkIndex;
  15. private int OpcIndex;
  16. private ABlock[] Graph;
  17. private ABlock Root;
  18. public ABlock CurrBlock => Graph[BlkIndex];
  19. public AOpCode CurrOp => Graph[BlkIndex].OpCodes[OpcIndex];
  20. private AILEmitter Emitter;
  21. private AILBlock ILBlock;
  22. private AOpCode OptOpLastCompare;
  23. private AOpCode OptOpLastFlagSet;
  24. //This is the index of the temporary register, used to store temporary
  25. //values needed by some functions, since IL doesn't have a swap instruction.
  26. //You can use any value here as long it doesn't conflict with the indices
  27. //for the other registers. Any value >= 64 or < 0 will do.
  28. private const int Tmp1Index = -1;
  29. private const int Tmp2Index = -2;
  30. private const int Tmp3Index = -3;
  31. private const int Tmp4Index = -4;
  32. private const int Tmp5Index = -5;
  33. public AILEmitterCtx(
  34. ATranslator Translator,
  35. ABlock[] Graph,
  36. ABlock Root,
  37. string SubName)
  38. {
  39. if (Translator == null)
  40. {
  41. throw new ArgumentNullException(nameof(Translator));
  42. }
  43. if (Graph == null)
  44. {
  45. throw new ArgumentNullException(nameof(Graph));
  46. }
  47. if (Root == null)
  48. {
  49. throw new ArgumentNullException(nameof(Root));
  50. }
  51. this.Translator = Translator;
  52. this.Graph = Graph;
  53. this.Root = Root;
  54. Labels = new Dictionary<long, AILLabel>();
  55. Emitter = new AILEmitter(Graph, Root, SubName);
  56. ILBlock = Emitter.GetILBlock(0);
  57. OpcIndex = -1;
  58. if (Graph.Length == 0 || !AdvanceOpCode())
  59. {
  60. throw new ArgumentException(nameof(Graph));
  61. }
  62. }
  63. public ATranslatedSub GetSubroutine()
  64. {
  65. return Emitter.GetSubroutine();
  66. }
  67. public bool AdvanceOpCode()
  68. {
  69. if (OpcIndex + 1 == CurrBlock.OpCodes.Count &&
  70. BlkIndex + 1 == Graph.Length)
  71. {
  72. return false;
  73. }
  74. while (++OpcIndex >= (CurrBlock?.OpCodes.Count ?? 0))
  75. {
  76. BlkIndex++;
  77. OpcIndex = -1;
  78. OptOpLastFlagSet = null;
  79. OptOpLastCompare = null;
  80. ILBlock = Emitter.GetILBlock(BlkIndex);
  81. }
  82. return true;
  83. }
  84. public void EmitOpCode()
  85. {
  86. if (OpcIndex == 0)
  87. {
  88. MarkLabel(GetLabel(CurrBlock.Position));
  89. }
  90. CurrOp.Emitter(this);
  91. ILBlock.Add(new AILBarrier());
  92. }
  93. public bool TryOptEmitSubroutineCall()
  94. {
  95. if (CurrBlock.Next == null)
  96. {
  97. return false;
  98. }
  99. if (!Translator.TryGetCachedSub(CurrOp, out ATranslatedSub Sub))
  100. {
  101. return false;
  102. }
  103. for (int Index = 0; Index < ATranslatedSub.FixedArgTypes.Length; Index++)
  104. {
  105. EmitLdarg(Index);
  106. }
  107. foreach (ARegister Reg in Sub.Params)
  108. {
  109. switch (Reg.Type)
  110. {
  111. case ARegisterType.Flag: Ldloc(Reg.Index, AIoType.Flag); break;
  112. case ARegisterType.Int: Ldloc(Reg.Index, AIoType.Int); break;
  113. case ARegisterType.Vector: Ldloc(Reg.Index, AIoType.Vector); break;
  114. }
  115. }
  116. EmitCall(Sub.Method);
  117. Sub.AddCaller(Root.Position);
  118. return true;
  119. }
  120. public void TryOptMarkCondWithoutCmp()
  121. {
  122. OptOpLastCompare = CurrOp;
  123. AInstEmitAluHelper.EmitDataLoadOpers(this);
  124. Stloc(Tmp4Index, AIoType.Int);
  125. Stloc(Tmp3Index, AIoType.Int);
  126. }
  127. private Dictionary<ACond, OpCode> BranchOps = new Dictionary<ACond, OpCode>()
  128. {
  129. { ACond.Eq, OpCodes.Beq },
  130. { ACond.Ne, OpCodes.Bne_Un },
  131. { ACond.Ge_Un, OpCodes.Bge_Un },
  132. { ACond.Lt_Un, OpCodes.Blt_Un },
  133. { ACond.Gt_Un, OpCodes.Bgt_Un },
  134. { ACond.Le_Un, OpCodes.Ble_Un },
  135. { ACond.Ge, OpCodes.Bge },
  136. { ACond.Lt, OpCodes.Blt },
  137. { ACond.Gt, OpCodes.Bgt },
  138. { ACond.Le, OpCodes.Ble }
  139. };
  140. public void EmitCondBranch(AILLabel Target, ACond Cond)
  141. {
  142. OpCode ILOp;
  143. int IntCond = (int)Cond;
  144. if (OptOpLastCompare != null &&
  145. OptOpLastCompare == OptOpLastFlagSet && BranchOps.ContainsKey(Cond))
  146. {
  147. Ldloc(Tmp3Index, AIoType.Int, OptOpLastCompare.RegisterSize);
  148. Ldloc(Tmp4Index, AIoType.Int, OptOpLastCompare.RegisterSize);
  149. if (OptOpLastCompare.Emitter == AInstEmit.Adds)
  150. {
  151. Emit(OpCodes.Neg);
  152. }
  153. ILOp = BranchOps[Cond];
  154. }
  155. else if (IntCond < 14)
  156. {
  157. int CondTrue = IntCond >> 1;
  158. switch (CondTrue)
  159. {
  160. case 0: EmitLdflg((int)APState.ZBit); break;
  161. case 1: EmitLdflg((int)APState.CBit); break;
  162. case 2: EmitLdflg((int)APState.NBit); break;
  163. case 3: EmitLdflg((int)APState.VBit); break;
  164. case 4:
  165. EmitLdflg((int)APState.CBit);
  166. EmitLdflg((int)APState.ZBit);
  167. Emit(OpCodes.Not);
  168. Emit(OpCodes.And);
  169. break;
  170. case 5:
  171. case 6:
  172. EmitLdflg((int)APState.NBit);
  173. EmitLdflg((int)APState.VBit);
  174. Emit(OpCodes.Ceq);
  175. if (CondTrue == 6)
  176. {
  177. EmitLdflg((int)APState.ZBit);
  178. Emit(OpCodes.Not);
  179. Emit(OpCodes.And);
  180. }
  181. break;
  182. }
  183. ILOp = (IntCond & 1) != 0
  184. ? OpCodes.Brfalse
  185. : OpCodes.Brtrue;
  186. }
  187. else
  188. {
  189. ILOp = OpCodes.Br;
  190. }
  191. Emit(ILOp, Target);
  192. }
  193. public void EmitCast(AIntType IntType)
  194. {
  195. switch (IntType)
  196. {
  197. case AIntType.UInt8: Emit(OpCodes.Conv_U1); break;
  198. case AIntType.UInt16: Emit(OpCodes.Conv_U2); break;
  199. case AIntType.UInt32: Emit(OpCodes.Conv_U4); break;
  200. case AIntType.UInt64: Emit(OpCodes.Conv_U8); break;
  201. case AIntType.Int8: Emit(OpCodes.Conv_I1); break;
  202. case AIntType.Int16: Emit(OpCodes.Conv_I2); break;
  203. case AIntType.Int32: Emit(OpCodes.Conv_I4); break;
  204. case AIntType.Int64: Emit(OpCodes.Conv_I8); break;
  205. }
  206. bool Sz64 = CurrOp.RegisterSize != ARegisterSize.Int32;
  207. if (Sz64 == (IntType == AIntType.UInt64 ||
  208. IntType == AIntType.Int64))
  209. {
  210. return;
  211. }
  212. if (Sz64)
  213. {
  214. Emit(IntType >= AIntType.Int8
  215. ? OpCodes.Conv_I8
  216. : OpCodes.Conv_U8);
  217. }
  218. else
  219. {
  220. Emit(OpCodes.Conv_U4);
  221. }
  222. }
  223. public void EmitLsl(int Amount) => EmitILShift(Amount, OpCodes.Shl);
  224. public void EmitLsr(int Amount) => EmitILShift(Amount, OpCodes.Shr_Un);
  225. public void EmitAsr(int Amount) => EmitILShift(Amount, OpCodes.Shr);
  226. private void EmitILShift(int Amount, OpCode ILOp)
  227. {
  228. if (Amount > 0)
  229. {
  230. EmitLdc_I4(Amount);
  231. Emit(ILOp);
  232. }
  233. }
  234. public void EmitRor(int Amount)
  235. {
  236. if (Amount > 0)
  237. {
  238. Stloc(Tmp2Index, AIoType.Int);
  239. Ldloc(Tmp2Index, AIoType.Int);
  240. EmitLdc_I4(Amount);
  241. Emit(OpCodes.Shr_Un);
  242. Ldloc(Tmp2Index, AIoType.Int);
  243. EmitLdc_I4(CurrOp.GetBitsCount() - Amount);
  244. Emit(OpCodes.Shl);
  245. Emit(OpCodes.Or);
  246. }
  247. }
  248. public AILLabel GetLabel(long Position)
  249. {
  250. if (!Labels.TryGetValue(Position, out AILLabel Output))
  251. {
  252. Output = new AILLabel();
  253. Labels.Add(Position, Output);
  254. }
  255. return Output;
  256. }
  257. public void MarkLabel(AILLabel Label)
  258. {
  259. ILBlock.Add(Label);
  260. }
  261. public void Emit(OpCode ILOp)
  262. {
  263. ILBlock.Add(new AILOpCode(ILOp));
  264. }
  265. public void Emit(OpCode ILOp, AILLabel Label)
  266. {
  267. ILBlock.Add(new AILOpCodeBranch(ILOp, Label));
  268. }
  269. public void Emit(string Text)
  270. {
  271. ILBlock.Add(new AILOpCodeLog(Text));
  272. }
  273. public void EmitLdarg(int Index)
  274. {
  275. ILBlock.Add(new AILOpCodeLoad(Index, AIoType.Arg));
  276. }
  277. public void EmitLdintzr(int Index)
  278. {
  279. if (Index != AThreadState.ZRIndex)
  280. {
  281. EmitLdint(Index);
  282. }
  283. else
  284. {
  285. EmitLdc_I(0);
  286. }
  287. }
  288. public void EmitStintzr(int Index)
  289. {
  290. if (Index != AThreadState.ZRIndex)
  291. {
  292. EmitStint(Index);
  293. }
  294. else
  295. {
  296. Emit(OpCodes.Pop);
  297. }
  298. }
  299. public void EmitLoadState(ABlock RetBlk)
  300. {
  301. ILBlock.Add(new AILOpCodeLoad(Array.IndexOf(Graph, RetBlk), AIoType.Fields));
  302. }
  303. public void EmitStoreState()
  304. {
  305. ILBlock.Add(new AILOpCodeStore(Array.IndexOf(Graph, CurrBlock), AIoType.Fields));
  306. }
  307. public void EmitLdtmp() => EmitLdint(Tmp1Index);
  308. public void EmitSttmp() => EmitStint(Tmp1Index);
  309. public void EmitLdvectmp() => EmitLdvec(Tmp5Index);
  310. public void EmitStvectmp() => EmitStvec(Tmp5Index);
  311. public void EmitLdint(int Index) => Ldloc(Index, AIoType.Int);
  312. public void EmitStint(int Index) => Stloc(Index, AIoType.Int);
  313. public void EmitLdvec(int Index) => Ldloc(Index, AIoType.Vector);
  314. public void EmitStvec(int Index) => Stloc(Index, AIoType.Vector);
  315. public void EmitLdflg(int Index) => Ldloc(Index, AIoType.Flag);
  316. public void EmitStflg(int Index)
  317. {
  318. OptOpLastFlagSet = CurrOp;
  319. Stloc(Index, AIoType.Flag);
  320. }
  321. private void Ldloc(int Index, AIoType IoType)
  322. {
  323. ILBlock.Add(new AILOpCodeLoad(Index, IoType, CurrOp.RegisterSize));
  324. }
  325. private void Ldloc(int Index, AIoType IoType, ARegisterSize RegisterSize)
  326. {
  327. ILBlock.Add(new AILOpCodeLoad(Index, IoType, RegisterSize));
  328. }
  329. private void Stloc(int Index, AIoType IoType)
  330. {
  331. ILBlock.Add(new AILOpCodeStore(Index, IoType, CurrOp.RegisterSize));
  332. }
  333. public void EmitCallPropGet(Type ObjType, string PropName)
  334. {
  335. if (ObjType == null)
  336. {
  337. throw new ArgumentNullException(nameof(ObjType));
  338. }
  339. if (PropName == null)
  340. {
  341. throw new ArgumentNullException(nameof(PropName));
  342. }
  343. EmitCall(ObjType.GetMethod($"get_{PropName}"));
  344. }
  345. public void EmitCallPropSet(Type ObjType, string PropName)
  346. {
  347. if (ObjType == null)
  348. {
  349. throw new ArgumentNullException(nameof(ObjType));
  350. }
  351. if (PropName == null)
  352. {
  353. throw new ArgumentNullException(nameof(PropName));
  354. }
  355. EmitCall(ObjType.GetMethod($"set_{PropName}"));
  356. }
  357. public void EmitCall(Type ObjType, string MthdName)
  358. {
  359. if (ObjType == null)
  360. {
  361. throw new ArgumentNullException(nameof(ObjType));
  362. }
  363. if (MthdName == null)
  364. {
  365. throw new ArgumentNullException(nameof(MthdName));
  366. }
  367. EmitCall(ObjType.GetMethod(MthdName));
  368. }
  369. public void EmitPrivateCall(Type ObjType, string MthdName)
  370. {
  371. if (ObjType == null)
  372. {
  373. throw new ArgumentNullException(nameof(ObjType));
  374. }
  375. if (MthdName == null)
  376. {
  377. throw new ArgumentNullException(nameof(MthdName));
  378. }
  379. EmitCall(ObjType.GetMethod(MthdName, BindingFlags.Instance | BindingFlags.NonPublic));
  380. }
  381. public void EmitCall(MethodInfo MthdInfo)
  382. {
  383. if (MthdInfo == null)
  384. {
  385. throw new ArgumentNullException(nameof(MthdInfo));
  386. }
  387. ILBlock.Add(new AILOpCodeCall(MthdInfo));
  388. }
  389. public void EmitLdc_I(long Value)
  390. {
  391. if (CurrOp.RegisterSize == ARegisterSize.Int32)
  392. {
  393. EmitLdc_I4((int)Value);
  394. }
  395. else
  396. {
  397. EmitLdc_I8(Value);
  398. }
  399. }
  400. public void EmitLdc_I4(int Value)
  401. {
  402. ILBlock.Add(new AILOpCodeConst(Value));
  403. }
  404. public void EmitLdc_I8(long Value)
  405. {
  406. ILBlock.Add(new AILOpCodeConst(Value));
  407. }
  408. public void EmitLdc_R4(float Value)
  409. {
  410. ILBlock.Add(new AILOpCodeConst(Value));
  411. }
  412. public void EmitLdc_R8(double Value)
  413. {
  414. ILBlock.Add(new AILOpCodeConst(Value));
  415. }
  416. public void EmitZNFlagCheck()
  417. {
  418. EmitZNCheck(OpCodes.Ceq, (int)APState.ZBit);
  419. EmitZNCheck(OpCodes.Clt, (int)APState.NBit);
  420. }
  421. private void EmitZNCheck(OpCode ILCmpOp, int Flag)
  422. {
  423. Emit(OpCodes.Dup);
  424. Emit(OpCodes.Ldc_I4_0);
  425. if (CurrOp.RegisterSize != ARegisterSize.Int32)
  426. {
  427. Emit(OpCodes.Conv_I8);
  428. }
  429. Emit(ILCmpOp);
  430. EmitStflg(Flag);
  431. }
  432. }
  433. }