EmitterContext.cs 18 KB

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