AILEmitterCtx.cs 14 KB

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