aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/IntermediateRepresentation/OperandType.cs
blob: 67ebdcde4f6a243452c1f9f85fed3f14bc9b4a7a (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
using System;

namespace ARMeilleure.IntermediateRepresentation
{
    enum OperandType
    {
        None,
        I32,
        I64,
        FP32,
        FP64,
        V128,
    }

    static class OperandTypeExtensions
    {
        public static bool IsInteger(this OperandType type)
        {
            return type == OperandType.I32 ||
                   type == OperandType.I64;
        }

        public static RegisterType ToRegisterType(this OperandType type)
        {
            return type switch
            {
                OperandType.FP32 => RegisterType.Vector,
                OperandType.FP64 => RegisterType.Vector,
                OperandType.I32 => RegisterType.Integer,
                OperandType.I64 => RegisterType.Integer,
                OperandType.V128 => RegisterType.Vector,
                _ => throw new InvalidOperationException($"Invalid operand type \"{type}\"."),
            };
        }

        public static int GetSizeInBytes(this OperandType type)
        {
            return type switch
            {
                OperandType.FP32 => 4,
                OperandType.FP64 => 8,
                OperandType.I32 => 4,
                OperandType.I64 => 8,
                OperandType.V128 => 16,
                _ => throw new InvalidOperationException($"Invalid operand type \"{type}\"."),
            };
        }

        public static int GetSizeInBytesLog2(this OperandType type)
        {
            return type switch
            {
                OperandType.FP32 => 2,
                OperandType.FP64 => 3,
                OperandType.I32 => 2,
                OperandType.I64 => 3,
                OperandType.V128 => 4,
                _ => throw new InvalidOperationException($"Invalid operand type \"{type}\"."),
            };
        }
    }
}