EmitterContext.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. using ARMeilleure.Diagnostics;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation.PTC;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  9. namespace ARMeilleure.Translation
  10. {
  11. class EmitterContext
  12. {
  13. private readonly Dictionary<Operand, BasicBlock> _irLabels;
  14. private readonly IntrusiveList<BasicBlock> _irBlocks;
  15. private BasicBlock _irBlock;
  16. private BasicBlock _ifBlock;
  17. private bool _needsNewBlock;
  18. private BasicBlockFrequency _nextBlockFreq;
  19. public EmitterContext()
  20. {
  21. _irLabels = new Dictionary<Operand, BasicBlock>();
  22. _irBlocks = new IntrusiveList<BasicBlock>();
  23. _needsNewBlock = true;
  24. _nextBlockFreq = BasicBlockFrequency.Default;
  25. }
  26. public Operand Add(Operand op1, Operand op2)
  27. {
  28. return Add(Instruction.Add, Local(op1.Type), op1, op2);
  29. }
  30. public Operand BitwiseAnd(Operand op1, Operand op2)
  31. {
  32. return Add(Instruction.BitwiseAnd, Local(op1.Type), op1, op2);
  33. }
  34. public Operand BitwiseExclusiveOr(Operand op1, Operand op2)
  35. {
  36. return Add(Instruction.BitwiseExclusiveOr, Local(op1.Type), op1, op2);
  37. }
  38. public Operand BitwiseNot(Operand op1)
  39. {
  40. return Add(Instruction.BitwiseNot, Local(op1.Type), op1);
  41. }
  42. public Operand BitwiseOr(Operand op1, Operand op2)
  43. {
  44. return Add(Instruction.BitwiseOr, Local(op1.Type), op1, op2);
  45. }
  46. public void Branch(Operand label)
  47. {
  48. NewNextBlockIfNeeded();
  49. BranchToLabel(label, uncond: true, BasicBlockFrequency.Default);
  50. }
  51. public void BranchIf(Operand label, Operand op1, Operand op2, Comparison comp, BasicBlockFrequency falseFreq = default)
  52. {
  53. Add(Instruction.BranchIf, null, op1, op2, Const((int)comp));
  54. BranchToLabel(label, uncond: false, falseFreq);
  55. }
  56. public void BranchIfFalse(Operand label, Operand op1, BasicBlockFrequency falseFreq = default)
  57. {
  58. BranchIf(label, op1, Const(op1.Type, 0), Comparison.Equal, falseFreq);
  59. }
  60. public void BranchIfTrue(Operand label, Operand op1, BasicBlockFrequency falseFreq = default)
  61. {
  62. BranchIf(label, op1, Const(op1.Type, 0), Comparison.NotEqual, falseFreq);
  63. }
  64. public Operand ByteSwap(Operand op1)
  65. {
  66. return Add(Instruction.ByteSwap, Local(op1.Type), op1);
  67. }
  68. public Operand Call(MethodInfo info, params Operand[] callArgs)
  69. {
  70. if (Ptc.State == PtcState.Disabled)
  71. {
  72. IntPtr funcPtr = Delegates.GetDelegateFuncPtr(info);
  73. OperandType returnType = GetOperandType(info.ReturnType);
  74. Symbols.Add((ulong)funcPtr.ToInt64(), info.Name);
  75. return Call(Const(funcPtr.ToInt64()), returnType, callArgs);
  76. }
  77. else
  78. {
  79. int index = Delegates.GetDelegateIndex(info);
  80. IntPtr funcPtr = Delegates.GetDelegateFuncPtrByIndex(index);
  81. OperandType returnType = GetOperandType(info.ReturnType);
  82. Symbols.Add((ulong)funcPtr.ToInt64(), info.Name);
  83. return Call(Const(funcPtr.ToInt64(), true, index), returnType, callArgs);
  84. }
  85. }
  86. private static OperandType GetOperandType(Type type)
  87. {
  88. if (type == typeof(bool) || type == typeof(byte) ||
  89. type == typeof(char) || type == typeof(short) ||
  90. type == typeof(int) || type == typeof(sbyte) ||
  91. type == typeof(ushort) || type == typeof(uint))
  92. {
  93. return OperandType.I32;
  94. }
  95. else if (type == typeof(long) || type == typeof(ulong))
  96. {
  97. return OperandType.I64;
  98. }
  99. else if (type == typeof(double))
  100. {
  101. return OperandType.FP64;
  102. }
  103. else if (type == typeof(float))
  104. {
  105. return OperandType.FP32;
  106. }
  107. else if (type == typeof(V128))
  108. {
  109. return OperandType.V128;
  110. }
  111. else if (type == typeof(void))
  112. {
  113. return OperandType.None;
  114. }
  115. else
  116. {
  117. throw new ArgumentException($"Invalid type \"{type.Name}\".");
  118. }
  119. }
  120. public Operand Call(Operand address, OperandType returnType, params Operand[] callArgs)
  121. {
  122. Operand[] args = new Operand[callArgs.Length + 1];
  123. args[0] = address;
  124. Array.Copy(callArgs, 0, args, 1, callArgs.Length);
  125. if (returnType != OperandType.None)
  126. {
  127. return Add(Instruction.Call, Local(returnType), args);
  128. }
  129. else
  130. {
  131. return Add(Instruction.Call, null, args);
  132. }
  133. }
  134. public void Tailcall(Operand address, params Operand[] callArgs)
  135. {
  136. Operand[] args = new Operand[callArgs.Length + 1];
  137. args[0] = address;
  138. Array.Copy(callArgs, 0, args, 1, callArgs.Length);
  139. Add(Instruction.Tailcall, null, args);
  140. _needsNewBlock = true;
  141. }
  142. public Operand CompareAndSwap(Operand address, Operand expected, Operand desired)
  143. {
  144. return Add(Instruction.CompareAndSwap, Local(desired.Type), address, expected, desired);
  145. }
  146. public Operand CompareAndSwap16(Operand address, Operand expected, Operand desired)
  147. {
  148. return Add(Instruction.CompareAndSwap16, Local(OperandType.I32), address, expected, desired);
  149. }
  150. public Operand CompareAndSwap8(Operand address, Operand expected, Operand desired)
  151. {
  152. return Add(Instruction.CompareAndSwap8, Local(OperandType.I32), address, expected, desired);
  153. }
  154. public Operand ConditionalSelect(Operand op1, Operand op2, Operand op3)
  155. {
  156. return Add(Instruction.ConditionalSelect, Local(op2.Type), op1, op2, op3);
  157. }
  158. public Operand ConvertI64ToI32(Operand op1)
  159. {
  160. if (op1.Type != OperandType.I64)
  161. {
  162. throw new ArgumentException($"Invalid operand type \"{op1.Type}\".");
  163. }
  164. return Add(Instruction.ConvertI64ToI32, Local(OperandType.I32), op1);
  165. }
  166. public Operand ConvertToFP(OperandType type, Operand op1)
  167. {
  168. return Add(Instruction.ConvertToFP, Local(type), op1);
  169. }
  170. public Operand ConvertToFPUI(OperandType type, Operand op1)
  171. {
  172. return Add(Instruction.ConvertToFPUI, Local(type), op1);
  173. }
  174. public Operand Copy(Operand op1)
  175. {
  176. return Add(Instruction.Copy, Local(op1.Type), op1);
  177. }
  178. public Operand Copy(Operand dest, Operand op1)
  179. {
  180. if (dest.Kind != OperandKind.Register)
  181. {
  182. throw new ArgumentException($"Invalid dest operand kind \"{dest.Kind}\".");
  183. }
  184. return Add(Instruction.Copy, dest, op1);
  185. }
  186. public Operand CountLeadingZeros(Operand op1)
  187. {
  188. return Add(Instruction.CountLeadingZeros, Local(op1.Type), op1);
  189. }
  190. public Operand Divide(Operand op1, Operand op2)
  191. {
  192. return Add(Instruction.Divide, Local(op1.Type), op1, op2);
  193. }
  194. public Operand DivideUI(Operand op1, Operand op2)
  195. {
  196. return Add(Instruction.DivideUI, Local(op1.Type), op1, op2);
  197. }
  198. public Operand ICompare(Operand op1, Operand op2, Comparison comp)
  199. {
  200. return Add(Instruction.Compare, Local(OperandType.I32), op1, op2, Const((int)comp));
  201. }
  202. public Operand ICompareEqual(Operand op1, Operand op2)
  203. {
  204. return ICompare(op1, op2, Comparison.Equal);
  205. }
  206. public Operand ICompareGreater(Operand op1, Operand op2)
  207. {
  208. return ICompare(op1, op2, Comparison.Greater);
  209. }
  210. public Operand ICompareGreaterOrEqual(Operand op1, Operand op2)
  211. {
  212. return ICompare(op1, op2, Comparison.GreaterOrEqual);
  213. }
  214. public Operand ICompareGreaterOrEqualUI(Operand op1, Operand op2)
  215. {
  216. return ICompare(op1, op2, Comparison.GreaterOrEqualUI);
  217. }
  218. public Operand ICompareGreaterUI(Operand op1, Operand op2)
  219. {
  220. return ICompare(op1, op2, Comparison.GreaterUI);
  221. }
  222. public Operand ICompareLess(Operand op1, Operand op2)
  223. {
  224. return ICompare(op1, op2, Comparison.Less);
  225. }
  226. public Operand ICompareLessOrEqual(Operand op1, Operand op2)
  227. {
  228. return ICompare(op1, op2, Comparison.LessOrEqual);
  229. }
  230. public Operand ICompareLessOrEqualUI(Operand op1, Operand op2)
  231. {
  232. return ICompare(op1, op2, Comparison.LessOrEqualUI);
  233. }
  234. public Operand ICompareLessUI(Operand op1, Operand op2)
  235. {
  236. return ICompare(op1, op2, Comparison.LessUI);
  237. }
  238. public Operand ICompareNotEqual(Operand op1, Operand op2)
  239. {
  240. return ICompare(op1, op2, Comparison.NotEqual);
  241. }
  242. public Operand Load(OperandType type, Operand address)
  243. {
  244. return Add(Instruction.Load, Local(type), address);
  245. }
  246. public Operand Load16(Operand address)
  247. {
  248. return Add(Instruction.Load16, Local(OperandType.I32), address);
  249. }
  250. public Operand Load8(Operand address)
  251. {
  252. return Add(Instruction.Load8, Local(OperandType.I32), address);
  253. }
  254. public Operand LoadArgument(OperandType type, int index)
  255. {
  256. return Add(Instruction.LoadArgument, Local(type), Const(index));
  257. }
  258. public void LoadFromContext()
  259. {
  260. _needsNewBlock = true;
  261. Add(Instruction.LoadFromContext);
  262. }
  263. public Operand Multiply(Operand op1, Operand op2)
  264. {
  265. return Add(Instruction.Multiply, Local(op1.Type), op1, op2);
  266. }
  267. public Operand Multiply64HighSI(Operand op1, Operand op2)
  268. {
  269. return Add(Instruction.Multiply64HighSI, Local(OperandType.I64), op1, op2);
  270. }
  271. public Operand Multiply64HighUI(Operand op1, Operand op2)
  272. {
  273. return Add(Instruction.Multiply64HighUI, Local(OperandType.I64), op1, op2);
  274. }
  275. public Operand Negate(Operand op1)
  276. {
  277. return Add(Instruction.Negate, Local(op1.Type), op1);
  278. }
  279. public void Return()
  280. {
  281. Add(Instruction.Return);
  282. _needsNewBlock = true;
  283. }
  284. public void Return(Operand op1)
  285. {
  286. Add(Instruction.Return, null, op1);
  287. _needsNewBlock = true;
  288. }
  289. public Operand RotateRight(Operand op1, Operand op2)
  290. {
  291. return Add(Instruction.RotateRight, Local(op1.Type), op1, op2);
  292. }
  293. public Operand ShiftLeft(Operand op1, Operand op2)
  294. {
  295. return Add(Instruction.ShiftLeft, Local(op1.Type), op1, op2);
  296. }
  297. public Operand ShiftRightSI(Operand op1, Operand op2)
  298. {
  299. return Add(Instruction.ShiftRightSI, Local(op1.Type), op1, op2);
  300. }
  301. public Operand ShiftRightUI(Operand op1, Operand op2)
  302. {
  303. return Add(Instruction.ShiftRightUI, Local(op1.Type), op1, op2);
  304. }
  305. public Operand SignExtend16(OperandType type, Operand op1)
  306. {
  307. return Add(Instruction.SignExtend16, Local(type), op1);
  308. }
  309. public Operand SignExtend32(OperandType type, Operand op1)
  310. {
  311. return Add(Instruction.SignExtend32, Local(type), op1);
  312. }
  313. public Operand SignExtend8(OperandType type, Operand op1)
  314. {
  315. return Add(Instruction.SignExtend8, Local(type), op1);
  316. }
  317. public void Store(Operand address, Operand value)
  318. {
  319. Add(Instruction.Store, null, address, value);
  320. }
  321. public void Store16(Operand address, Operand value)
  322. {
  323. Add(Instruction.Store16, null, address, value);
  324. }
  325. public void Store8(Operand address, Operand value)
  326. {
  327. Add(Instruction.Store8, null, address, value);
  328. }
  329. public void StoreToContext()
  330. {
  331. Add(Instruction.StoreToContext);
  332. _needsNewBlock = true;
  333. }
  334. public Operand Subtract(Operand op1, Operand op2)
  335. {
  336. return Add(Instruction.Subtract, Local(op1.Type), op1, op2);
  337. }
  338. public Operand VectorCreateScalar(Operand value)
  339. {
  340. return Add(Instruction.VectorCreateScalar, Local(OperandType.V128), value);
  341. }
  342. public Operand VectorExtract(OperandType type, Operand vector, int index)
  343. {
  344. return Add(Instruction.VectorExtract, Local(type), vector, Const(index));
  345. }
  346. public Operand VectorExtract16(Operand vector, int index)
  347. {
  348. return Add(Instruction.VectorExtract16, Local(OperandType.I32), vector, Const(index));
  349. }
  350. public Operand VectorExtract8(Operand vector, int index)
  351. {
  352. return Add(Instruction.VectorExtract8, Local(OperandType.I32), vector, Const(index));
  353. }
  354. public Operand VectorInsert(Operand vector, Operand value, int index)
  355. {
  356. return Add(Instruction.VectorInsert, Local(OperandType.V128), vector, value, Const(index));
  357. }
  358. public Operand VectorInsert16(Operand vector, Operand value, int index)
  359. {
  360. return Add(Instruction.VectorInsert16, Local(OperandType.V128), vector, value, Const(index));
  361. }
  362. public Operand VectorInsert8(Operand vector, Operand value, int index)
  363. {
  364. return Add(Instruction.VectorInsert8, Local(OperandType.V128), vector, value, Const(index));
  365. }
  366. public Operand VectorOne()
  367. {
  368. return Add(Instruction.VectorOne, Local(OperandType.V128));
  369. }
  370. public Operand VectorZero()
  371. {
  372. return Add(Instruction.VectorZero, Local(OperandType.V128));
  373. }
  374. public Operand VectorZeroUpper64(Operand vector)
  375. {
  376. return Add(Instruction.VectorZeroUpper64, Local(OperandType.V128), vector);
  377. }
  378. public Operand VectorZeroUpper96(Operand vector)
  379. {
  380. return Add(Instruction.VectorZeroUpper96, Local(OperandType.V128), vector);
  381. }
  382. public Operand ZeroExtend16(OperandType type, Operand op1)
  383. {
  384. return Add(Instruction.ZeroExtend16, Local(type), op1);
  385. }
  386. public Operand ZeroExtend32(OperandType type, Operand op1)
  387. {
  388. return Add(Instruction.ZeroExtend32, Local(type), op1);
  389. }
  390. public Operand ZeroExtend8(OperandType type, Operand op1)
  391. {
  392. return Add(Instruction.ZeroExtend8, Local(type), op1);
  393. }
  394. private void NewNextBlockIfNeeded()
  395. {
  396. if (_needsNewBlock)
  397. {
  398. NewNextBlock();
  399. }
  400. }
  401. private Operand Add(Instruction inst, Operand dest = null)
  402. {
  403. NewNextBlockIfNeeded();
  404. Operation operation = OperationHelper.Operation(inst, dest);
  405. _irBlock.Operations.AddLast(operation);
  406. return dest;
  407. }
  408. private Operand Add(Instruction inst, Operand dest, Operand[] sources)
  409. {
  410. NewNextBlockIfNeeded();
  411. Operation operation = OperationHelper.Operation(inst, dest, sources);
  412. _irBlock.Operations.AddLast(operation);
  413. return dest;
  414. }
  415. private Operand Add(Instruction inst, Operand dest, Operand source0)
  416. {
  417. NewNextBlockIfNeeded();
  418. Operation operation = OperationHelper.Operation(inst, dest, source0);
  419. _irBlock.Operations.AddLast(operation);
  420. return dest;
  421. }
  422. private Operand Add(Instruction inst, Operand dest, Operand source0, Operand source1)
  423. {
  424. NewNextBlockIfNeeded();
  425. Operation operation = OperationHelper.Operation(inst, dest, source0, source1);
  426. _irBlock.Operations.AddLast(operation);
  427. return dest;
  428. }
  429. private Operand Add(Instruction inst, Operand dest, Operand source0, Operand source1, Operand source2)
  430. {
  431. NewNextBlockIfNeeded();
  432. Operation operation = OperationHelper.Operation(inst, dest, source0, source1, source2);
  433. _irBlock.Operations.AddLast(operation);
  434. return dest;
  435. }
  436. public Operand AddIntrinsic(Intrinsic intrin, params Operand[] args)
  437. {
  438. return Add(intrin, Local(OperandType.V128), args);
  439. }
  440. public Operand AddIntrinsicInt(Intrinsic intrin, params Operand[] args)
  441. {
  442. return Add(intrin, Local(OperandType.I32), args);
  443. }
  444. public Operand AddIntrinsicLong(Intrinsic intrin, params Operand[] args)
  445. {
  446. return Add(intrin, Local(OperandType.I64), args);
  447. }
  448. public void AddIntrinsicNoRet(Intrinsic intrin, params Operand[] args)
  449. {
  450. Add(intrin, null, args);
  451. }
  452. private Operand Add(Intrinsic intrin, Operand dest, params Operand[] sources)
  453. {
  454. NewNextBlockIfNeeded();
  455. IntrinsicOperation operation = new IntrinsicOperation(intrin, dest, sources);
  456. _irBlock.Operations.AddLast(operation);
  457. return dest;
  458. }
  459. private void BranchToLabel(Operand label, bool uncond, BasicBlockFrequency nextFreq)
  460. {
  461. if (!_irLabels.TryGetValue(label, out BasicBlock branchBlock))
  462. {
  463. branchBlock = new BasicBlock();
  464. _irLabels.Add(label, branchBlock);
  465. }
  466. if (uncond)
  467. {
  468. _irBlock.AddSuccessor(branchBlock);
  469. }
  470. else
  471. {
  472. // Defer registration of successor to _irBlock so that the order of successors is correct.
  473. _ifBlock = branchBlock;
  474. }
  475. _needsNewBlock = true;
  476. _nextBlockFreq = nextFreq;
  477. }
  478. public void MarkLabel(Operand label, BasicBlockFrequency nextFreq = default)
  479. {
  480. _nextBlockFreq = nextFreq;
  481. if (_irLabels.TryGetValue(label, out BasicBlock nextBlock))
  482. {
  483. nextBlock.Index = _irBlocks.Count;
  484. _irBlocks.AddLast(nextBlock);
  485. NextBlock(nextBlock);
  486. }
  487. else
  488. {
  489. NewNextBlock();
  490. _irLabels.Add(label, _irBlock);
  491. }
  492. }
  493. private void NewNextBlock()
  494. {
  495. BasicBlock block = new BasicBlock(_irBlocks.Count);
  496. _irBlocks.AddLast(block);
  497. NextBlock(block);
  498. }
  499. private void NextBlock(BasicBlock nextBlock)
  500. {
  501. if (_irBlock?.SuccessorCount == 0 && !EndsWithUnconditional(_irBlock))
  502. {
  503. _irBlock.AddSuccessor(nextBlock);
  504. if (_ifBlock != null)
  505. {
  506. _irBlock.AddSuccessor(_ifBlock);
  507. _ifBlock = null;
  508. }
  509. }
  510. _irBlock = nextBlock;
  511. _irBlock.Frequency = _nextBlockFreq;
  512. _needsNewBlock = false;
  513. _nextBlockFreq = BasicBlockFrequency.Default;
  514. }
  515. private static bool EndsWithUnconditional(BasicBlock block)
  516. {
  517. return block.Operations.Last is Operation lastOp &&
  518. (lastOp.Instruction == Instruction.Return ||
  519. lastOp.Instruction == Instruction.Tailcall);
  520. }
  521. public ControlFlowGraph GetControlFlowGraph()
  522. {
  523. return new ControlFlowGraph(_irBlocks.First, _irBlocks);
  524. }
  525. }
  526. }