TestMethods.cs 2.9 KB

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