EmitterContext.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. public Operand Divide(Operand op1, Operand op2)
  165. {
  166. return Add(Instruction.Divide, Local(op1.Type), op1, op2);
  167. }
  168. public Operand DivideUI(Operand op1, Operand op2)
  169. {
  170. return Add(Instruction.DivideUI, Local(op1.Type), op1, op2);
  171. }
  172. public Operand ICompareEqual(Operand op1, Operand op2)
  173. {
  174. return Add(Instruction.CompareEqual, Local(OperandType.I32), op1, op2);
  175. }
  176. public Operand ICompareGreater(Operand op1, Operand op2)
  177. {
  178. return Add(Instruction.CompareGreater, Local(OperandType.I32), op1, op2);
  179. }
  180. public Operand ICompareGreaterOrEqual(Operand op1, Operand op2)
  181. {
  182. return Add(Instruction.CompareGreaterOrEqual, Local(OperandType.I32), op1, op2);
  183. }
  184. public Operand ICompareGreaterOrEqualUI(Operand op1, Operand op2)
  185. {
  186. return Add(Instruction.CompareGreaterOrEqualUI, Local(OperandType.I32), op1, op2);
  187. }
  188. public Operand ICompareGreaterUI(Operand op1, Operand op2)
  189. {
  190. return Add(Instruction.CompareGreaterUI, Local(OperandType.I32), op1, op2);
  191. }
  192. public Operand ICompareLess(Operand op1, Operand op2)
  193. {
  194. return Add(Instruction.CompareLess, Local(OperandType.I32), op1, op2);
  195. }
  196. public Operand ICompareLessOrEqual(Operand op1, Operand op2)
  197. {
  198. return Add(Instruction.CompareLessOrEqual, Local(OperandType.I32), op1, op2);
  199. }
  200. public Operand ICompareLessOrEqualUI(Operand op1, Operand op2)
  201. {
  202. return Add(Instruction.CompareLessOrEqualUI, Local(OperandType.I32), op1, op2);
  203. }
  204. public Operand ICompareLessUI(Operand op1, Operand op2)
  205. {
  206. return Add(Instruction.CompareLessUI, Local(OperandType.I32), op1, op2);
  207. }
  208. public Operand ICompareNotEqual(Operand op1, Operand op2)
  209. {
  210. return Add(Instruction.CompareNotEqual, Local(OperandType.I32), op1, op2);
  211. }
  212. public Operand Load(OperandType type, Operand address)
  213. {
  214. return Add(Instruction.Load, Local(type), address);
  215. }
  216. public Operand Load16(Operand address)
  217. {
  218. return Add(Instruction.Load16, Local(OperandType.I32), address);
  219. }
  220. public Operand Load8(Operand address)
  221. {
  222. return Add(Instruction.Load8, Local(OperandType.I32), address);
  223. }
  224. public Operand LoadArgument(OperandType type, int index)
  225. {
  226. return Add(Instruction.LoadArgument, Local(type), Const(index));
  227. }
  228. public void LoadFromContext()
  229. {
  230. _needsNewBlock = true;
  231. Add(Instruction.LoadFromContext);
  232. }
  233. public Operand Multiply(Operand op1, Operand op2)
  234. {
  235. return Add(Instruction.Multiply, Local(op1.Type), op1, op2);
  236. }
  237. public Operand Multiply64HighSI(Operand op1, Operand op2)
  238. {
  239. return Add(Instruction.Multiply64HighSI, Local(OperandType.I64), op1, op2);
  240. }
  241. public Operand Multiply64HighUI(Operand op1, Operand op2)
  242. {
  243. return Add(Instruction.Multiply64HighUI, Local(OperandType.I64), op1, op2);
  244. }
  245. public Operand Negate(Operand op1)
  246. {
  247. return Add(Instruction.Negate, Local(op1.Type), op1);
  248. }
  249. public void Return()
  250. {
  251. Add(Instruction.Return);
  252. _needsNewBlock = true;
  253. }
  254. public void Return(Operand op1)
  255. {
  256. Add(Instruction.Return, null, op1);
  257. _needsNewBlock = true;
  258. }
  259. public Operand RotateRight(Operand op1, Operand op2)
  260. {
  261. return Add(Instruction.RotateRight, Local(op1.Type), op1, op2);
  262. }
  263. public Operand ShiftLeft(Operand op1, Operand op2)
  264. {
  265. return Add(Instruction.ShiftLeft, Local(op1.Type), op1, op2);
  266. }
  267. public Operand ShiftRightSI(Operand op1, Operand op2)
  268. {
  269. return Add(Instruction.ShiftRightSI, Local(op1.Type), op1, op2);
  270. }
  271. public Operand ShiftRightUI(Operand op1, Operand op2)
  272. {
  273. return Add(Instruction.ShiftRightUI, Local(op1.Type), op1, op2);
  274. }
  275. public Operand SignExtend16(OperandType type, Operand op1)
  276. {
  277. return Add(Instruction.SignExtend16, Local(type), op1);
  278. }
  279. public Operand SignExtend32(OperandType type, Operand op1)
  280. {
  281. return Add(Instruction.SignExtend32, Local(type), op1);
  282. }
  283. public Operand SignExtend8(OperandType type, Operand op1)
  284. {
  285. return Add(Instruction.SignExtend8, Local(type), op1);
  286. }
  287. public void Store(Operand address, Operand value)
  288. {
  289. Add(Instruction.Store, null, address, value);
  290. }
  291. public void Store16(Operand address, Operand value)
  292. {
  293. Add(Instruction.Store16, null, address, value);
  294. }
  295. public void Store8(Operand address, Operand value)
  296. {
  297. Add(Instruction.Store8, null, address, value);
  298. }
  299. public void StoreToContext()
  300. {
  301. Add(Instruction.StoreToContext);
  302. _needsNewBlock = true;
  303. }
  304. public Operand Subtract(Operand op1, Operand op2)
  305. {
  306. return Add(Instruction.Subtract, Local(op1.Type), op1, op2);
  307. }
  308. public Operand VectorCreateScalar(Operand value)
  309. {
  310. return Add(Instruction.VectorCreateScalar, Local(OperandType.V128), value);
  311. }
  312. public Operand VectorExtract(OperandType type, Operand vector, int index)
  313. {
  314. return Add(Instruction.VectorExtract, Local(type), vector, Const(index));
  315. }
  316. public Operand VectorExtract16(Operand vector, int index)
  317. {
  318. return Add(Instruction.VectorExtract16, Local(OperandType.I32), vector, Const(index));
  319. }
  320. public Operand VectorExtract8(Operand vector, int index)
  321. {
  322. return Add(Instruction.VectorExtract8, Local(OperandType.I32), vector, Const(index));
  323. }
  324. public Operand VectorInsert(Operand vector, Operand value, int index)
  325. {
  326. return Add(Instruction.VectorInsert, Local(OperandType.V128), vector, value, Const(index));
  327. }
  328. public Operand VectorInsert16(Operand vector, Operand value, int index)
  329. {
  330. return Add(Instruction.VectorInsert16, Local(OperandType.V128), vector, value, Const(index));
  331. }
  332. public Operand VectorInsert8(Operand vector, Operand value, int index)
  333. {
  334. return Add(Instruction.VectorInsert8, Local(OperandType.V128), vector, value, Const(index));
  335. }
  336. public Operand VectorZero()
  337. {
  338. return Add(Instruction.VectorZero, Local(OperandType.V128));
  339. }
  340. public Operand VectorZeroUpper64(Operand vector)
  341. {
  342. return Add(Instruction.VectorZeroUpper64, Local(OperandType.V128), vector);
  343. }
  344. public Operand VectorZeroUpper96(Operand vector)
  345. {
  346. return Add(Instruction.VectorZeroUpper96, Local(OperandType.V128), vector);
  347. }
  348. public Operand ZeroExtend16(OperandType type, Operand op1)
  349. {
  350. return Add(Instruction.ZeroExtend16, Local(type), op1);
  351. }
  352. public Operand ZeroExtend32(OperandType type, Operand op1)
  353. {
  354. return Add(Instruction.ZeroExtend32, Local(type), op1);
  355. }
  356. public Operand ZeroExtend8(OperandType type, Operand op1)
  357. {
  358. return Add(Instruction.ZeroExtend8, Local(type), op1);
  359. }
  360. private void NewNextBlockIfNeeded()
  361. {
  362. if (_needsNewBlock)
  363. {
  364. NewNextBlock();
  365. }
  366. }
  367. private Operand Add(Instruction inst, Operand dest = null)
  368. {
  369. NewNextBlockIfNeeded();
  370. Operation operation = OperationHelper.Operation(inst, dest);
  371. _irBlock.Operations.AddLast(operation);
  372. return dest;
  373. }
  374. private Operand Add(Instruction inst, Operand dest, Operand[] sources)
  375. {
  376. NewNextBlockIfNeeded();
  377. Operation operation = OperationHelper.Operation(inst, dest, sources);
  378. _irBlock.Operations.AddLast(operation);
  379. return dest;
  380. }
  381. private Operand Add(Instruction inst, Operand dest, Operand source0)
  382. {
  383. NewNextBlockIfNeeded();
  384. Operation operation = OperationHelper.Operation(inst, dest, source0);
  385. _irBlock.Operations.AddLast(operation);
  386. return dest;
  387. }
  388. private Operand Add(Instruction inst, Operand dest, Operand source0, Operand source1)
  389. {
  390. NewNextBlockIfNeeded();
  391. Operation operation = OperationHelper.Operation(inst, dest, source0, source1);
  392. _irBlock.Operations.AddLast(operation);
  393. return dest;
  394. }
  395. private Operand Add(Instruction inst, Operand dest, Operand source0, Operand source1, Operand source2)
  396. {
  397. NewNextBlockIfNeeded();
  398. Operation operation = OperationHelper.Operation(inst, dest, source0, source1, source2);
  399. _irBlock.Operations.AddLast(operation);
  400. return dest;
  401. }
  402. public Operand AddIntrinsic(Intrinsic intrin, params Operand[] args)
  403. {
  404. return Add(intrin, Local(OperandType.V128), args);
  405. }
  406. public Operand AddIntrinsicInt(Intrinsic intrin, params Operand[] args)
  407. {
  408. return Add(intrin, Local(OperandType.I32), args);
  409. }
  410. public Operand AddIntrinsicLong(Intrinsic intrin, params Operand[] args)
  411. {
  412. return Add(intrin, Local(OperandType.I64), args);
  413. }
  414. private Operand Add(Intrinsic intrin, Operand dest, params Operand[] sources)
  415. {
  416. if (_needsNewBlock)
  417. {
  418. NewNextBlock();
  419. }
  420. IntrinsicOperation operation = new IntrinsicOperation(intrin, dest, sources);
  421. _irBlock.Operations.AddLast(operation);
  422. return dest;
  423. }
  424. private void BranchToLabel(Operand label)
  425. {
  426. if (!_irLabels.TryGetValue(label, out BasicBlock branchBlock))
  427. {
  428. branchBlock = new BasicBlock();
  429. _irLabels.Add(label, branchBlock);
  430. }
  431. _irBlock.Branch = branchBlock;
  432. _needsNewBlock = true;
  433. }
  434. public void MarkLabel(Operand label)
  435. {
  436. if (_irLabels.TryGetValue(label, out BasicBlock nextBlock))
  437. {
  438. nextBlock.Index = _irBlocks.Count;
  439. _irBlocks.AddLast(nextBlock);
  440. NextBlock(nextBlock);
  441. }
  442. else
  443. {
  444. NewNextBlock();
  445. _irLabels.Add(label, _irBlock);
  446. }
  447. }
  448. private void NewNextBlock()
  449. {
  450. BasicBlock block = new BasicBlock(_irBlocks.Count);
  451. _irBlocks.AddLast(block);
  452. NextBlock(block);
  453. }
  454. private void NextBlock(BasicBlock nextBlock)
  455. {
  456. if (_irBlock != null && !EndsWithUnconditional(_irBlock))
  457. {
  458. _irBlock.Next = nextBlock;
  459. }
  460. _irBlock = nextBlock;
  461. _needsNewBlock = false;
  462. }
  463. private static bool EndsWithUnconditional(BasicBlock block)
  464. {
  465. return block.Operations.Last is Operation lastOp &&
  466. (lastOp.Instruction == Instruction.Branch ||
  467. lastOp.Instruction == Instruction.Return ||
  468. lastOp.Instruction == Instruction.Tailcall);
  469. }
  470. public ControlFlowGraph GetControlFlowGraph()
  471. {
  472. return new ControlFlowGraph(_irBlocks.First, _irBlocks);
  473. }
  474. }
  475. }