EmitterContext.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 Dictionary<Operand, BasicBlock> _irLabels;
  14. private IntrusiveList<BasicBlock> _irBlocks;
  15. private BasicBlock _irBlock;
  16. private bool _needsNewBlock;
  17. public EmitterContext()
  18. {
  19. _irLabels = new Dictionary<Operand, BasicBlock>();
  20. _irBlocks = new IntrusiveList<BasicBlock>();
  21. _needsNewBlock = true;
  22. }
  23. public Operand Add(Operand op1, Operand op2)
  24. {
  25. return Add(Instruction.Add, Local(op1.Type), op1, op2);
  26. }
  27. public Operand BitwiseAnd(Operand op1, Operand op2)
  28. {
  29. return Add(Instruction.BitwiseAnd, Local(op1.Type), op1, op2);
  30. }
  31. public Operand BitwiseExclusiveOr(Operand op1, Operand op2)
  32. {
  33. return Add(Instruction.BitwiseExclusiveOr, Local(op1.Type), op1, op2);
  34. }
  35. public Operand BitwiseNot(Operand op1)
  36. {
  37. return Add(Instruction.BitwiseNot, Local(op1.Type), op1);
  38. }
  39. public Operand BitwiseOr(Operand op1, Operand op2)
  40. {
  41. return Add(Instruction.BitwiseOr, Local(op1.Type), op1, op2);
  42. }
  43. public void Branch(Operand label)
  44. {
  45. Add(Instruction.Branch, null);
  46. BranchToLabel(label);
  47. }
  48. public void BranchIf(Operand label, Operand op1, Operand op2, Comparison comp)
  49. {
  50. Add(Instruction.BranchIf, null, op1, op2, Const((int)comp));
  51. BranchToLabel(label);
  52. }
  53. public void BranchIfFalse(Operand label, Operand op1)
  54. {
  55. BranchIf(label, op1, Const(op1.Type, 0), Comparison.Equal);
  56. }
  57. public void BranchIfTrue(Operand label, Operand op1)
  58. {
  59. BranchIf(label, op1, Const(op1.Type, 0), Comparison.NotEqual);
  60. }
  61. public Operand ByteSwap(Operand op1)
  62. {
  63. return Add(Instruction.ByteSwap, Local(op1.Type), op1);
  64. }
  65. public Operand Call(MethodInfo info, params Operand[] callArgs)
  66. {
  67. if (Ptc.State == PtcState.Disabled)
  68. {
  69. IntPtr funcPtr = Delegates.GetDelegateFuncPtr(info);
  70. OperandType returnType = GetOperandType(info.ReturnType);
  71. Symbols.Add((ulong)funcPtr.ToInt64(), info.Name);
  72. return Call(Const(funcPtr.ToInt64()), returnType, callArgs);
  73. }
  74. else
  75. {
  76. int index = Delegates.GetDelegateIndex(info);
  77. IntPtr funcPtr = Delegates.GetDelegateFuncPtrByIndex(index);
  78. OperandType returnType = GetOperandType(info.ReturnType);
  79. Symbols.Add((ulong)funcPtr.ToInt64(), info.Name);
  80. return Call(Const(funcPtr.ToInt64(), true, index), returnType, callArgs);
  81. }
  82. }
  83. private 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. {
  179. throw new ArgumentException($"Invalid dest operand kind \"{dest.Kind}\".");
  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, null, 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, null, address, value);
  317. }
  318. public void Store16(Operand address, Operand value)
  319. {
  320. Add(Instruction.Store16, null, address, value);
  321. }
  322. public void Store8(Operand address, Operand value)
  323. {
  324. Add(Instruction.Store8, null, 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 = null)
  399. {
  400. NewNextBlockIfNeeded();
  401. Operation operation = OperationHelper.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 = OperationHelper.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 = OperationHelper.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 = OperationHelper.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 = OperationHelper.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. private Operand Add(Intrinsic intrin, Operand dest, params Operand[] sources)
  446. {
  447. if (_needsNewBlock)
  448. {
  449. NewNextBlock();
  450. }
  451. IntrinsicOperation operation = new IntrinsicOperation(intrin, dest, sources);
  452. _irBlock.Operations.AddLast(operation);
  453. return dest;
  454. }
  455. private void BranchToLabel(Operand label)
  456. {
  457. if (!_irLabels.TryGetValue(label, out BasicBlock branchBlock))
  458. {
  459. branchBlock = new BasicBlock();
  460. _irLabels.Add(label, branchBlock);
  461. }
  462. _irBlock.Branch = branchBlock;
  463. _needsNewBlock = true;
  464. }
  465. public void MarkLabel(Operand label)
  466. {
  467. if (_irLabels.TryGetValue(label, out BasicBlock nextBlock))
  468. {
  469. nextBlock.Index = _irBlocks.Count;
  470. _irBlocks.AddLast(nextBlock);
  471. NextBlock(nextBlock);
  472. }
  473. else
  474. {
  475. NewNextBlock();
  476. _irLabels.Add(label, _irBlock);
  477. }
  478. }
  479. private void NewNextBlock()
  480. {
  481. BasicBlock block = new BasicBlock(_irBlocks.Count);
  482. _irBlocks.AddLast(block);
  483. NextBlock(block);
  484. }
  485. private void NextBlock(BasicBlock nextBlock)
  486. {
  487. if (_irBlock != null && !EndsWithUnconditional(_irBlock))
  488. {
  489. _irBlock.Next = nextBlock;
  490. }
  491. _irBlock = nextBlock;
  492. _needsNewBlock = false;
  493. }
  494. private static bool EndsWithUnconditional(BasicBlock block)
  495. {
  496. return block.Operations.Last is Operation lastOp &&
  497. (lastOp.Instruction == Instruction.Branch ||
  498. lastOp.Instruction == Instruction.Return ||
  499. lastOp.Instruction == Instruction.Tailcall);
  500. }
  501. public ControlFlowGraph GetControlFlowGraph()
  502. {
  503. return new ControlFlowGraph(_irBlocks.First, _irBlocks);
  504. }
  505. }
  506. }