TestMethods.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using ARMeilleure.IntermediateRepresentation;
  2. using ARMeilleure.Translation;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  6. namespace ARMeilleure.Signal
  7. {
  8. public struct NativeWriteLoopState
  9. {
  10. public int Running;
  11. public int Error;
  12. }
  13. public static class TestMethods
  14. {
  15. public delegate bool DebugPartialUnmap();
  16. public delegate int DebugThreadLocalMapGetOrReserve(int threadId, int initialState);
  17. public delegate void DebugNativeWriteLoop(IntPtr nativeWriteLoopPtr, IntPtr writePtr);
  18. public static DebugPartialUnmap GenerateDebugPartialUnmap()
  19. {
  20. EmitterContext context = new EmitterContext();
  21. var result = WindowsPartialUnmapHandler.EmitRetryFromAccessViolation(context);
  22. context.Return(result);
  23. // Compile and return the function.
  24. ControlFlowGraph cfg = context.GetControlFlowGraph();
  25. OperandType[] argTypes = new OperandType[] { OperandType.I64 };
  26. return Compiler.Compile(cfg, argTypes, OperandType.I32, CompilerOptions.HighCq, RuntimeInformation.ProcessArchitecture).Map<DebugPartialUnmap>();
  27. }
  28. public static DebugThreadLocalMapGetOrReserve GenerateDebugThreadLocalMapGetOrReserve(IntPtr structPtr)
  29. {
  30. EmitterContext context = new EmitterContext();
  31. var result = WindowsPartialUnmapHandler.EmitThreadLocalMapIntGetOrReserve(context, structPtr, context.LoadArgument(OperandType.I32, 0), context.LoadArgument(OperandType.I32, 1));
  32. context.Return(result);
  33. // Compile and return the function.
  34. ControlFlowGraph cfg = context.GetControlFlowGraph();
  35. OperandType[] argTypes = new OperandType[] { OperandType.I64 };
  36. return Compiler.Compile(cfg, argTypes, OperandType.I32, CompilerOptions.HighCq, RuntimeInformation.ProcessArchitecture).Map<DebugThreadLocalMapGetOrReserve>();
  37. }
  38. public static DebugNativeWriteLoop GenerateDebugNativeWriteLoop()
  39. {
  40. EmitterContext context = new EmitterContext();
  41. // Loop a write to the target address until "running" is false.
  42. Operand structPtr = context.Copy(context.LoadArgument(OperandType.I64, 0));
  43. Operand writePtr = context.Copy(context.LoadArgument(OperandType.I64, 1));
  44. Operand loopLabel = Label();
  45. context.MarkLabel(loopLabel);
  46. context.Store(writePtr, Const(12345));
  47. Operand running = context.Load(OperandType.I32, structPtr);
  48. context.BranchIfTrue(loopLabel, running);
  49. context.Return();
  50. // Compile and return the function.
  51. ControlFlowGraph cfg = context.GetControlFlowGraph();
  52. OperandType[] argTypes = new OperandType[] { OperandType.I64 };
  53. return Compiler.Compile(cfg, argTypes, OperandType.None, CompilerOptions.HighCq, RuntimeInformation.ProcessArchitecture).Map<DebugNativeWriteLoop>();
  54. }
  55. }
  56. }