AILEmitterCtx.cs 15 KB

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