aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/ATranslator.cs
blob: 5be41d3ef76b97eedd1f31dde7a51c6ca6da2c44 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using ChocolArm64.Decoder;
using ChocolArm64.Events;
using ChocolArm64.Memory;
using ChocolArm64.State;
using ChocolArm64.Translation;
using System;
using System.Reflection.Emit;

namespace ChocolArm64
{
    public class ATranslator
    {
        private ATranslatorCache Cache;

        public event EventHandler<ACpuTraceEventArgs> CpuTrace;

        public bool EnableCpuTrace { get; set; }

        public ATranslator()
        {
            Cache = new ATranslatorCache();
        }

        internal void ExecuteSubroutine(AThread Thread, long Position)
        {
            //TODO: Both the execute A32/A64 methods should be merged on the future,
            //when both ISAs are implemented with the interpreter and JIT.
            //As of now, A32 only has a interpreter and A64 a JIT.
            AThreadState State  = Thread.ThreadState;
            AMemory      Memory = Thread.Memory;

            if (State.ExecutionMode == AExecutionMode.AArch32)
            {
                ExecuteSubroutineA32(State, Memory);
            }
            else
            {
                ExecuteSubroutineA64(State, Memory, Position);
            }
        }

        private void ExecuteSubroutineA32(AThreadState State, AMemory Memory)
        {
            do
            {
                AOpCode OpCode = ADecoder.DecodeOpCode(State, Memory, State.R15);

                OpCode.Interpreter(State, Memory, OpCode);
            }
            while (State.R15 != 0 && State.Running);
        }

        private void ExecuteSubroutineA64(AThreadState State, AMemory Memory, long Position)
        {
            do
            {
                if (EnableCpuTrace)
                {
                    CpuTrace?.Invoke(this, new ACpuTraceEventArgs(Position));
                }

                if (!Cache.TryGetSubroutine(Position, out ATranslatedSub Sub))
                {
                    Sub = TranslateTier0(State, Memory, Position);
                }

                if (Sub.ShouldReJit())
                {
                    TranslateTier1(State, Memory, Position);
                }

                Position = Sub.Execute(State, Memory);
            }
            while (Position != 0 && State.Running);
        }

        internal bool HasCachedSub(long Position)
        {
            return Cache.HasSubroutine(Position);
        }

        private ATranslatedSub TranslateTier0(AThreadState State, AMemory Memory, long Position)
        {
            ABlock Block = ADecoder.DecodeBasicBlock(State, Memory, Position);

            ABlock[] Graph = new ABlock[] { Block };

            string SubName = GetSubroutineName(Position);

            AILEmitterCtx Context = new AILEmitterCtx(Cache, Graph, Block, SubName);

            do
            {
                Context.EmitOpCode();
            }
            while (Context.AdvanceOpCode());

            ATranslatedSub Subroutine = Context.GetSubroutine();

            Subroutine.SetType(ATranslatedSubType.SubTier0);

            Cache.AddOrUpdate(Position, Subroutine, Block.OpCodes.Count);

            AOpCode LastOp = Block.GetLastOp();

            return Subroutine;
        }

        private void TranslateTier1(AThreadState State, AMemory Memory, long Position)
        {
            (ABlock[] Graph, ABlock Root) = ADecoder.DecodeSubroutine(Cache, State, Memory, Position);

            string SubName = GetSubroutineName(Position);

            AILEmitterCtx Context = new AILEmitterCtx(Cache, Graph, Root, SubName);

            if (Context.CurrBlock.Position != Position)
            {
                Context.Emit(OpCodes.Br, Context.GetLabel(Position));
            }

            do
            {
                Context.EmitOpCode();
            }
            while (Context.AdvanceOpCode());

            //Mark all methods that calls this method for ReJiting,
            //since we can now call it directly which is faster.
            if (Cache.TryGetSubroutine(Position, out ATranslatedSub OldSub))
            {
                foreach (long CallerPos in OldSub.GetCallerPositions())
                {
                    if (Cache.TryGetSubroutine(Position, out ATranslatedSub CallerSub))
                    {
                        CallerSub.MarkForReJit();
                    }
                }
            }

            ATranslatedSub Subroutine = Context.GetSubroutine();

            Subroutine.SetType(ATranslatedSubType.SubTier1);

            Cache.AddOrUpdate(Position, Subroutine, GetGraphInstCount(Graph));
        }

        private string GetSubroutineName(long Position)
        {
            return $"Sub{Position:x16}";
        }

        private int GetGraphInstCount(ABlock[] Graph)
        {
            int Size = 0;

            foreach (ABlock Block in Graph)
            {
                Size += Block.OpCodes.Count;
            }

            return Size;
        }
    }
}