aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Tamper/CompilationContext.cs
blob: 45a47f445932ed4b7636e0cf2dcd44da8518750d (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
using Ryujinx.HLE.HOS.Tamper.Operations;
using System.Collections.Generic;

namespace Ryujinx.HLE.HOS.Tamper
{
    class CompilationContext
    {
        public OperationBlock CurrentBlock => BlockStack.Peek();
        public List<IOperation> CurrentOperations => CurrentBlock.Operations;

        public ITamperedProcess Process { get; }
        public Parameter<long> PressedKeys { get; }
        public Stack<OperationBlock> BlockStack { get; }
        public Dictionary<byte, Register> Registers { get; }
        public Dictionary<byte, Register> SavedRegisters { get; }
        public Dictionary<byte, Register> StaticRegisters { get; }
        public ulong ExeAddress { get; }
        public ulong HeapAddress { get; }
        public ulong AliasAddress { get; }
        public ulong AslrAddress { get; }

        public CompilationContext(ulong exeAddress, ulong heapAddress, ulong aliasAddress, ulong aslrAddress, ITamperedProcess process)
        {
            Process         = process;
            PressedKeys     = new Parameter<long>(0);
            BlockStack      = new Stack<OperationBlock>();
            Registers       = new Dictionary<byte, Register>();
            SavedRegisters  = new Dictionary<byte, Register>();
            StaticRegisters = new Dictionary<byte, Register>();
            ExeAddress      = exeAddress;
            HeapAddress     = heapAddress;
            AliasAddress    = aliasAddress;
            AslrAddress     = aslrAddress;
        }

        public Register GetRegister(byte index)
        {
            if (Registers.TryGetValue(index, out Register register))
            {
                return register;
            }

            register = new Register($"R_{index:X2}");
            Registers.Add(index, register);

            return register;
        }

        public Register GetSavedRegister(byte index)
        {
            if (SavedRegisters.TryGetValue(index, out Register register))
            {
                return register;
            }

            register = new Register($"S_{index:X2}");
            SavedRegisters.Add(index, register);

            return register;
        }

        public Register GetStaticRegister(byte index)
        {
            if (SavedRegisters.TryGetValue(index, out Register register))
            {
                return register;
            }

            register = new Register($"T_{index:X2}");
            SavedRegisters.Add(index, register);

            return register;
        }
    }
}