MemoryManagerPal.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using ARMeilleure.IntermediateRepresentation;
  2. using ARMeilleure.State;
  3. using ARMeilleure.Translation;
  4. namespace ARMeilleure.Memory
  5. {
  6. static class MemoryManagerPal
  7. {
  8. private delegate V128 CompareExchange128(ref V128 location, V128 expected, V128 desired);
  9. private static CompareExchange128 _compareExchange128;
  10. private static object _lock;
  11. static MemoryManagerPal()
  12. {
  13. _lock = new object();
  14. }
  15. public static V128 AtomicLoad128(ref V128 location)
  16. {
  17. return GetCompareAndSwap128()(ref location, V128.Zero, V128.Zero);
  18. }
  19. public static V128 CompareAndSwap128(ref V128 location, V128 expected, V128 desired)
  20. {
  21. return GetCompareAndSwap128()(ref location, expected, desired);
  22. }
  23. private static CompareExchange128 GetCompareAndSwap128()
  24. {
  25. if (_compareExchange128 == null)
  26. {
  27. GenerateCompareAndSwap128();
  28. }
  29. return _compareExchange128;
  30. }
  31. private static void GenerateCompareAndSwap128()
  32. {
  33. lock (_lock)
  34. {
  35. if (_compareExchange128 != null)
  36. {
  37. return;
  38. }
  39. EmitterContext context = new EmitterContext();
  40. Operand address = context.LoadArgument(OperandType.I64, 0);
  41. Operand expected = context.LoadArgument(OperandType.V128, 1);
  42. Operand desired = context.LoadArgument(OperandType.V128, 2);
  43. Operand result = context.CompareAndSwap(address, expected, desired);
  44. context.Return(result);
  45. ControlFlowGraph cfg = context.GetControlFlowGraph();
  46. OperandType[] argTypes = new OperandType[]
  47. {
  48. OperandType.I64,
  49. OperandType.V128,
  50. OperandType.V128
  51. };
  52. _compareExchange128 = Compiler.Compile<CompareExchange128>(
  53. cfg,
  54. argTypes,
  55. OperandType.V128,
  56. CompilerOptions.HighCq);
  57. }
  58. }
  59. }
  60. }