EmitterContext.cs 20 KB

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