aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Translation/ILGeneratorEx.cs
blob: 6b1512d29d9b2bf182767339b05d989e247bf7a3 (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
using System;

namespace ChocolArm64
{
    using System.Reflection.Emit;

    static class ILGeneratorEx
    {
        public static void EmitLdc_I4(this ILGenerator generator, int value)
        {
            switch (value)
            {
                case  0: generator.Emit(OpCodes.Ldc_I4_0);      break;
                case  1: generator.Emit(OpCodes.Ldc_I4_1);      break;
                case  2: generator.Emit(OpCodes.Ldc_I4_2);      break;
                case  3: generator.Emit(OpCodes.Ldc_I4_3);      break;
                case  4: generator.Emit(OpCodes.Ldc_I4_4);      break;
                case  5: generator.Emit(OpCodes.Ldc_I4_5);      break;
                case  6: generator.Emit(OpCodes.Ldc_I4_6);      break;
                case  7: generator.Emit(OpCodes.Ldc_I4_7);      break;
                case  8: generator.Emit(OpCodes.Ldc_I4_8);      break;
                case -1: generator.Emit(OpCodes.Ldc_I4_M1);     break;
                default: generator.Emit(OpCodes.Ldc_I4, value); break;
            }
        }

        public static void EmitLdarg(this ILGenerator generator, int index)
        {
            switch (index)
            {
                case 0:  generator.Emit(OpCodes.Ldarg_0); break;
                case 1:  generator.Emit(OpCodes.Ldarg_1); break;
                case 2:  generator.Emit(OpCodes.Ldarg_2); break;
                case 3:  generator.Emit(OpCodes.Ldarg_3); break;

                default:
                    if ((uint)index <= byte.MaxValue)
                    {
                        generator.Emit(OpCodes.Ldarg_S, (byte)index);
                    }
                    else if ((uint)index < ushort.MaxValue)
                    {
                        generator.Emit(OpCodes.Ldarg, (short)index);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException(nameof(index));
                    }
                    break;
            }
        }

        public static void EmitStarg(this ILGenerator generator, int index)
        {
            if ((uint)index <= byte.MaxValue)
            {
                generator.Emit(OpCodes.Starg_S, (byte)index);
            }
            else if ((uint)index < ushort.MaxValue)
            {
                generator.Emit(OpCodes.Starg, (short)index);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
        }

        public static void EmitLdloc(this ILGenerator generator, int index)
        {
            switch (index)
            {
                case 0:  generator.Emit(OpCodes.Ldloc_0); break;
                case 1:  generator.Emit(OpCodes.Ldloc_1); break;
                case 2:  generator.Emit(OpCodes.Ldloc_2); break;
                case 3:  generator.Emit(OpCodes.Ldloc_3); break;

                default:
                    if ((uint)index <= byte.MaxValue)
                    {
                        generator.Emit(OpCodes.Ldloc_S, (byte)index);
                    }
                    else if ((uint)index < ushort.MaxValue)
                    {
                        generator.Emit(OpCodes.Ldloc, (short)index);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException(nameof(index));
                    }
                    break;
            }
        }

        public static void EmitStloc(this ILGenerator generator, int index)
        {
            switch (index)
            {
                case 0:  generator.Emit(OpCodes.Stloc_0); break;
                case 1:  generator.Emit(OpCodes.Stloc_1); break;
                case 2:  generator.Emit(OpCodes.Stloc_2); break;
                case 3:  generator.Emit(OpCodes.Stloc_3); break;

                default:
                    if ((uint)index <= byte.MaxValue)
                    {
                        generator.Emit(OpCodes.Stloc_S, (byte)index);
                    }
                    else if ((uint)index < ushort.MaxValue)
                    {
                        generator.Emit(OpCodes.Stloc, (short)index);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException(nameof(index));
                    }
                    break;
            }
        }
    }
}