EmitterContext.cs 17 KB

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