ArmEmitterContext.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using ARMeilleure.CodeGen.Linking;
  2. using ARMeilleure.Common;
  3. using ARMeilleure.Decoders;
  4. using ARMeilleure.Diagnostics;
  5. using ARMeilleure.Instructions;
  6. using ARMeilleure.IntermediateRepresentation;
  7. using ARMeilleure.Memory;
  8. using ARMeilleure.State;
  9. using ARMeilleure.Translation.PTC;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Reflection;
  13. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  14. namespace ARMeilleure.Translation
  15. {
  16. class ArmEmitterContext : EmitterContext
  17. {
  18. private readonly Dictionary<ulong, Operand> _labels;
  19. private OpCode _optOpLastCompare;
  20. private OpCode _optOpLastFlagSet;
  21. private Operand _optCmpTempN;
  22. private Operand _optCmpTempM;
  23. private Block _currBlock;
  24. public Block CurrBlock
  25. {
  26. get
  27. {
  28. return _currBlock;
  29. }
  30. set
  31. {
  32. _currBlock = value;
  33. ResetBlockState();
  34. }
  35. }
  36. public OpCode CurrOp { get; set; }
  37. public IMemoryManager Memory { get; }
  38. public bool HasPtc { get; }
  39. public EntryTable<uint> CountTable { get; }
  40. public AddressTable<ulong> FunctionTable { get; }
  41. public TranslatorStubs Stubs { get; }
  42. public ulong EntryAddress { get; }
  43. public bool HighCq { get; }
  44. public Aarch32Mode Mode { get; }
  45. public ArmEmitterContext(
  46. IMemoryManager memory,
  47. EntryTable<uint> countTable,
  48. AddressTable<ulong> funcTable,
  49. TranslatorStubs stubs,
  50. ulong entryAddress,
  51. bool highCq,
  52. Aarch32Mode mode)
  53. {
  54. HasPtc = Ptc.State != PtcState.Disabled;
  55. Memory = memory;
  56. CountTable = countTable;
  57. FunctionTable = funcTable;
  58. Stubs = stubs;
  59. EntryAddress = entryAddress;
  60. HighCq = highCq;
  61. Mode = mode;
  62. _labels = new Dictionary<ulong, Operand>();
  63. }
  64. public override Operand Call(MethodInfo info, params Operand[] callArgs)
  65. {
  66. if (!HasPtc)
  67. {
  68. return base.Call(info, callArgs);
  69. }
  70. else
  71. {
  72. int index = Delegates.GetDelegateIndex(info);
  73. IntPtr funcPtr = Delegates.GetDelegateFuncPtrByIndex(index);
  74. OperandType returnType = GetOperandType(info.ReturnType);
  75. Symbol symbol = new Symbol(SymbolType.DelegateTable, (ulong)index);
  76. Symbols.Add((ulong)funcPtr.ToInt64(), info.Name);
  77. return Call(Const(funcPtr.ToInt64(), symbol), returnType, callArgs);
  78. }
  79. }
  80. public Operand GetLabel(ulong address)
  81. {
  82. if (!_labels.TryGetValue(address, out Operand label))
  83. {
  84. label = Label();
  85. _labels.Add(address, label);
  86. }
  87. return label;
  88. }
  89. public void MarkComparison(Operand n, Operand m)
  90. {
  91. _optOpLastCompare = CurrOp;
  92. _optCmpTempN = Copy(n);
  93. _optCmpTempM = Copy(m);
  94. }
  95. public void MarkFlagSet(PState stateFlag)
  96. {
  97. // Set this only if any of the NZCV flag bits were modified.
  98. // This is used to ensure that when emiting a direct IL branch
  99. // instruction for compare + branch sequences, we're not expecting
  100. // to use comparison values from an old instruction, when in fact
  101. // the flags were already overwritten by another instruction further along.
  102. if (stateFlag >= PState.VFlag)
  103. {
  104. _optOpLastFlagSet = CurrOp;
  105. }
  106. }
  107. private void ResetBlockState()
  108. {
  109. _optOpLastCompare = null;
  110. _optOpLastFlagSet = null;
  111. }
  112. public Operand TryGetComparisonResult(Condition condition)
  113. {
  114. if (_optOpLastCompare == null || _optOpLastCompare != _optOpLastFlagSet)
  115. {
  116. return default;
  117. }
  118. Operand n = _optCmpTempN;
  119. Operand m = _optCmpTempM;
  120. InstName cmpName = _optOpLastCompare.Instruction.Name;
  121. if (cmpName == InstName.Subs)
  122. {
  123. switch (condition)
  124. {
  125. case Condition.Eq: return ICompareEqual (n, m);
  126. case Condition.Ne: return ICompareNotEqual (n, m);
  127. case Condition.GeUn: return ICompareGreaterOrEqualUI(n, m);
  128. case Condition.LtUn: return ICompareLessUI (n, m);
  129. case Condition.GtUn: return ICompareGreaterUI (n, m);
  130. case Condition.LeUn: return ICompareLessOrEqualUI (n, m);
  131. case Condition.Ge: return ICompareGreaterOrEqual (n, m);
  132. case Condition.Lt: return ICompareLess (n, m);
  133. case Condition.Gt: return ICompareGreater (n, m);
  134. case Condition.Le: return ICompareLessOrEqual (n, m);
  135. }
  136. }
  137. else if (cmpName == InstName.Adds && _optOpLastCompare is IOpCodeAluImm op)
  138. {
  139. // There are several limitations that needs to be taken into account for CMN comparisons:
  140. // - The unsigned comparisons are not valid, as they depend on the
  141. // carry flag value, and they will have different values for addition and
  142. // subtraction. For addition, it's carry, and for subtraction, it's borrow.
  143. // So, we need to make sure we're not doing a unsigned compare for the CMN case.
  144. // - We can only do the optimization for the immediate variants,
  145. // because when the second operand value is exactly INT_MIN, we can't
  146. // negate the value as theres no positive counterpart.
  147. // Such invalid values can't be encoded on the immediate encodings.
  148. if (op.RegisterSize == RegisterSize.Int32)
  149. {
  150. m = Const((int)-op.Immediate);
  151. }
  152. else
  153. {
  154. m = Const(-op.Immediate);
  155. }
  156. switch (condition)
  157. {
  158. case Condition.Eq: return ICompareEqual (n, m);
  159. case Condition.Ne: return ICompareNotEqual (n, m);
  160. case Condition.Ge: return ICompareGreaterOrEqual(n, m);
  161. case Condition.Lt: return ICompareLess (n, m);
  162. case Condition.Gt: return ICompareGreater (n, m);
  163. case Condition.Le: return ICompareLessOrEqual (n, m);
  164. }
  165. }
  166. return default;
  167. }
  168. }
  169. }