AILEmitterCtx.cs 15 KB

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