AILEmitterCtx.cs 14 KB

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