AILEmitterCtx.cs 14 KB

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