EmitterContext.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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.OperandHelper;
  8. namespace ARMeilleure.Translation
  9. {
  10. using PTC;
  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. public EmitterContext()
  19. {
  20. _irLabels = new Dictionary<Operand, BasicBlock>();
  21. _irBlocks = new IntrusiveList<BasicBlock>();
  22. _needsNewBlock = true;
  23. }
  24. public Operand Add(Operand op1, Operand op2)
  25. {
  26. return Add(Instruction.Add, Local(op1.Type), op1, op2);
  27. }
  28. public Operand BitwiseAnd(Operand op1, Operand op2)
  29. {
  30. return Add(Instruction.BitwiseAnd, Local(op1.Type), op1, op2);
  31. }
  32. public Operand BitwiseExclusiveOr(Operand op1, Operand op2)
  33. {
  34. return Add(Instruction.BitwiseExclusiveOr, Local(op1.Type), op1, op2);
  35. }
  36. public Operand BitwiseNot(Operand op1)
  37. {
  38. return Add(Instruction.BitwiseNot, Local(op1.Type), op1);
  39. }
  40. public Operand BitwiseOr(Operand op1, Operand op2)
  41. {
  42. return Add(Instruction.BitwiseOr, Local(op1.Type), op1, op2);
  43. }
  44. public void Branch(Operand label)
  45. {
  46. NewNextBlockIfNeeded();
  47. BranchToLabel(label, uncond: true);
  48. }
  49. public void BranchIf(Operand label, Operand op1, Operand op2, Comparison comp)
  50. {
  51. Add(Instruction.BranchIf, null, op1, op2, Const((int)comp));
  52. BranchToLabel(label, uncond: false);
  53. }
  54. public void BranchIfFalse(Operand label, Operand op1)
  55. {
  56. BranchIf(label, op1, Const(op1.Type, 0), Comparison.Equal);
  57. }
  58. public void BranchIfTrue(Operand label, Operand op1)
  59. {
  60. BranchIf(label, op1, Const(op1.Type, 0), Comparison.NotEqual);
  61. }
  62. public Operand ByteSwap(Operand op1)
  63. {
  64. return Add(Instruction.ByteSwap, Local(op1.Type), op1);
  65. }
  66. public Operand Call(MethodInfo info, params Operand[] callArgs)
  67. {
  68. if (Ptc.State == PtcState.Disabled)
  69. {
  70. IntPtr funcPtr = Delegates.GetDelegateFuncPtr(info);
  71. OperandType returnType = GetOperandType(info.ReturnType);
  72. Symbols.Add((ulong)funcPtr.ToInt64(), info.Name);
  73. return Call(Const(funcPtr.ToInt64()), returnType, callArgs);
  74. }
  75. else
  76. {
  77. int index = Delegates.GetDelegateIndex(info);
  78. IntPtr funcPtr = Delegates.GetDelegateFuncPtrByIndex(index);
  79. OperandType returnType = GetOperandType(info.ReturnType);
  80. Symbols.Add((ulong)funcPtr.ToInt64(), info.Name);
  81. return Call(Const(funcPtr.ToInt64(), true, index), returnType, callArgs);
  82. }
  83. }
  84. private static OperandType GetOperandType(Type type)
  85. {
  86. if (type == typeof(bool) || type == typeof(byte) ||
  87. type == typeof(char) || type == typeof(short) ||
  88. type == typeof(int) || type == typeof(sbyte) ||
  89. type == typeof(ushort) || type == typeof(uint))
  90. {
  91. return OperandType.I32;
  92. }
  93. else if (type == typeof(long) || type == typeof(ulong))
  94. {
  95. return OperandType.I64;
  96. }
  97. else if (type == typeof(double))
  98. {
  99. return OperandType.FP64;
  100. }
  101. else if (type == typeof(float))
  102. {
  103. return OperandType.FP32;
  104. }
  105. else if (type == typeof(V128))
  106. {
  107. return OperandType.V128;
  108. }
  109. else if (type == typeof(void))
  110. {
  111. return OperandType.None;
  112. }
  113. else
  114. {
  115. throw new ArgumentException($"Invalid type \"{type.Name}\".");
  116. }
  117. }
  118. public Operand Call(Operand address, OperandType returnType, params Operand[] callArgs)
  119. {
  120. Operand[] args = new Operand[callArgs.Length + 1];
  121. args[0] = address;
  122. Array.Copy(callArgs, 0, args, 1, callArgs.Length);
  123. if (returnType != OperandType.None)
  124. {
  125. return Add(Instruction.Call, Local(returnType), args);
  126. }
  127. else
  128. {
  129. return Add(Instruction.Call, null, args);
  130. }
  131. }
  132. public void Tailcall(Operand address, params Operand[] callArgs)
  133. {
  134. Operand[] args = new Operand[callArgs.Length + 1];
  135. args[0] = address;
  136. Array.Copy(callArgs, 0, args, 1, callArgs.Length);
  137. Add(Instruction.Tailcall, null, args);
  138. _needsNewBlock = true;
  139. }
  140. public Operand CompareAndSwap(Operand address, Operand expected, Operand desired)
  141. {
  142. return Add(Instruction.CompareAndSwap, Local(desired.Type), address, expected, desired);
  143. }
  144. public Operand CompareAndSwap16(Operand address, Operand expected, Operand desired)
  145. {
  146. return Add(Instruction.CompareAndSwap16, Local(OperandType.I32), address, expected, desired);
  147. }
  148. public Operand CompareAndSwap8(Operand address, Operand expected, Operand desired)
  149. {
  150. return Add(Instruction.CompareAndSwap8, Local(OperandType.I32), address, expected, desired);
  151. }
  152. public Operand ConditionalSelect(Operand op1, Operand op2, Operand op3)
  153. {
  154. return Add(Instruction.ConditionalSelect, Local(op2.Type), op1, op2, op3);
  155. }
  156. public Operand ConvertI64ToI32(Operand op1)
  157. {
  158. if (op1.Type != OperandType.I64)
  159. {
  160. throw new ArgumentException($"Invalid operand type \"{op1.Type}\".");
  161. }
  162. return Add(Instruction.ConvertI64ToI32, Local(OperandType.I32), op1);
  163. }
  164. public Operand ConvertToFP(OperandType type, Operand op1)
  165. {
  166. return Add(Instruction.ConvertToFP, Local(type), op1);
  167. }
  168. public Operand ConvertToFPUI(OperandType type, Operand op1)
  169. {
  170. return Add(Instruction.ConvertToFPUI, Local(type), op1);
  171. }
  172. public Operand Copy(Operand op1)
  173. {
  174. return Add(Instruction.Copy, Local(op1.Type), op1);
  175. }
  176. public Operand Copy(Operand dest, Operand op1)
  177. {
  178. if (dest.Kind != OperandKind.Register)
  179. {
  180. throw new ArgumentException($"Invalid dest operand kind \"{dest.Kind}\".");
  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. private Operand Add(Intrinsic intrin, Operand dest, params Operand[] sources)
  447. {
  448. NewNextBlockIfNeeded();
  449. IntrinsicOperation operation = new IntrinsicOperation(intrin, dest, sources);
  450. _irBlock.Operations.AddLast(operation);
  451. return dest;
  452. }
  453. private void BranchToLabel(Operand label, bool uncond)
  454. {
  455. if (!_irLabels.TryGetValue(label, out BasicBlock branchBlock))
  456. {
  457. branchBlock = new BasicBlock();
  458. _irLabels.Add(label, branchBlock);
  459. }
  460. if (uncond)
  461. {
  462. _irBlock.AddSuccessor(branchBlock);
  463. }
  464. else
  465. {
  466. // Defer registration of successor to _irBlock so that the order of successors is correct.
  467. _ifBlock = branchBlock;
  468. }
  469. _needsNewBlock = true;
  470. }
  471. public void MarkLabel(Operand label)
  472. {
  473. if (_irLabels.TryGetValue(label, out BasicBlock nextBlock))
  474. {
  475. nextBlock.Index = _irBlocks.Count;
  476. _irBlocks.AddLast(nextBlock);
  477. NextBlock(nextBlock);
  478. }
  479. else
  480. {
  481. NewNextBlock();
  482. _irLabels.Add(label, _irBlock);
  483. }
  484. }
  485. private void NewNextBlock()
  486. {
  487. BasicBlock block = new BasicBlock(_irBlocks.Count);
  488. _irBlocks.AddLast(block);
  489. NextBlock(block);
  490. }
  491. private void NextBlock(BasicBlock nextBlock)
  492. {
  493. if (_irBlock != null && _irBlock.SuccessorCount == 0 && !EndsWithUnconditional(_irBlock))
  494. {
  495. _irBlock.AddSuccessor(nextBlock);
  496. if (_ifBlock != null)
  497. {
  498. _irBlock.AddSuccessor(_ifBlock);
  499. _ifBlock = null;
  500. }
  501. }
  502. _irBlock = nextBlock;
  503. _needsNewBlock = false;
  504. }
  505. private static bool EndsWithUnconditional(BasicBlock block)
  506. {
  507. return block.Operations.Last is Operation lastOp &&
  508. (lastOp.Instruction == Instruction.Return ||
  509. lastOp.Instruction == Instruction.Tailcall);
  510. }
  511. public ControlFlowGraph GetControlFlowGraph()
  512. {
  513. return new ControlFlowGraph(_irBlocks.First, _irBlocks);
  514. }
  515. }
  516. }