AILEmitterCtx.cs 15 KB

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