AILEmitterCtx.cs 13 KB

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