aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/CodeGen/X86/CallingConvention.cs
blob: 953fef5b0e9e86d12c4590f0876d194bf1f285b1 (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
using System;

namespace ARMeilleure.CodeGen.X86
{
    static class CallingConvention
    {
        private const int RegistersMask = 0xffff;

        public static int GetIntAvailableRegisters()
        {
            return RegistersMask & ~(1 << (int)X86Register.Rsp);
        }

        public static int GetVecAvailableRegisters()
        {
            return RegistersMask;
        }

        public static int GetIntCallerSavedRegisters()
        {
            if (GetCurrentCallConv() == CallConvName.Windows)
            {
                return (1 << (int)X86Register.Rax) |
                       (1 << (int)X86Register.Rcx) |
                       (1 << (int)X86Register.Rdx) |
                       (1 << (int)X86Register.R8)  |
                       (1 << (int)X86Register.R9)  |
                       (1 << (int)X86Register.R10) |
                       (1 << (int)X86Register.R11);
            }
            else /* if (GetCurrentCallConv() == CallConvName.SystemV) */
            {
                return (1 << (int)X86Register.Rax) |
                       (1 << (int)X86Register.Rcx) |
                       (1 << (int)X86Register.Rdx) |
                       (1 << (int)X86Register.Rsi) |
                       (1 << (int)X86Register.Rdi) |
                       (1 << (int)X86Register.R8)  |
                       (1 << (int)X86Register.R9)  |
                       (1 << (int)X86Register.R10) |
                       (1 << (int)X86Register.R11);
            }
        }

        public static int GetVecCallerSavedRegisters()
        {
            if (GetCurrentCallConv() == CallConvName.Windows)
            {
                return (1 << (int)X86Register.Xmm0) |
                       (1 << (int)X86Register.Xmm1) |
                       (1 << (int)X86Register.Xmm2) |
                       (1 << (int)X86Register.Xmm3) |
                       (1 << (int)X86Register.Xmm4) |
                       (1 << (int)X86Register.Xmm5);
            }
            else /* if (GetCurrentCallConv() == CallConvName.SystemV) */
            {
                return RegistersMask;
            }
        }

        public static int GetIntCalleeSavedRegisters()
        {
            return GetIntCallerSavedRegisters() ^ RegistersMask;
        }

        public static int GetVecCalleeSavedRegisters()
        {
            return GetVecCallerSavedRegisters() ^ RegistersMask;
        }

        public static int GetArgumentsOnRegsCount()
        {
            return 4;
        }

        public static int GetIntArgumentsOnRegsCount()
        {
            return 6;
        }

        public static int GetVecArgumentsOnRegsCount()
        {
            return 8;
        }

        public static X86Register GetIntArgumentRegister(int index)
        {
            if (GetCurrentCallConv() == CallConvName.Windows)
            {
                switch (index)
                {
                    case 0: return X86Register.Rcx;
                    case 1: return X86Register.Rdx;
                    case 2: return X86Register.R8;
                    case 3: return X86Register.R9;
                }
            }
            else /* if (GetCurrentCallConv() == CallConvName.SystemV) */
            {
                switch (index)
                {
                    case 0: return X86Register.Rdi;
                    case 1: return X86Register.Rsi;
                    case 2: return X86Register.Rdx;
                    case 3: return X86Register.Rcx;
                    case 4: return X86Register.R8;
                    case 5: return X86Register.R9;
                }
            }

            throw new ArgumentOutOfRangeException(nameof(index));
        }

        public static X86Register GetVecArgumentRegister(int index)
        {
            int count;

            if (GetCurrentCallConv() == CallConvName.Windows)
            {
                count = 4;
            }
            else /* if (GetCurrentCallConv() == CallConvName.SystemV) */
            {
                count = 8;
            }

            if ((uint)index < count)
            {
                return X86Register.Xmm0 + index;
            }

            throw new ArgumentOutOfRangeException(nameof(index));
        }

        public static X86Register GetIntReturnRegister()
        {
            return X86Register.Rax;
        }

        public static X86Register GetIntReturnRegisterHigh()
        {
            return X86Register.Rdx;
        }

        public static X86Register GetVecReturnRegister()
        {
            return X86Register.Xmm0;
        }

        public static CallConvName GetCurrentCallConv()
        {
            return OperatingSystem.IsWindows()
                ? CallConvName.Windows
                : CallConvName.SystemV;
        }
    }
}