AILEmitterCtx.cs 15 KB

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