aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Signal/TestMethods.cs
blob: 2d7cef166eea7077948696ca4583a74d16572b1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.Translation;
using System;

using static ARMeilleure.IntermediateRepresentation.Operand.Factory;

namespace ARMeilleure.Signal
{
    public struct NativeWriteLoopState
    {
        public int Running;
        public int Error;
    }

    public static class TestMethods
    {
        public delegate bool DebugPartialUnmap();
        public delegate int DebugThreadLocalMapGetOrReserve(int threadId, int initialState);
        public delegate void DebugNativeWriteLoop(IntPtr nativeWriteLoopPtr, IntPtr writePtr);

        public static DebugPartialUnmap GenerateDebugPartialUnmap()
        {
            EmitterContext context = new EmitterContext();

            var result = WindowsPartialUnmapHandler.EmitRetryFromAccessViolation(context);

            context.Return(result);

            // Compile and return the function.

            ControlFlowGraph cfg = context.GetControlFlowGraph();

            OperandType[] argTypes = new OperandType[] { OperandType.I64 };

            return Compiler.Compile(cfg, argTypes, OperandType.I32, CompilerOptions.HighCq).Map<DebugPartialUnmap>();
        }

        public static DebugThreadLocalMapGetOrReserve GenerateDebugThreadLocalMapGetOrReserve(IntPtr structPtr)
        {
            EmitterContext context = new EmitterContext();

            var result = WindowsPartialUnmapHandler.EmitThreadLocalMapIntGetOrReserve(context, structPtr, context.LoadArgument(OperandType.I32, 0), context.LoadArgument(OperandType.I32, 1));

            context.Return(result);

            // Compile and return the function.

            ControlFlowGraph cfg = context.GetControlFlowGraph();

            OperandType[] argTypes = new OperandType[] { OperandType.I64 };

            return Compiler.Compile(cfg, argTypes, OperandType.I32, CompilerOptions.HighCq).Map<DebugThreadLocalMapGetOrReserve>();
        }

        public static DebugNativeWriteLoop GenerateDebugNativeWriteLoop()
        {
            EmitterContext context = new EmitterContext();

            // Loop a write to the target address until "running" is false.

            Operand structPtr = context.Copy(context.LoadArgument(OperandType.I64, 0));
            Operand writePtr = context.Copy(context.LoadArgument(OperandType.I64, 1));

            Operand loopLabel = Label();
            context.MarkLabel(loopLabel);

            context.Store(writePtr, Const(12345));

            Operand running = context.Load(OperandType.I32, structPtr);

            context.BranchIfTrue(loopLabel, running);

            context.Return();

            // Compile and return the function.

            ControlFlowGraph cfg = context.GetControlFlowGraph();

            OperandType[] argTypes = new OperandType[] { OperandType.I64 };

            return Compiler.Compile(cfg, argTypes, OperandType.None, CompilerOptions.HighCq).Map<DebugNativeWriteLoop>();
        }
    }
}