EmitterContext.cs 20 KB

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