EmitterContext.cs 18 KB

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