aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/Instructions/InstEmitSimdMemory.cs
blob: dedf0fa059bb9e1fb6f7cb05b7b6ce1530efe39f (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
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
using ARMeilleure.Decoders;
using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.State;
using ARMeilleure.Translation;
using System.Diagnostics;

using static ARMeilleure.Instructions.InstEmitHelper;
using static ARMeilleure.Instructions.InstEmitMemoryHelper;
using static ARMeilleure.IntermediateRepresentation.Operand.Factory;

namespace ARMeilleure.Instructions
{
    static partial class InstEmit
    {
        public static void Ld__Vms(ArmEmitterContext context)
        {
            EmitSimdMemMs(context, isLoad: true);
        }

        public static void Ld__Vss(ArmEmitterContext context)
        {
            EmitSimdMemSs(context, isLoad: true);
        }

        public static void St__Vms(ArmEmitterContext context)
        {
            EmitSimdMemMs(context, isLoad: false);
        }

        public static void St__Vss(ArmEmitterContext context)
        {
            EmitSimdMemSs(context, isLoad: false);
        }

        private static void EmitSimdMemMs(ArmEmitterContext context, bool isLoad)
        {
            OpCodeSimdMemMs op = (OpCodeSimdMemMs)context.CurrOp;

            Operand n = GetIntOrSP(context, op.Rn);

            long offset = 0;

#pragma warning disable IDE0055 // Disable formatting
            for (int rep   = 0; rep   < op.Reps;   rep++)
            for (int elem  = 0; elem  < op.Elems;  elem++)
            for (int sElem = 0; sElem < op.SElems; sElem++)
            {
                int rtt = (op.Rt + rep + sElem) & 0x1f;

                Operand tt = GetVec(rtt);

                Operand address = context.Add(n, Const(offset));

                if (isLoad)
                {
                    EmitLoadSimd(context, address, tt, rtt, elem, op.Size);

                    if (op.RegisterSize == RegisterSize.Simd64 && elem == op.Elems - 1)
                    {
                        context.Copy(tt, context.VectorZeroUpper64(tt));
                    }
                }
                else
                {
                    EmitStoreSimd(context, address, rtt, elem, op.Size);
                }

                offset += 1 << op.Size;
            }
#pragma warning restore IDE0055

            if (op.WBack)
            {
                EmitSimdMemWBack(context, offset);
            }
        }

        private static void EmitSimdMemSs(ArmEmitterContext context, bool isLoad)
        {
            OpCodeSimdMemSs op = (OpCodeSimdMemSs)context.CurrOp;

            Operand n = GetIntOrSP(context, op.Rn);

            long offset = 0;

            if (op.Replicate)
            {
                // Only loads uses the replicate mode.
                Debug.Assert(isLoad, "Replicate mode is not valid for stores.");

                int elems = op.GetBytesCount() >> op.Size;

                for (int sElem = 0; sElem < op.SElems; sElem++)
                {
                    int rt = (op.Rt + sElem) & 0x1f;

                    Operand t = GetVec(rt);

                    Operand address = context.Add(n, Const(offset));

                    for (int index = 0; index < elems; index++)
                    {
                        EmitLoadSimd(context, address, t, rt, index, op.Size);
                    }

                    if (op.RegisterSize == RegisterSize.Simd64)
                    {
                        context.Copy(t, context.VectorZeroUpper64(t));
                    }

                    offset += 1 << op.Size;
                }
            }
            else
            {
                for (int sElem = 0; sElem < op.SElems; sElem++)
                {
                    int rt = (op.Rt + sElem) & 0x1f;

                    Operand t = GetVec(rt);

                    Operand address = context.Add(n, Const(offset));

                    if (isLoad)
                    {
                        EmitLoadSimd(context, address, t, rt, op.Index, op.Size);
                    }
                    else
                    {
                        EmitStoreSimd(context, address, rt, op.Index, op.Size);
                    }

                    offset += 1 << op.Size;
                }
            }

            if (op.WBack)
            {
                EmitSimdMemWBack(context, offset);
            }
        }

        private static void EmitSimdMemWBack(ArmEmitterContext context, long offset)
        {
            OpCodeMemReg op = (OpCodeMemReg)context.CurrOp;

            Operand n = GetIntOrSP(context, op.Rn);
            Operand m;

            if (op.Rm != RegisterAlias.Zr)
            {
                m = GetIntOrZR(context, op.Rm);
            }
            else
            {
                m = Const(offset);
            }

            context.Copy(n, context.Add(n, m));
        }
    }
}