MemoryManagerPal.cs 2.2 KB

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