AILEmitterCtx.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. if (IntType == AIntType.UInt64 ||
  209. IntType == AIntType.Int64)
  210. {
  211. return;
  212. }
  213. if (CurrOp.RegisterSize != ARegisterSize.Int32)
  214. {
  215. Emit(IntType >= AIntType.Int8
  216. ? OpCodes.Conv_I8
  217. : OpCodes.Conv_U8);
  218. }
  219. }
  220. public void EmitLsl(int Amount) => EmitILShift(Amount, OpCodes.Shl);
  221. public void EmitLsr(int Amount) => EmitILShift(Amount, OpCodes.Shr_Un);
  222. public void EmitAsr(int Amount) => EmitILShift(Amount, OpCodes.Shr);
  223. private void EmitILShift(int Amount, OpCode ILOp)
  224. {
  225. if (Amount > 0)
  226. {
  227. EmitLdc_I4(Amount);
  228. Emit(ILOp);
  229. }
  230. }
  231. public void EmitRor(int Amount)
  232. {
  233. if (Amount > 0)
  234. {
  235. Stloc(Tmp2Index, AIoType.Int);
  236. Ldloc(Tmp2Index, AIoType.Int);
  237. EmitLdc_I4(Amount);
  238. Emit(OpCodes.Shr_Un);
  239. Ldloc(Tmp2Index, AIoType.Int);
  240. EmitLdc_I4(CurrOp.GetBitsCount() - Amount);
  241. Emit(OpCodes.Shl);
  242. Emit(OpCodes.Or);
  243. }
  244. }
  245. public AILLabel GetLabel(long Position)
  246. {
  247. if (!Labels.TryGetValue(Position, out AILLabel Output))
  248. {
  249. Output = new AILLabel();
  250. Labels.Add(Position, Output);
  251. }
  252. return Output;
  253. }
  254. public void MarkLabel(AILLabel Label)
  255. {
  256. ILBlock.Add(Label);
  257. }
  258. public void Emit(OpCode ILOp)
  259. {
  260. ILBlock.Add(new AILOpCode(ILOp));
  261. }
  262. public void Emit(OpCode ILOp, AILLabel Label)
  263. {
  264. ILBlock.Add(new AILOpCodeBranch(ILOp, Label));
  265. }
  266. public void Emit(string Text)
  267. {
  268. ILBlock.Add(new AILOpCodeLog(Text));
  269. }
  270. public void EmitLdarg(int Index)
  271. {
  272. ILBlock.Add(new AILOpCodeLoad(Index, AIoType.Arg));
  273. }
  274. public void EmitLdintzr(int Index)
  275. {
  276. if (Index != AThreadState.ZRIndex)
  277. {
  278. EmitLdint(Index);
  279. }
  280. else
  281. {
  282. EmitLdc_I(0);
  283. }
  284. }
  285. public void EmitStintzr(int Index)
  286. {
  287. if (Index != AThreadState.ZRIndex)
  288. {
  289. EmitStint(Index);
  290. }
  291. else
  292. {
  293. Emit(OpCodes.Pop);
  294. }
  295. }
  296. public void EmitLoadState(ABlock RetBlk)
  297. {
  298. ILBlock.Add(new AILOpCodeLoad(Array.IndexOf(Graph, RetBlk), AIoType.Fields));
  299. }
  300. public void EmitStoreState()
  301. {
  302. ILBlock.Add(new AILOpCodeStore(Array.IndexOf(Graph, CurrBlock), AIoType.Fields));
  303. }
  304. public void EmitLdtmp() => EmitLdint(Tmp1Index);
  305. public void EmitSttmp() => EmitStint(Tmp1Index);
  306. public void EmitLdvectmp() => EmitLdvec(Tmp5Index);
  307. public void EmitStvectmp() => EmitStvec(Tmp5Index);
  308. public void EmitLdint(int Index) => Ldloc(Index, AIoType.Int);
  309. public void EmitStint(int Index) => Stloc(Index, AIoType.Int);
  310. public void EmitLdvec(int Index) => Ldloc(Index, AIoType.Vector);
  311. public void EmitStvec(int Index) => Stloc(Index, AIoType.Vector);
  312. public void EmitLdflg(int Index) => Ldloc(Index, AIoType.Flag);
  313. public void EmitStflg(int Index)
  314. {
  315. OptOpLastFlagSet = CurrOp;
  316. Stloc(Index, AIoType.Flag);
  317. }
  318. private void Ldloc(int Index, AIoType IoType)
  319. {
  320. ILBlock.Add(new AILOpCodeLoad(Index, IoType, CurrOp.RegisterSize));
  321. }
  322. private void Ldloc(int Index, AIoType IoType, ARegisterSize RegisterSize)
  323. {
  324. ILBlock.Add(new AILOpCodeLoad(Index, IoType, RegisterSize));
  325. }
  326. private void Stloc(int Index, AIoType IoType)
  327. {
  328. ILBlock.Add(new AILOpCodeStore(Index, IoType, CurrOp.RegisterSize));
  329. }
  330. public void EmitCallPropGet(Type ObjType, string PropName)
  331. {
  332. if (ObjType == null)
  333. {
  334. throw new ArgumentNullException(nameof(ObjType));
  335. }
  336. if (PropName == null)
  337. {
  338. throw new ArgumentNullException(nameof(PropName));
  339. }
  340. EmitCall(ObjType.GetMethod($"get_{PropName}"));
  341. }
  342. public void EmitCallPropSet(Type ObjType, string PropName)
  343. {
  344. if (ObjType == null)
  345. {
  346. throw new ArgumentNullException(nameof(ObjType));
  347. }
  348. if (PropName == null)
  349. {
  350. throw new ArgumentNullException(nameof(PropName));
  351. }
  352. EmitCall(ObjType.GetMethod($"set_{PropName}"));
  353. }
  354. public void EmitCall(Type ObjType, string MthdName)
  355. {
  356. if (ObjType == null)
  357. {
  358. throw new ArgumentNullException(nameof(ObjType));
  359. }
  360. if (MthdName == null)
  361. {
  362. throw new ArgumentNullException(nameof(MthdName));
  363. }
  364. EmitCall(ObjType.GetMethod(MthdName));
  365. }
  366. public void EmitCall(MethodInfo MthdInfo)
  367. {
  368. if (MthdInfo == null)
  369. {
  370. throw new ArgumentNullException(nameof(MthdInfo));
  371. }
  372. ILBlock.Add(new AILOpCodeCall(MthdInfo));
  373. }
  374. public void EmitLdc_I(long Value)
  375. {
  376. if (CurrOp.RegisterSize == ARegisterSize.Int32)
  377. {
  378. EmitLdc_I4((int)Value);
  379. }
  380. else
  381. {
  382. EmitLdc_I8(Value);
  383. }
  384. }
  385. public void EmitLdc_I4(int Value)
  386. {
  387. ILBlock.Add(new AILOpCodeConst(Value));
  388. }
  389. public void EmitLdc_I8(long Value)
  390. {
  391. ILBlock.Add(new AILOpCodeConst(Value));
  392. }
  393. public void EmitLdc_R4(float Value)
  394. {
  395. ILBlock.Add(new AILOpCodeConst(Value));
  396. }
  397. public void EmitLdc_R8(double Value)
  398. {
  399. ILBlock.Add(new AILOpCodeConst(Value));
  400. }
  401. public void EmitZNFlagCheck()
  402. {
  403. EmitZNCheck(OpCodes.Ceq, (int)APState.ZBit);
  404. EmitZNCheck(OpCodes.Clt, (int)APState.NBit);
  405. }
  406. private void EmitZNCheck(OpCode ILCmpOp, int Flag)
  407. {
  408. Emit(OpCodes.Dup);
  409. Emit(OpCodes.Ldc_I4_0);
  410. if (CurrOp.RegisterSize != ARegisterSize.Int32)
  411. {
  412. Emit(OpCodes.Conv_I8);
  413. }
  414. Emit(ILCmpOp);
  415. EmitStflg(Flag);
  416. }
  417. }
  418. }