EmitterContext.cs 20 KB

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